SessionHandlerFactory.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if ($options = \is_string($connection) ? parse_url($connection) : false) {
  29. parse_str($options['query'] ?? '', $options);
  30. }
  31. switch (true) {
  32. case $connection instanceof \Redis:
  33. case $connection instanceof \RedisArray:
  34. case $connection instanceof \RedisCluster:
  35. case $connection instanceof \Predis\ClientInterface:
  36. case $connection instanceof RedisProxy:
  37. case $connection instanceof RedisClusterProxy:
  38. return new RedisSessionHandler($connection);
  39. case $connection instanceof \Memcached:
  40. return new MemcachedSessionHandler($connection);
  41. case $connection instanceof \PDO:
  42. return new PdoSessionHandler($connection);
  43. case !\is_string($connection):
  44. throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
  45. case str_starts_with($connection, 'file://'):
  46. $savePath = substr($connection, 7);
  47. return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
  48. case str_starts_with($connection, 'redis:'):
  49. case str_starts_with($connection, 'rediss:'):
  50. case str_starts_with($connection, 'memcached:'):
  51. if (!class_exists(AbstractAdapter::class)) {
  52. throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
  53. }
  54. $handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
  55. $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
  56. return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
  57. case str_starts_with($connection, 'pdo_oci://'):
  58. if (!class_exists(DriverManager::class)) {
  59. throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
  60. }
  61. $connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
  62. // no break;
  63. case str_starts_with($connection, 'mssql://'):
  64. case str_starts_with($connection, 'mysql://'):
  65. case str_starts_with($connection, 'mysql2://'):
  66. case str_starts_with($connection, 'pgsql://'):
  67. case str_starts_with($connection, 'postgres://'):
  68. case str_starts_with($connection, 'postgresql://'):
  69. case str_starts_with($connection, 'sqlsrv://'):
  70. case str_starts_with($connection, 'sqlite://'):
  71. case str_starts_with($connection, 'sqlite3://'):
  72. return new PdoSessionHandler($connection, $options ?: []);
  73. }
  74. throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
  75. }
  76. }