1 | <?php |
||
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) |
|
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) |
|
52 | |||
53 | /** |
||
54 | * @param Container $pimple |
||
55 | * |
||
56 | * @return Cache |
||
57 | */ |
||
58 | 1 | private function build(Container $pimple) |
|
59 | { |
||
60 | 1 | if (!$pimple['cache.exceptions']) { |
|
61 | 1 | $this->builder->withoutExceptions(); |
|
62 | } |
||
63 | |||
64 | 1 | if ($pimple['cache.logger'] instanceof LoggerInterface) { |
|
65 | $this->builder->withLogging($pimple['cache.logger'], $pimple['cache.log_level']); |
||
66 | } |
||
67 | |||
68 | 1 | foreach ($pimple['cache.backends'] as $backend) { |
|
69 | 1 | $this->addBackend($pimple, $backend['backend'], $backend); |
|
70 | } |
||
71 | |||
72 | 1 | 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 | 1 | private function addBackend(Container $pimple, $backend, array $options) |
|
98 | |||
99 | /** |
||
100 | * @param Container $pimple |
||
101 | * @param array $options |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | 1 | private function buildRedis(Container $pimple, array $options) |
|
106 | { |
||
107 | 1 | if (!isset($options['connection'])) { |
|
108 | 1 | return $this->builder->withRedisCacheFromParams( |
|
109 | 1 | $this->getParameter($options, 'host', RedisCache::DEFAULT_HOST), |
|
110 | 1 | $this->getParameter($options, 'port', RedisCache::DEFAULT_HOST), |
|
111 | 1 | $this->getParameter($options, 'db', RedisCache::DEFAULT_DB), |
|
112 | 1 | $this->getParameter($options, 'timeout', RedisCache::DEFAULT_TIMEOUT) |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | 1 | if ($options['connection'] instanceof Redis) { |
|
117 | 1 | return $this->builder->withRedis($options['connection']); |
|
118 | } |
||
119 | |||
120 | 1 | 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 | 1 | private function getParameter(array $options, $key, $default) |
|
134 | } |
||
135 |