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