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
|
|
|
public function __construct(CacheBuilder $builder = null) |
32
|
|
|
{ |
33
|
|
|
$this->builder = $builder ?: new CacheBuilder(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Registers cache services on the given container. |
38
|
|
|
* |
39
|
|
|
* @param Container $pimple A container instance |
40
|
|
|
*/ |
41
|
|
|
public function register(Container $pimple) |
42
|
|
|
{ |
43
|
|
|
$pimple['cache.backends'] = ['array' => ['backend' => 'array']]; |
44
|
|
|
$pimple['cache.exceptions'] = true; |
45
|
|
|
$pimple['cache.logger'] = null; |
46
|
|
|
$pimple['cache.log_level'] = LogLevel::ALERT; |
47
|
|
|
|
48
|
|
|
$pimple['cache'] = function () use ($pimple) { |
49
|
|
|
return $this->build($pimple); |
50
|
|
|
}; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Container $pimple |
55
|
|
|
* |
56
|
|
|
* @return Cache |
57
|
|
|
*/ |
58
|
|
|
private function build(Container $pimple) |
59
|
|
|
{ |
60
|
|
|
if (!$pimple['cache.exceptions']) { |
61
|
|
|
$this->builder->withoutExceptions(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($pimple['cache.logger'] instanceof LoggerInterface) { |
65
|
|
|
$this->builder->withLogging($pimple['cache.logger'], $pimple['cache.log_level']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($pimple['cache.backends'] as $backend) { |
69
|
|
|
$this->addBackend($pimple, $backend['backend'], $backend); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->builder->build(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Container $pimple |
77
|
|
|
* @param string $backend |
78
|
|
|
* @param array $options |
79
|
|
|
* |
80
|
|
|
* @return $this|CacheServiceProvider |
81
|
|
|
*/ |
82
|
|
|
private function addBackend(Container $pimple, $backend, array $options) |
83
|
|
|
{ |
84
|
|
|
if ($backend == 'array') { |
85
|
|
|
return $this->builder->withArrayCache(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($backend == 'redis') { |
89
|
|
|
return $this->buildRedis($pimple, $options); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($backend instanceof Cache) { |
93
|
|
|
return $this->builder->withCache($backend); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->builder->withCache($pimple[$backend]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Container $pimple |
101
|
|
|
* @param array $options |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
private function buildRedis(Container $pimple, array $options) |
106
|
|
|
{ |
107
|
|
|
if (!isset($options['connection'])) { |
108
|
|
|
return $this->builder->withRedisCacheFromParams( |
109
|
|
|
$this->getParameter($options, 'host', RedisCache::DEFAULT_HOST), |
110
|
|
|
$this->getParameter($options, 'port', RedisCache::DEFAULT_HOST), |
111
|
|
|
$this->getParameter($options, 'db', RedisCache::DEFAULT_DB), |
112
|
|
|
$this->getParameter($options, 'timeout', RedisCache::DEFAULT_TIMEOUT) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($options['connection'] instanceof Redis) { |
117
|
|
|
return $this->builder->withRedis($options['connection']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->builder->withRedis($pimple[$options['connection']]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $options |
125
|
|
|
* @param string $key |
126
|
|
|
* @param mixed $default |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
private function getParameter(array $options, $key, $default) |
131
|
|
|
{ |
132
|
|
|
return isset($options[$key]) ? $options[$key] : $default; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|