1 | <?php |
||
20 | class CacheFactory extends AbstractFactory |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritDoc} |
||
24 | * |
||
25 | * @return \Doctrine\Common\Cache\Cache |
||
26 | * |
||
27 | * @throws RuntimeException |
||
28 | */ |
||
29 | 3 | public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
|
30 | { |
||
31 | 3 | $options = $this->getOptions($container, 'cache'); |
|
32 | 3 | $class = $options->getClass(); |
|
33 | |||
34 | 3 | if (! $class) { |
|
35 | throw new RuntimeException('Cache must have a class name to instantiate'); |
||
36 | } |
||
37 | |||
38 | 3 | $instance = $options->getInstance(); |
|
39 | |||
40 | 3 | if (is_string($instance) && $container->has($instance)) { |
|
41 | 1 | $instance = $container->get($instance); |
|
42 | } |
||
43 | |||
44 | 3 | if ($container->has($class)) { |
|
45 | 1 | $cache = $container->get($class); |
|
46 | } else { |
||
47 | 2 | switch ($class) { |
|
48 | case Cache\FilesystemCache::class: |
||
49 | $cache = new $class($options->getDirectory()); |
||
50 | break; |
||
51 | |||
52 | case LaminasStorageCache::class: |
||
53 | case Cache\PredisCache::class: |
||
54 | 1 | $cache = new $class($instance); |
|
55 | 1 | break; |
|
56 | |||
57 | default: |
||
58 | 1 | $cache = new $class(); |
|
59 | 1 | break; |
|
60 | } |
||
61 | } |
||
62 | |||
63 | 3 | if ($cache instanceof Cache\MemcacheCache) { |
|
64 | $cache->setMemcache($instance); |
||
65 | 3 | } elseif ($cache instanceof Cache\MemcachedCache) { |
|
66 | $cache->setMemcached($instance); |
||
67 | 3 | } elseif ($cache instanceof Cache\RedisCache) { |
|
68 | $cache->setRedis($instance); |
||
69 | } |
||
70 | |||
71 | 3 | if ($cache instanceof CacheProvider) { |
|
72 | 3 | $namespace = $options->getNamespace(); |
|
73 | 3 | if ($namespace) { |
|
74 | 2 | $cache->setNamespace($namespace); |
|
75 | } |
||
76 | } |
||
77 | |||
78 | 3 | return $cache; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | * |
||
84 | * @return \Doctrine\Common\Cache\Cache |
||
85 | * |
||
86 | * @throws RuntimeException |
||
87 | */ |
||
88 | 5 | public function createService(ServiceLocatorInterface $container) |
|
92 | |||
93 | 3 | public function getOptionsClass() : string |
|
97 | } |
||
98 |