Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a4dcde

Browse files
committed
bug #41670 [HttpFoundation] allow savePath of NativeFileSessionHandler to be null (simon.chrzanowski)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation] allow savePath of NativeFileSessionHandler to be null | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #41669 | License | MIT By introducing the great Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory it is possible to configure session handler via DSN. But if I want to use the NativeFileSessionHandler to use configured session.save_path of php.ini, it doesn't work as expected because the $savePath constructor param is an empty string (and not NULL) by using "file://" as DSN. Commits ------- f8a082d [HttpFoundation] allow savePath of NativeFileSessionHandler to be null
2 parents ffb0d2d + f8a082d commit 6a4dcde

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public static function createHandler($connection): AbstractSessionHandler
4848
case !\is_string($connection):
4949
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', \get_class($connection)));
5050
case 0 === strpos($connection, 'file://'):
51-
return new StrictSessionHandler(new NativeFileSessionHandler(substr($connection, 7)));
51+
$savePath = substr($connection, 7);
52+
53+
return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
5254

5355
case 0 === strpos($connection, 'redis:'):
5456
case 0 === strpos($connection, 'rediss:'):
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory;
16+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
17+
18+
/**
19+
* Test class for SessionHandlerFactory.
20+
*
21+
* @author Simon <[email protected]>
22+
*
23+
* @runTestsInSeparateProcesses
24+
* @preserveGlobalState disabled
25+
*/
26+
class SessionHandlerFactoryTest extends TestCase
27+
{
28+
/**
29+
* @dataProvider provideConnectionDSN
30+
*/
31+
public function testCreateHandler(string $connectionDSN, string $expectedPath, string $expectedHandlerType)
32+
{
33+
$handler = SessionHandlerFactory::createHandler($connectionDSN);
34+
35+
$this->assertInstanceOf($expectedHandlerType, $handler);
36+
$this->assertEquals($expectedPath, ini_get('session.save_path'));
37+
}
38+
39+
public function provideConnectionDSN(): array
40+
{
41+
$base = sys_get_temp_dir();
42+
43+
return [
44+
'native file handler using save_path from php.ini' => ['connectionDSN' => 'file://', 'expectedPath' => ini_get('session.save_path'), 'expectedHandlerType' => StrictSessionHandler::class],
45+
'native file handler using provided save_path' => ['connectionDSN' => 'file://'.$base.'/session/storage', 'expectedPath' => $base.'/session/storage', 'expectedHandlerType' => StrictSessionHandler::class],
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)