1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineModule\Service; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
23
|
|
|
use Interop\Container\ContainerInterface; |
24
|
|
|
use RuntimeException; |
25
|
|
|
use Doctrine\Common\Cache; |
26
|
|
|
use DoctrineModule\Cache\ZendStorageCache; |
27
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cache ServiceManager factory |
31
|
|
|
* |
32
|
|
|
* @license MIT |
33
|
|
|
* @link http://www.doctrine-project.org/ |
34
|
|
|
* @author Kyle Spraggs <[email protected]> |
35
|
|
|
*/ |
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) |
106
|
|
|
{ |
107
|
5 |
|
return $this($container, \Doctrine\Common\Cache\Cache::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
3 |
|
public function getOptionsClass() |
114
|
|
|
{ |
115
|
3 |
|
return 'DoctrineModule\Options\Cache'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|