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