|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Egils\Component\Cache package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Egidijus Lukauskas <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Egils\Component\Cache; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
14
|
|
|
|
|
15
|
|
|
class CacheManager implements CacheManagerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var CacheItemPoolInterface[] */ |
|
18
|
|
|
private $adapters = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $defaultAdapter = null; |
|
22
|
|
|
|
|
23
|
11 |
|
public function __construct(array $adapters = [], $defaultAdapterName = null) |
|
24
|
|
|
{ |
|
25
|
11 |
|
foreach ($adapters as $name => $adapter) { |
|
26
|
11 |
|
$this->addAdapter($name, $adapter); |
|
27
|
11 |
|
} |
|
28
|
|
|
|
|
29
|
11 |
|
if (null !== $defaultAdapterName) { |
|
30
|
2 |
|
$this->setDefaultAdapterName($defaultAdapterName); |
|
31
|
1 |
|
} |
|
32
|
11 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @param CacheItemPoolInterface $adapter |
|
37
|
|
|
* @param bool $default Should this adapter be marked as default? |
|
38
|
|
|
* |
|
39
|
|
|
* @throws CacheException |
|
40
|
|
|
*/ |
|
41
|
11 |
|
public function addAdapter($name, CacheItemPoolInterface $adapter, $default = false) |
|
42
|
|
|
{ |
|
43
|
11 |
|
if (false === is_string($name)) { |
|
44
|
1 |
|
throw CacheException::adapterNameNotString($name); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
11 |
|
if (true === array_key_exists($name, $this->adapters)) { |
|
48
|
1 |
|
throw CacheException::adapterAlreadyExists($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
11 |
|
$this->adapters[$name] = $adapter; |
|
52
|
|
|
|
|
53
|
11 |
|
if (true === $default) { |
|
54
|
1 |
|
$this->setDefaultAdapterName($name); |
|
55
|
1 |
|
} |
|
56
|
11 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function getAdapter($name) |
|
62
|
|
|
{ |
|
63
|
6 |
|
if (false === $this->hasAdapter($name)) { |
|
64
|
2 |
|
throw CacheException::adapterDoesNotExist($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
return $this->adapters[$name]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
8 |
|
public function hasAdapter($name) |
|
74
|
|
|
{ |
|
75
|
8 |
|
return isset($this->adapters[$name]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function hasAdapterInstance(CacheItemPoolInterface $adapter) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return in_array($adapter, $this->adapters, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function removeAdapter($name) |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (true === $this->hasAdapter($name)) { |
|
92
|
1 |
|
unset($this->adapters[$name]); |
|
93
|
1 |
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $name |
|
98
|
|
|
* @throws CacheException |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function setDefaultAdapterName($name) |
|
101
|
|
|
{ |
|
102
|
5 |
|
if (false === $this->hasAdapter($name)) { |
|
103
|
2 |
|
throw CacheException::adapterDoesNotExist($name); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
$this->defaultAdapter = $name; |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
4 |
|
public function getDefaultAdapter() |
|
113
|
|
|
{ |
|
114
|
4 |
|
if (null === $this->defaultAdapter) { |
|
115
|
1 |
|
throw CacheException::defaultAdapterNotSet(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
return $this->getAdapter($this->defaultAdapter); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|