123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- 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;
- class SessionHandlerFactory
- {
-
- 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('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
- }
- $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('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
- }
- $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();
-
- 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));
- }
- }
|