8000 [HttpFoundation][FrameworkBundle] allow configuring the session handl… · symfony/symfony@de9c61f · GitHub
[go: up one dir, main page]

Skip to content

Commit de9c61f

Browse files
[HttpFoundation][FrameworkBundle] allow configuring the session handler with a DSN
1 parent d082732 commit de9c61f

File tree

7 files changed

+155
-35
lines changed

7 files changed

+155
-35
lines changed

src/Symfony/Bundle/FrameworkBundle/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
-----

src/Symfony/Bundle/FrameworkBundle/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']);

src/Symfony/Bundle/FrameworkBundle/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">

src/Symfony/Bundle/FrameworkBundle/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",

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* `PdoSessionHandler` now precalculates the expiry timestamp in the lifetime column,
1111
make sure to run `CREATE INDEX EXPIRY ON sessions (sess_lifetime)` to update your database
1212
to speed up garbage collection of expired sessions.
13+
* added `SessionHandlerFactory` to create session handlers with a DSN
1314

1415
4.3.0
1516
-----
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Session\Storage\Handler;
13+
14+
use Doctrine\DBAL\DriverManager;
15+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
16+
use Symfony\Component\Cache\Traits\RedisClusterProxy;
17+
use Symfony\Component\Cache\Traits\RedisProxy;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class SessionHandlerFactory
23+
{
24+
/**
25+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|string $connection Connection or DSN
26+
*/
27+
public static function createHandler($connection): AbstractSessionHandler
28+
{
29+
if (!\is_string($connection) && !\is_object($connection)) {
30+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be a string or a connection object, %s given.', __METHOD__, \gettype($connection)));
31+
}
32+
33+
switch (true) {
34+
case $connection instanceof \Redis:
35+
case $connection instanceof \RedisArray:
36+
case $connection instanceof \RedisCluster:
37+
case $connection instanceof \Predis\ClientInterface:
38+
case $connection instanceof RedisProxy:
39+
case $connection instanceof RedisClusterProxy:
40+
return new RedisSessionHandler($connection);
41+
42+
case $connection instanceof \Memcached:
43+
return new MemcachedSessionHandler($connection);
44+
45+
case $connection instanceof \PDO:
46+
return new PdoSessionHandler($connection);
47+
48+
case !\is_string($connection):
49+
throw new \InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
50+
case 0 === strpos($connection, 'file://'):
51+
return new StrictSessionHandler(new NativeFileSessionHandler(substr($connection, 7)));
52+
53+
case 0 === strpos($connection, 'redis://'):
54+
case 0 === strpos($connection, 'rediss://'):
55+
case 0 === strpos($connection, 'memcached://'):
56+
if (!class_exists(AbstractAdapter::class)) {
57+
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $this->dsn));
58+
}
59+
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
60+
61+
return 0 === strpos($connection, 'memcached://') ? new MemcachedSessionHandler($connection) : new RedisSessionHandler($connection);
62+
63+
case 0 === strpos($connection, 'pdo_oci://'):
64+
if (!class_exists(DriverManager::class)) {
65+
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
66+
}
67+
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
68+
// no break;
69+
70+
case 0 === strpos($connection, 'mssql://'):
71+
case 0 === strpos($connection, 'mysql://'):
72+
case 0 === strpos($connection, 'mysql2://'):
73+
case 0 === strpos($connection, 'pgsql://'):
74+
case 0 === strpos($connection, 'postgres://'):
75+
case 0 === strpos($connection, 'postgresql://'):
76+
case 0 === strpos($connection, 'sqlsrv://'):
77+
case 0 === strpos($connection, 'sqlite://'):
78+
case 0 === strpos($connection, 'sqlite3://'):
79+
return new PdoSessionHandler($connection);
80+
}
81+
82+
throw new \InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
83+
}
84+
}

src/Symfony/Component/Lock/Store/StoreFactory.php

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Doctrine\DBAL\Connection;
1415
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1516
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1617
use Symfony\Component\Cache\Traits\RedisProxy;
@@ -25,59 +26,74 @@
2526
class StoreFactory
2627
{
2728
/**
28-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|\Memcached|\Zookeeper|string $connection Connection or DSN or Store short name
29+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|Connection|\Zookeeper|string $connection Connection or DSN or Store short name
2930
*
3031
* @return PersistingStoreInterface
3132
*/
3233
public static function createStore($connection)
3334
{
34-
if (
35-
$connection instanceof \Redis ||
36-
$connection instanceof \RedisArray ||
37-
$connection instanceof \RedisCluster ||
38-
$connection instanceof \Predis\ClientInterface ||
39-
$connection instanceof RedisProxy ||
40-
$connection instanceof RedisClusterProxy
41-
) {
42-
return new RedisStore($connection);
43-
}
44-
if ($connection instanceof \Memcached) {
45-
return new MemcachedStore($connection);
46-
}
47-
if ($connection instanceof \Zookeeper) {
48-
return new ZookeeperStore($connection);
49-
}
50-
if (!\is_string($connection)) {
51-
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
35+
if (!\is_string($connection) && !\is_object($connection)) {
36+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be a string or a connection object, %s given.', __METHOD__, \gettype($connection)));
5237
}
5338

5439
switch (true) {
40+
case $connection instanceof \Redis:
41+
case $connection instanceof \RedisArray:
42+
case $connection instanceof \RedisCluster:
43+
case $connection instanceof \Predis\ClientInterface:
44+
case $connection instanceof RedisProxy:
45+
case $connection instanceof RedisClusterProxy:
46+
return new RedisStore($connection);
47+
48+
case $connection instanceof \Memcached:
49+
return new MemcachedStore($connection);
50+
51+
case $connection instanceof \PDO:
52+
case $connection instanceof Connection:
53+
return new PdoStore($connection);
54+
55+
case $connection instanceof \Zookeeper:
56+
return new ZookeeperStore($connection);
57+
58+
case !\is_string($connection):
59+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
5560
case 'flock' === $connection:
5661
return new FlockStore();
62+
5763
case 0 === strpos($connection, 'flock://'):
5864
return new FlockStore(substr($connection, 8));
65+
5966
case 'semaphore' === $connection:
6067
return new SemaphoreStore();
61-
case 0 === strpos($connection, 'redis://') && class_exists(AbstractAdapter::class):
62-
case 0 === strpos($connection, 'rediss://') && class_exists(AbstractAdapter::class):
63-
return new RedisStore(AbstractAdapter::createConnection($connection, ['lazy' => true]));
64-
case 0 === strpos($connection, 'memcached://') && class_exists(AbstractAdapter::class):
65-
return new MemcachedStore(AbstractAdapter::createConnection($connection, ['lazy' => true]));
66-
case 0 === strpos($connection, 'sqlite:'):
68+
69+
case 0 === strpos($connection, 'redis://'):
70+
case 0 === strpos($connection, 'rediss://'):
71+
case 0 === strpos($connection, 'memcached://'):
72+
if (!class_exists(AbstractAdapter::class)) {
73+
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $this->dsn));
74+
}
75+
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
76+
77+
return 0 === strpos($connection, 'memcached://') ? new MemcachedStore($connection) : new RedisStore($connection);
78+
79+
case 0 === strpos($connection, 'mssql://'):
6780
case 0 === strpos($connection, 'mysql:'):
68-
case 0 === strpos($connection, 'pgsql:'):
69-
case 0 === strpos($connection, 'oci:'):
70-
case 0 === strpos($connection, 'sqlsrv:'):
71-
case 0 === strpos($connection, 'sqlite3://'):
7281
case 0 === strpos($connection, 'mysql2://'):
82+
case 0 === strpos($connection, 'oci:'):
83+
case 0 === strpos($connection, 'oci8://'):
84+
case 0 === strpos($connection, 'pdo_oci://'):
85+
case 0 === strpos($connection, 'pgsql:'):
7386
case 0 === strpos($connection, 'postgres://'):
7487
case 0 === strpos($connection, 'postgresql://'):
75-
case 0 === strpos($connection, 'mssql://'):
88+
case 0 === strpos($connection, 'sqlsrv:'):
89+
case 0 === strpos($connection, 'sqlite:'):
90+
case 0 === strpos($connection, 'sqlite3://'):
7691
return new PdoStore($connection);
92+
7793
case 0 === strpos($connection, 'zookeeper://'):
7894
return new ZookeeperStore(ZookeeperStore::createConnection($connection));
79-
default:
80-
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
8195
}
96+
97+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
8298
}
8399
}

0 commit comments

Comments
 (0)
0