8000 [FrameworkBundle] Fix wiring session.handler when handler_id is null by nicolas-grekas · Pull Request #49745 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Fix wiring session.handler when handler_id is null #49745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1178,16 +1178,8 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c

// session handler (the internal callback registered with PHP session management)
if (null === $config['handler_id']) {
// Set the handler class to be null
if ($container->hasDefinition('session.storage.native')) {
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
} else {
$container->getDefinition('session.storage.factory.native')->replaceArgument(1, null);
$container->getDefinition('session.storage.factory.php_bridge')->replaceArgument(0, null);
}

$container->setAlias('session.handler', 'session.handler.native_file');
$config['save_path'] = null;
$container->setAlias('session.handler', 'session.handler.native');
} else {
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
])
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "session.storage.factory.mock_file" instead.')

->set('session.handler.native', StrictSessionHandler::class)
->args([
inline_service(\SessionHandler::class),
])

->set('session.handler.native_file', StrictSessionHandler::class)
->args([
inline_service(NativeFileSessionHandler::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,8 @@ public function testNullSessionHandler()
$container = $this->createContainerFromFile('session');

$this->assertTrue($container->hasAlias(SessionInterface::class), '->registerSessionConfiguration() loads session.xml');
$this->assertNull($container->getDefinition('session.storage.factory.native')->getArgument(1));
$this->assertNull($container->getDefinition('session.storage.factory.php_bridge')->getArgument(0));
$this->assertSame('session.handler.native_file', (string) $container->getAlias('session.handler'));
$this->assertNull($container->getParameter('session.save_path'));
$this->assertSame('session.handler.native', (string) $container->getAlias('session.handler'));

$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector'];
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
Expand All @@ -667,9 +666,8 @@ public function testNullSessionHandlerLegacy()
$container = $this->createContainerFromFile('session_legacy');

$this->assertTrue($container->hasAlias(SessionInterface::class), '->registerSessionConfiguration() loads session.xml');
$this->assertNull($container->getDefinition('session.storage.native')->getArgument(1));
$this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
$this->assertSame('session.handler.native_file', (string) $container->getAlias('session.handler'));
$this->assertNull($container->getParameter('session.save_path'));
$this->assertSame('session.handler.native', (string) $container->getAlias('session.handler'));

$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector'];
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function __construct(string $savePath = null)
throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s".', $baseDir));
}

ini_set('session.save_path', $savePath);
ini_set('session.save_handler', 'files');
if ($savePath !== \ini_get('session.save_path')) {
ini_set('session.save_path', $savePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to check if different before setting the config?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not exactly needed for the fix, but this might remove useless "ini_set(): Session ini settings cannot be changed when a session is active".

}
if ('files' !== \ini_get('session.save_handler')) {
ini_set('session.save_handler', 'files');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,10 @@ public function setOptions(array $options)
*/
public function setSaveHandler($saveHandler = null)
{
if (!$saveHandler instanceof AbstractProxy &&
!$saveHandler instanceof \SessionHandlerInterface &&
null !== $saveHandler) {
if (!$saveHandler instanceof AbstractProxy
&& !$saveHandler instanceof \SessionHandlerInterface
&& null !== $saveHandler
) {
throw new \InvalidArgumentException('Must be instance of AbstractProxy; implement \SessionHandlerInterface; or be null.');
}

Expand Down
0