SessionHandlerFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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__, get_debug_type($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_debug_type($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. }