SessionHandlerFactory.php 3.7 KB

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