Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/Handler/SessionHandlerFactory.php

raw symbols SCIP
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 Relay\Relay;
19 use Symfony\Component\Cache\Adapter\AbstractAdapter;
20
21 /**
22 * @author Nicolas Grekas <[email protected]>
23 */
24 class SessionHandlerFactory
25 {
26 public static function createHandler(object|string $connection, array $options = []): AbstractSessionHandler
27 {
28 if ($query = \is_string($connection) ? parse_url($connection) : false) {
29 parse_str($query['query'] ?? '', $query);
30
31 if (($options['ttl'] ?? null) instanceof \Closure) {
32 $query['ttl'] = $options['ttl'];
33 }
34 }
35 $options = ($query ?: []) + $options;
36
37 switch (true) {
38 case $connection instanceof \Redis:
39 case $connection instanceof Relay:
40 case $connection instanceof \RedisArray:
41 case $connection instanceof \RedisCluster:
42 case $connection instanceof \Predis\ClientInterface:
43 return new RedisSessionHandler($connection);
44
45 case $connection instanceof \Memcached:
46 return new MemcachedSessionHandler($connection);
47
48 case $connection instanceof \PDO:
49 return new PdoSessionHandler($connection);
50
51 case !\is_string($connection):
52 throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
53 case str_starts_with($connection, 'file://'):
54 $savePath = substr($connection, 7);
55
56 return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
57
58 case str_starts_with($connection, 'redis:'):
59 case str_starts_with($connection, 'rediss:'):
60 case str_starts_with($connection, 'valkey:'):
61 case str_starts_with($connection, 'valkeys:'):
62 case str_starts_with($connection, 'memcached:'):
63 if (!class_exists(AbstractAdapter::class)) {
64 throw new \InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
65 }
66 $handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
67 $connection = preg_replace('/([?&])prefix=[^&]*+&?/', '\1', $connection);
68 $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
69
70 return new $handlerClass($connection, array_intersect_key($options, ['prefix' => 1, 'ttl' => 1]));
71
72 case str_starts_with($connection, 'pdo_oci://'):
73 if (!class_exists(DriverManager::class)) {
74 throw new \InvalidArgumentException('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
75 }
76 $connection[3] = '-';
77 $params = (new DsnParser())->parse($connection);
78 $config = new Configuration();
79 $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
80
81 $connection = DriverManager::getConnection($params, $config)->getNativeConnection();
82 // no break;
83
84 case str_starts_with($connection, 'mssql://'):
85 case str_starts_with($connection, 'mysql://'):
86 case str_starts_with($connection, 'mysql2://'):
87 case str_starts_with($connection, 'pgsql://'):
88 case str_starts_with($connection, 'postgres://'):
89 case str_starts_with($connection, 'postgresql://'):
90 case str_starts_with($connection, 'sqlsrv://'):
91 case str_starts_with($connection, 'sqlite://'):
92 case str_starts_with($connection, 'sqlite3://'):
93 return new PdoSessionHandler($connection, $options);
94 }
95
96 throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', $connection));
97 }
98 }