|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Lock\Store; |
13 | 13 |
|
| 14 | +use Doctrine\DBAL\Connection; |
14 | 15 | use Symfony\Component\Cache\Adapter\AbstractAdapter; |
15 | 16 | use Symfony\Component\Cache\Traits\RedisClusterProxy; |
16 | 17 | use Symfony\Component\Cache\Traits\RedisProxy; |
|
25 | 26 | class StoreFactory |
26 | 27 | { |
27 | 28 | /** |
28 | | - * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|\Memcached|\Zookeeper|string $connection Connection or DSN or Store short name |
| 29 | + * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\PDO|Connection|\Zookeeper|string $connection Connection or DSN or Store short name |
29 | 30 | * |
30 | 31 | * @return PersistingStoreInterface |
31 | 32 | */ |
32 | 33 | public static function createStore($connection) |
33 | 34 | { |
34 | | - if ( |
35 | | - $connection instanceof \Redis || |
36 | | - $connection instanceof \RedisArray || |
37 | | - $connection instanceof \RedisCluster || |
38 | | - $connection instanceof \Predis\ClientInterface || |
39 | | - $connection instanceof RedisProxy || |
40 | | - $connection instanceof RedisClusterProxy |
41 | | - ) { |
42 | | - return new RedisStore($connection); |
43 | | - } |
44 | | - if ($connection instanceof \Memcached) { |
45 | | - return new MemcachedStore($connection); |
46 | | - } |
47 | | - if ($connection instanceof \Zookeeper) { |
48 | | - return new ZookeeperStore($connection); |
49 | | - } |
50 | | - if (!\is_string($connection)) { |
51 | | - throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection))); |
| 35 | + if (!\is_string($connection) && !\is_object($connection)) { |
| 36 | + throw new \TypeError(sprintf('Argument 1 passed to %s() must be a string or a connection object, %s given.', __METHOD__, \gettype($connection))); |
52 | 37 | } |
53 | 38 |
|
54 | 39 | switch (true) { |
| 40 | + case $connection instanceof \Redis: |
| 41 | + case $connection instanceof \RedisArray: |
| 42 | + case $connection instanceof \RedisCluster: |
| 43 | + case $connection instanceof \Predis\ClientInterface: |
| 44 | + case $connection instanceof RedisProxy: |
| 45 | + case $connection instanceof RedisClusterProxy: |
| 46 | + return new RedisStore($connection); |
| 47 | + |
| 48 | + case $connection instanceof \Memcached: |
| 49 | + return new MemcachedStore($connection); |
| 50 | + |
| 51 | + case $connection instanceof \PDO: |
| 52 | + case $connection instanceof Connection: |
| 53 | + return new PdoStore($connection); |
| 54 | + |
| 55 | + case $connection instanceof \Zookeeper: |
| 56 | + return new ZookeeperStore($connection); |
| 57 | + |
| 58 | + case !\is_string($connection): |
| 59 | + throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection))); |
55 | 60 | case 'flock' === $connection: |
56 | 61 | return new FlockStore(); |
| 62 | + |
57 | 63 | case 0 === strpos($connection, 'flock://'): |
58 | 64 | return new FlockStore(substr($connection, 8)); |
| 65 | + |
59 | 66 | case 'semaphore' === $connection: |
60 | 67 | return new SemaphoreStore(); |
61 | | - case 0 === strpos($connection, 'redis://') && class_exists(AbstractAdapter::class): |
62 | | - case 0 === strpos($connection, 'rediss://') && class_exists(AbstractAdapter::class): |
63 | | - return new RedisStore(AbstractAdapter::createConnection($connection, ['lazy' => true])); |
64 | | - case 0 === strpos($connection, 'memcached://') && class_exists(AbstractAdapter::class): |
65 | | - return new MemcachedStore(AbstractAdapter::createConnection($connection, ['lazy' => true])); |
66 | | - case 0 === strpos($connection, 'sqlite:'): |
| 68 | + |
| 69 | + case 0 === strpos($connection, 'redis://'): |
| 70 | + case 0 === strpos($connection, 'rediss://'): |
| 71 | + case 0 === strpos($connection, 'memcached://'): |
| 72 | + if (!class_exists(AbstractAdapter::class)) { |
| 73 | + throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $this->dsn)); |
| 74 | + } |
| 75 | + $connection = AbstractAdapter::createConnection($connection, ['lazy' => true]); |
| 76 | + |
| 77 | + return 0 === strpos($connection, 'memcached://') ? new MemcachedStore($connection) : new RedisStore($connection); |
| 78 | + |
| 79 | + case 0 === strpos($connection, 'mssql://'): |
67 | 80 | case 0 === strpos($connection, 'mysql:'): |
68 | | - case 0 === strpos($connection, 'pgsql:'): |
69 | | - case 0 === strpos($connection, 'oci:'): |
70 | | - case 0 === strpos($connection, 'sqlsrv:'): |
71 | | - case 0 === strpos($connection, 'sqlite3://'): |
72 | 81 | case 0 === strpos($connection, 'mysql2://'): |
| 82 | + case 0 === strpos($connection, 'oci:'): |
| 83 | + case 0 === strpos($connection, 'oci8://'): |
| 84 | + case 0 === strpos($connection, 'pdo_oci://'): |
| 85 | + case 0 === strpos($connection, 'pgsql:'): |
73 | 86 | case 0 === strpos($connection, 'postgres://'): |
74 | 87 | case 0 === strpos($connection, 'postgresql://'): |
75 | | - case 0 === strpos($connection, 'mssql://'): |
| 88 | + case 0 === strpos($connection, 'sqlsrv:'): |
| 89 | + case 0 === strpos($connection, 'sqlite:'): |
| 90 | + case 0 === strpos($connection, 'sqlite3://'): |
76 | 91 | return new PdoStore($connection); |
| 92 | + |
77 | 93 | case 0 === strpos($connection, 'zookeeper://'): |
78 | 94 | return new ZookeeperStore(ZookeeperStore::createConnection($connection)); |
79 | | - default: |
80 | | - throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection)); |
81 | 95 | } |
| 96 | + |
| 97 | + throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection)); |
82 | 98 | } |
83 | 99 | } |
0 commit comments