No Description

SessionHandlerFactory.php 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 str_starts_with($connection, 'file://'):
  43. $savePath = substr($connection, 7);
  44. return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
  45. case str_starts_with($connection, 'redis:'):
  46. case str_starts_with($connection, 'rediss:'):
  47. case str_starts_with($connection, 'memcached:'):
  48. if (!class_exists(AbstractAdapter::class)) {
  49. throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
  50. }
  51. $handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
  52. $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
  53. return new $handlerClass($connection);
  54. case str_starts_with($connection, 'pdo_oci://'):
  55. if (!class_exists(DriverManager::class)) {
  56. throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
  57. }
  58. $connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
  59. // no break;
  60. case str_starts_with($connection, 'mssql://'):
  61. case str_starts_with($connection, 'mysql://'):
  62. case str_starts_with($connection, 'mysql2://'):
  63. case str_starts_with($connection, 'pgsql://'):
  64. case str_starts_with($connection, 'postgres://'):
  65. case str_starts_with($connection, 'postgresql://'):
  66. case str_starts_with($connection, 'sqlsrv://'):
  67. case str_starts_with($connection, 'sqlite://'):
  68. case str_starts_with($connection, 'sqlite3://'):
  69. return new PdoSessionHandler($connection);
  70. }
  71. throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
  72. }
  73. }