1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Governator; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use Symfony\Component\Cache\Adapter\AbstractAdapter; |
8
|
|
|
use Symfony\Component\Cache\Traits\RedisClusterProxy; |
9
|
|
|
use Symfony\Component\Cache\Traits\RedisProxy; |
10
|
|
|
use Zenstruck\Governator\Store\MemoryStore; |
11
|
|
|
use Zenstruck\Governator\Store\Psr16CacheStore; |
12
|
|
|
use Zenstruck\Governator\Store\Psr6CacheStore; |
13
|
|
|
use Zenstruck\Governator\Store\RedisStore; |
14
|
|
|
use Zenstruck\Governator\Store\UnlimitedStore; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Lock/Store/StoreFactory.php |
18
|
|
|
* |
19
|
|
|
* @author Kevin Bond <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class StoreFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Create a Throttle Store for passed connection DSN/object. |
25
|
|
|
* |
26
|
|
|
* @param string|object $connection |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException If not capable of handling passed connection |
29
|
|
|
*/ |
30
|
22 |
|
public static function create($connection): Store |
31
|
|
|
{ |
32
|
22 |
|
if (!\is_string($connection) && !\is_object($connection)) { |
33
|
1 |
|
throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, \gettype($connection))); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
switch (true) { |
37
|
21 |
|
case $connection instanceof Store: |
38
|
1 |
|
return $connection; |
39
|
20 |
|
case $connection instanceof CacheItemPoolInterface: |
40
|
1 |
|
return new Psr6CacheStore($connection); |
41
|
19 |
|
case $connection instanceof CacheInterface: |
42
|
1 |
|
return new Psr16CacheStore($connection); |
43
|
18 |
|
case $connection instanceof \Redis: |
44
|
17 |
|
case $connection instanceof \RedisArray: |
45
|
16 |
|
case $connection instanceof \RedisCluster: |
46
|
15 |
|
case $connection instanceof \Predis\ClientInterface: |
47
|
14 |
|
case $connection instanceof RedisProxy: |
48
|
13 |
|
case $connection instanceof RedisClusterProxy: |
49
|
6 |
|
return new RedisStore($connection); |
|
|
|
|
50
|
12 |
|
case !\is_string($connection): |
51
|
1 |
|
throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', \get_class($connection))); |
|
|
|
|
52
|
11 |
|
case 'memory' === $connection: |
53
|
8 |
|
return new MemoryStore(); |
54
|
3 |
|
case 'unlimited' === $connection: |
55
|
1 |
|
return new UnlimitedStore(); |
56
|
2 |
|
case 0 === \strpos($connection, 'redis:'): |
|
|
|
|
57
|
1 |
|
case 0 === \strpos($connection, 'rediss:'): |
58
|
1 |
|
if (!\class_exists(AbstractAdapter::class)) { |
59
|
|
|
throw new \InvalidArgumentException(\sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection)); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return new RedisStore(AbstractAdapter::createConnection($connection, ['lazy' => true])); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', $connection)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|