Blame view

vendor/symfony/http-foundation/Session/Storage/Handler/SessionHandlerFactory.php 4.71 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  <?php
  
  /*
   * This file is part of the Symfony package.
   *
   * (c) Fabien Potencier <fabien@symfony.com>
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  
  use Doctrine\DBAL\Configuration;
  use Doctrine\DBAL\DriverManager;
  use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
  use Doctrine\DBAL\Tools\DsnParser;
  use Symfony\Component\Cache\Adapter\AbstractAdapter;
  use Symfony\Component\Cache\Traits\RedisClusterProxy;
  use Symfony\Component\Cache\Traits\RedisProxy;
  
  /**
   * @author Nicolas Grekas <p@tchwork.com>
   */
  class SessionHandlerFactory
  {
      /**
       * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|string $connection Connection or DSN
       */
      public static function createHandler($connection): AbstractSessionHandler
      {
          if (!\is_string($connection) && !\is_object($connection)) {
              throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
          }
  
          if ($options = \is_string($connection) ? parse_url($connection) : false) {
              parse_str($options['query'] ?? '', $options);
          }
  
          switch (true) {
              case $connection instanceof \Redis:
              case $connection instanceof \RedisArray:
              case $connection instanceof \RedisCluster:
              case $connection instanceof \Predis\ClientInterface:
              case $connection instanceof RedisProxy:
              case $connection instanceof RedisClusterProxy:
                  return new RedisSessionHandler($connection);
  
              case $connection instanceof \Memcached:
                  return new MemcachedSessionHandler($connection);
  
              case $connection instanceof \PDO:
                  return new PdoSessionHandler($connection);
  
              case !\is_string($connection):
                  throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
              case str_starts_with($connection, 'file://'):
                  $savePath = substr($connection, 7);
  
                  return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
  
              case str_starts_with($connection, 'redis:'):
              case str_starts_with($connection, 'rediss:'):
              case str_starts_with($connection, 'memcached:'):
                  if (!class_exists(AbstractAdapter::class)) {
                      throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
                  }
                  $handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
                  $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
  
                  return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
  
              case str_starts_with($connection, 'pdo_oci://'):
                  if (!class_exists(DriverManager::class)) {
                      throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
                  }
                  $connection[3] = '-';
                  $params = class_exists(DsnParser::class) ? (new DsnParser())->parse($connection) : ['url' => $connection];
                  $config = new Configuration();
                  if (class_exists(DefaultSchemaManagerFactory::class)) {
                      $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
                  }
  
                  $connection = DriverManager::getConnection($params, $config);
                  $connection = method_exists($connection, 'getNativeConnection') ? $connection->getNativeConnection() : $connection->getWrappedConnection();
                  // no break;
  
              case str_starts_with($connection, 'mssql://'):
              case str_starts_with($connection, 'mysql://'):
              case str_starts_with($connection, 'mysql2://'):
              case str_starts_with($connection, 'pgsql://'):
              case str_starts_with($connection, 'postgres://'):
              case str_starts_with($connection, 'postgresql://'):
              case str_starts_with($connection, 'sqlsrv://'):
              case str_starts_with($connection, 'sqlite://'):
              case str_starts_with($connection, 'sqlite3://'):
                  return new PdoSessionHandler($connection, $options ?: []);
          }
  
          throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
      }
  }