1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Factory\Pimple; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Backend\RedisCache; |
6
|
|
|
use Cmp\Cache\Cache; |
7
|
|
|
use Cmp\Cache\Factory\CacheBuilder; |
8
|
|
|
use Pimple\Container; |
9
|
|
|
use Pimple\ServiceProviderInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use Redis; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CacheServiceProvider |
16
|
|
|
* |
17
|
|
|
* @package Cmp\Cache\Factory\Pimple |
18
|
|
|
*/ |
19
|
|
|
class CacheServiceProvider implements ServiceProviderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var CacheBuilder |
23
|
|
|
*/ |
24
|
|
|
private $builder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PimpleCacheProvider constructor. |
28
|
|
|
* |
29
|
|
|
* @param CacheBuilder|null $builder |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct(CacheBuilder $builder = null) |
32
|
|
|
{ |
33
|
1 |
|
$this->builder = $builder ?: new CacheBuilder(); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Registers cache services on the given container. |
38
|
|
|
* |
39
|
|
|
* @param Container $pimple A container instance |
40
|
|
|
*/ |
41
|
1 |
|
public function register(Container $pimple) |
42
|
|
|
{ |
43
|
1 |
|
$pimple['cache.backends'] = ['array' => ['backend' => 'array']]; |
44
|
1 |
|
$pimple['cache.logging'] = ['logger' => null, 'level' => LogLevel::ALERT]; |
45
|
1 |
|
$pimple['cache.exceptions'] = true; |
46
|
|
|
|
47
|
1 |
|
$pimple['cache'] = function () use ($pimple) { |
48
|
1 |
|
return $this->build($pimple); |
49
|
|
|
}; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Container $pimple |
54
|
|
|
* |
55
|
|
|
* @return Cache |
56
|
|
|
*/ |
57
|
1 |
|
private function build(Container $pimple) |
58
|
|
|
{ |
59
|
1 |
|
if (!$pimple['cache.exceptions']) { |
60
|
1 |
|
$this->builder->withoutExceptions(); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
if ($pimple['cache.logging']['logger'] instanceof LoggerInterface) { |
64
|
1 |
|
$this->builder->withLogging($pimple['cache.logging']['logger'], $pimple['cache.logging']['level']); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
foreach ($pimple['cache.backends'] as $backend) { |
68
|
1 |
|
$this->addBackend($pimple, $backend['backend'], $backend); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->builder->build(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Container $pimple |
76
|
|
|
* @param string $backend |
77
|
|
|
* @param array $options |
78
|
|
|
* |
79
|
|
|
* @return $this|CacheServiceProvider |
80
|
|
|
*/ |
81
|
1 |
|
private function addBackend(Container $pimple, $backend, array $options) |
82
|
|
|
{ |
83
|
1 |
|
if ($backend == 'array') { |
84
|
1 |
|
return $this->builder->withArrayCache(); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
if ($backend == 'redis') { |
88
|
1 |
|
return $this->buildRedis($pimple, $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
if ($backend instanceof Cache) { |
92
|
1 |
|
return $this->builder->withCache($backend); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $this->builder->withCache($pimple[$backend]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Container $pimple |
100
|
|
|
* @param array $options |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
1 |
|
private function buildRedis(Container $pimple, array $options) |
105
|
|
|
{ |
106
|
1 |
|
if (!isset($options['connection'])) { |
107
|
1 |
|
return $this->builder->withRedisCacheFromParams( |
108
|
1 |
|
$this->getParameter($options, 'host', RedisCache::DEFAULT_HOST), |
109
|
1 |
|
$this->getParameter($options, 'port', RedisCache::DEFAULT_HOST), |
110
|
1 |
|
$this->getParameter($options, 'db', RedisCache::DEFAULT_DB), |
111
|
1 |
|
$this->getParameter($options, 'timeout', RedisCache::DEFAULT_TIMEOUT) |
112
|
1 |
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if ($options['connection'] instanceof Redis) { |
116
|
1 |
|
return $this->builder->withRedis($options['connection']); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $this->builder->withRedis($pimple[$options['connection']]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $options |
124
|
|
|
* @param string $key |
125
|
|
|
* @param mixed $default |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
1 |
|
private function getParameter(array $options, $key, $default) |
130
|
|
|
{ |
131
|
1 |
|
return isset($options[$key]) ? $options[$key] : $default; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|