| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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\DriverManager;
|
|
|
15 |
use Symfony\Component\Cache\Adapter\AbstractAdapter;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
19 |
*/
|
|
|
20 |
class SessionHandlerFactory
|
|
|
21 |
{
|
|
|
22 |
public static function createHandler(object|string $connection, array $options = []): AbstractSessionHandler
|
|
|
23 |
{
|
|
|
24 |
if ($query = \is_string($connection) ? parse_url($connection) : false) {
|
|
|
25 |
parse_str($query['query'] ?? '', $query);
|
|
|
26 |
|
|
|
27 |
if (($options['ttl'] ?? null) instanceof \Closure) {
|
|
|
28 |
$query['ttl'] = $options['ttl'];
|
|
|
29 |
}
|
|
|
30 |
}
|
|
|
31 |
$options = ($query ?: []) + $options;
|
|
|
32 |
|
|
|
33 |
switch (true) {
|
|
|
34 |
case $connection instanceof \Redis:
|
|
|
35 |
case $connection instanceof \RedisArray:
|
|
|
36 |
case $connection instanceof \RedisCluster:
|
|
|
37 |
case $connection instanceof \Predis\ClientInterface:
|
|
|
38 |
return new RedisSessionHandler($connection);
|
|
|
39 |
|
|
|
40 |
case $connection instanceof \Memcached:
|
|
|
41 |
return new MemcachedSessionHandler($connection);
|
|
|
42 |
|
|
|
43 |
case $connection instanceof \PDO:
|
|
|
44 |
return new PdoSessionHandler($connection);
|
|
|
45 |
|
|
|
46 |
case !\is_string($connection):
|
|
|
47 |
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
|
|
|
48 |
case str_starts_with($connection, 'file://'):
|
|
|
49 |
$savePath = substr($connection, 7);
|
|
|
50 |
|
|
|
51 |
return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
|
|
|
52 |
|
|
|
53 |
case str_starts_with($connection, 'redis:'):
|
|
|
54 |
case str_starts_with($connection, 'rediss:'):
|
|
|
55 |
case str_starts_with($connection, 'memcached:'):
|
|
|
56 |
if (!class_exists(AbstractAdapter::class)) {
|
|
|
57 |
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
|
|
|
58 |
}
|
|
|
59 |
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
|
|
|
60 |
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
|
|
|
61 |
|
|
|
62 |
return new $handlerClass($connection, array_intersect_key($options, ['prefix' => 1, 'ttl' => 1]));
|
|
|
63 |
|
|
|
64 |
case str_starts_with($connection, 'pdo_oci://'):
|
|
|
65 |
if (!class_exists(DriverManager::class)) {
|
|
|
66 |
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
|
|
|
67 |
}
|
|
|
68 |
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
|
|
|
69 |
// no break;
|
|
|
70 |
|
|
|
71 |
case str_starts_with($connection, 'mssql://'):
|
|
|
72 |
case str_starts_with($connection, 'mysql://'):
|
|
|
73 |
case str_starts_with($connection, 'mysql2://'):
|
|
|
74 |
case str_starts_with($connection, 'pgsql://'):
|
|
|
75 |
case str_starts_with($connection, 'postgres://'):
|
|
|
76 |
case str_starts_with($connection, 'postgresql://'):
|
|
|
77 |
case str_starts_with($connection, 'sqlsrv://'):
|
|
|
78 |
case str_starts_with($connection, 'sqlite://'):
|
|
|
79 |
case str_starts_with($connection, 'sqlite3://'):
|
|
|
80 |
return new PdoSessionHandler($connection, $options);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
|
|
|
84 |
}
|
|
|
85 |
}
|