Skip to content

Commit 8be37e7

Browse files
[HttpFoundation][FrameworkBundle] allow configuring the session handler with a DSN
1 parent 1b54a4b commit 8be37e7

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Added sort option for `translation:update` command.
1919
* [BC Break] The `framework.messenger.routing.senders` config key is not deep merged anymore.
2020
* Added `secrets:*` commands and `%env(secret:...)%` processor to deal with secrets seamlessly.
21+
* Made `framework.session.handler_id` accept a DSN
2122

2223
4.3.0
2324
-----

DependencyInjection/FrameworkExtension.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ public function load(array $configs, ContainerBuilder $container)
240240
}
241241
}
242242

243+
// register cache before session so both can share the connection services
244+
$this->registerCacheConfiguration($config['cache'], $container);
245+
243246
if ($this->isConfigEnabled($container, $config['session'])) {
244247
if (!\extension_loaded('session')) {
245248
throw new LogicException('Session support cannot be enabled as the session extension is not installed. See https://php.net/session.installation for instructions.');
@@ -326,7 +329,6 @@ public function load(array $configs, ContainerBuilder $container)
326329
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
327330
$this->registerTranslatorConfiguration($config['translator'], $container, $loader, $config['default_locale']);
328331
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
329-
$this->registerCacheConfiguration($config['cache'], $container);
330332
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
331333
$this->registerDebugConfiguration($config['php_errors'], $container, $loader);
332334
$this->registerRouterConfiguration($config['router'], $container, $loader);
@@ -925,7 +927,18 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
925927
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
926928
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
927929
} else {
928-
$container->setAlias('session.handler', $config['handler_id'])->setPrivate(true);
930+
$container->resolveEnvPlaceholders($config['handler_id'], null, $usedEnvs);
931+
932+
if ($usedEnvs || preg_match('#^[a-z]++://#', $config['handler_id'])) {
933+
$id = '.cache_connection.'.ContainerBuilder::hash($config['handler_id']);
934+
935+
$container->getDefinition('session.abstract_handler')
936+
->replaceArgument(0, $container->hasDefinition($id) ? new Reference($id) : $config['handler_id']);
937+
938+
$container->setAlias('session.handler', 'session.abstract_handler')->setPrivate(true);
939+
} else {
940+
$container->setAlias('session.handler', $config['handler_id'])->setPrivate(true);
941+
}
929942
}
930943

931944
$container->setParameter('session.save_path', $config['save_path']);

Resources/config/session.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
</argument>
5757
</service>
5858

59+
<service id="session.abstract_handler" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler">
60+
<factory class="Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory" method="createHandler" />
61+
<argument />
62+
</service>
63+
5964
<service id="session_listener" class="Symfony\Component\HttpKernel\EventListener\SessionListener">
6065
<tag name="kernel.event_subscriber" />
6166
<argument type="service_locator">

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/config": "^4.3.4|^5.0",
2323
"symfony/dependency-injection": "^4.4|^5.0",
2424
"symfony/error-renderer": "^4.4|^5.0",
25-
"symfony/http-foundation": "^4.3|^5.0",
25+
"symfony/http-foundation": "^4.4|^5.0",
2626
"symfony/http-kernel": "^4.4",
2727
"symfony/polyfill-mbstring": "~1.0",
2828
"symfony/filesystem": "^3.4|^4.0|^5.0",

0 commit comments

Comments
 (0)