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