SessionHandlerFactory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Configuration;
  12. use Doctrine\DBAL\DriverManager;
  13. use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
  14. use Doctrine\DBAL\Tools\DsnParser;
  15. use Symfony\Component\Cache\Adapter\AbstractAdapter;
  16. use Symfony\Component\Cache\Traits\RedisClusterProxy;
  17. use Symfony\Component\Cache\Traits\RedisProxy;
  18. /**
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. */
  21. class SessionHandlerFactory
  22. {
  23. /**
  24. * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|string $connection Connection or DSN
  25. */
  26. public static function createHandler($connection): AbstractSessionHandler
  27. {
  28. if (!\is_string($connection) && !\is_object($connection)) {
  29. throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
  30. }
  31. if ($options = \is_string($connection) ? parse_url($connection) : false) {
  32. parse_str($options['query'] ?? '', $options);
  33. }
  34. switch (true) {
  35. case $connection instanceof \Redis:
  36. case $connection instanceof \RedisArray:
  37. case $connection instanceof \RedisCluster:
  38. case $connection instanceof \Predis\ClientInterface:
  39. case $connection instanceof RedisProxy:
  40. case $connection instanceof RedisClusterProxy:
  41. return new RedisSessionHandler($connection);
  42. case $connection instanceof \Memcached:
  43. return new MemcachedSessionHandler($connection);
  44. case $connection instanceof \PDO:
  45. return new PdoSessionHandler($connection);
  46. case !\is_string($connection):
  47. throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
  48. case str_starts_with($connection, 'file://'):
  49. $savePath = substr($connection, 7);
  50. return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
  51. case str_starts_with($connection, 'redis:'):
  52. case str_starts_with($connection, 'rediss:'):
  53. case str_starts_with($connection, 'memcached:'):
  54. if (!class_exists(AbstractAdapter::class)) {
  55. throw new \InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
  56. }
  57. $handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
  58. $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
  59. return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
  60. case str_starts_with($connection, 'pdo_oci://'):
  61. if (!class_exists(DriverManager::class)) {
  62. throw new \InvalidArgumentException('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
  63. }
  64. $connection[3] = '-';
  65. $params = class_exists(DsnParser::class) ? (new DsnParser())->parse($connection) : ['url' => $connection];
  66. $config = new Configuration();
  67. if (class_exists(DefaultSchemaManagerFactory::class)) {
  68. $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
  69. }
  70. $connection = DriverManager::getConnection($params, $config);
  71. // The condition should be removed once support for DBAL <3.3 is dropped
  72. $connection = method_exists($connection, 'getNativeConnection') ? $connection->getNativeConnection() : $connection->getWrappedConnection();
  73. // no break;
  74. case str_starts_with($connection, 'mssql://'):
  75. case str_starts_with($connection, 'mysql://'):
  76. case str_starts_with($connection, 'mysql2://'):
  77. case str_starts_with($connection, 'pgsql://'):
  78. case str_starts_with($connection, 'postgres://'):
  79. case str_starts_with($connection, 'postgresql://'):
  80. case str_starts_with($connection, 'sqlsrv://'):
  81. case str_starts_with($connection, 'sqlite://'):
  82. case str_starts_with($connection, 'sqlite3://'):
  83. return new PdoSessionHandler($connection, $options ?: []);
  84. }
  85. throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
  86. }
  87. }