Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #34177 [HttpFoundation][FrameworkBundle] allow configuring th…
…e session handler with a DSN (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation][FrameworkBundle] allow configuring the session handler with a DSN | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - ```yaml framework: session: handler_id: 'redis://localhost' handler_id: '%env(REDIS_URL)%' handler_id: '%env(DATABASE_URL)%' handler_id: 'file://%kernel.project_dir%/var/sessions' ``` etc. the database connection is not shared with the ORM (don't mess with transactions.) redis/memcached connections are shared between cache and session. (as a reminder, cache and ORM share the db connection: we're ok with trashing the cache on a rollback) Lock-related changes are a follow up of #34043. (fabbot failure is false positive) Commits ------- de9c61f [HttpFoundation][FrameworkBundle] allow configuring the session handler with a DSN
- Loading branch information
Showing
7 changed files
with
155 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use Symfony\Component\Cache\Adapter\AbstractAdapter; | ||
use Symfony\Component\Cache\Traits\RedisClusterProxy; | ||
use Symfony\Component\Cache\Traits\RedisProxy; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class SessionHandlerFactory | ||
{ | ||
/** | ||
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|string $connection Connection or DSN | ||
*/ | ||
public static function createHandler($connection): AbstractSessionHandler | ||
{ | ||
if (!\is_string($connection) && !\is_object($connection)) { | ||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be a string or a connection object, %s given.', __METHOD__, \gettype($connection))); | ||
} | ||
|
||
switch (true) { | ||
case $connection instanceof \Redis: | ||
case $connection instanceof \RedisArray: | ||
case $connection instanceof \RedisCluster: | ||
case $connection instanceof \Predis\ClientInterface: | ||
case $connection instanceof RedisProxy: | ||
case $connection instanceof RedisClusterProxy: | ||
return new RedisSessionHandler($connection); | ||
|
||
case $connection instanceof \Memcached: | ||
return new MemcachedSessionHandler($connection); | ||
|
||
case $connection instanceof \PDO: | ||
return new PdoSessionHandler($connection); | ||
|
||
case !\is_string($connection): | ||
throw new \InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection))); | ||
case 0 === strpos($connection, 'file://'): | ||
return new StrictSessionHandler(new NativeFileSessionHandler(substr($connection, 7))); | ||
|
||
case 0 === strpos($connection, 'redis://'): | ||
case 0 === strpos($connection, 'rediss://'): | ||
case 0 === strpos($connection, 'memcached://'): | ||
if (!class_exists(AbstractAdapter::class)) { | ||
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $this->dsn)); | ||
} | ||
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]); | ||
|
||
return 0 === strpos($connection, 'memcached://') ? new MemcachedSessionHandler($connection) : new RedisSessionHandler($connection); | ||
|
||
case 0 === strpos($connection, 'pdo_oci://'): | ||
if (!class_exists(DriverManager::class)) { | ||
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection)); | ||
} | ||
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection(); | ||
// no break; | ||
|
||
case 0 === strpos($connection, 'mssql://'): | ||
case 0 === strpos($connection, 'mysql://'): | ||
case 0 === strpos($connection, 'mysql2://'): | ||
case 0 === strpos($connection, 'pgsql://'): | ||
case 0 === strpos($connection, 'postgres://'): | ||
case 0 === strpos($connection, 'postgresql://'): | ||
case 0 === strpos($connection, 'sqlsrv://'): | ||
case 0 === strpos($connection, 'sqlite://'): | ||
case 0 === strpos($connection, 'sqlite3://'): | ||
return new PdoSessionHandler($connection); | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters