forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoreFactory.php
108 lines (89 loc) · 4.52 KB
/
StoreFactory.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
103
104
105
106
107
108
<?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\Lock\Store;
use Doctrine\DBAL\Connection;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\PersistingStoreInterface;
/**
* StoreFactory create stores and connections.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class StoreFactory
{
/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\MongoDB\Collection|\PDO|Connection|\Zookeeper|string $connection Connection or DSN or Store short name
*
* @return PersistingStoreInterface
*/
public static function createStore($connection)
{
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)));
}
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 RedisStore($connection);
case $connection instanceof \Memcached:
return new MemcachedStore($connection);
case $connection instanceof \MongoDB\Collection:
return new MongoDbStore($connection);
case $connection instanceof \PDO:
case $connection instanceof Connection:
return new PdoStore($connection);
case $connection instanceof \Zookeeper:
return new ZookeeperStore($connection);
case !\is_string($connection):
throw new InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
case 'flock' === $connection:
return new FlockStore();
case 0 === strpos($connection, 'flock://'):
return new FlockStore(substr($connection, 8));
case 'semaphore' === $connection:
return new SemaphoreStore();
case 0 === strpos($connection, 'redis://'):
case 0 === strpos($connection, 'rediss://'):
case 0 === strpos($connection, 'memcached://'):
if (!class_exists(AbstractAdapter::class)) {
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
}
$storeClass = 0 === strpos($connection, 'memcached://') ? MemcachedStore::class : RedisStore::class;
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
return new $storeClass($connection);
case 0 === strpos($connection, 'mongodb'):
trigger_deprecation('symfony/lock', '5.2', 'Using "%s" to construct a "%s" with a connection URI string is deprecated. Use a "%s" instead.', __CLASS__, MongoDbStore::class, Collection::class);
return new MongoDbStore($connection);
case 0 === strpos($connection, 'mssql://'):
case 0 === strpos($connection, 'mysql:'):
case 0 === strpos($connection, 'mysql2://'):
case 0 === strpos($connection, 'oci:'):
case 0 === strpos($connection, 'oci8://'):
case 0 === strpos($connection, 'pdo_oci://'):
case 0 === strpos($connection, 'pgsql:'):
case 0 === strpos($connection, 'postgres://'):
case 0 === strpos($connection, 'postgresql://'):
case 0 === strpos($connection, 'sqlsrv:'):
case 0 === strpos($connection, 'sqlite:'):
case 0 === strpos($connection, 'sqlite3://'):
return new PdoStore($connection);
case 0 === strpos($connection, 'zookeeper://'):
return new ZookeeperStore(ZookeeperStore::createConnection($connection));
}
throw new InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));
}
}