1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Infrastructure\Provider; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Cmp\Cache\Application\CacheFactory; |
7
|
|
|
use Cmp\Cache\Infrastructure\RedisCache; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
use Pimple\ServiceProviderInterface; |
11
|
|
|
use Redis; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PimpleCacheProvider |
15
|
|
|
* |
16
|
|
|
* @package Cmp\Cache\Infrastructure\Provider |
17
|
|
|
*/ |
18
|
|
|
class PimpleCacheProvider implements ServiceProviderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Registers cache services on the given container. |
22
|
|
|
* |
23
|
|
|
* @param Container $pimple A container instance |
24
|
|
|
*/ |
25
|
|
|
public function register(Container $pimple) |
26
|
|
|
{ |
27
|
|
|
$pimple['cache.backend'] = 'array'; |
28
|
|
|
$pimple['cache.debug'] = false; |
29
|
|
|
|
30
|
|
|
$pimple['cache'] = function () use ($pimple) { |
31
|
|
|
$cache = $this->getCache($pimple['cache.backend']); |
32
|
|
|
|
33
|
|
|
if ($pimple['cache.debug']) { |
34
|
|
|
$cache = CacheFactory::decorateForTesting($cache); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $cache; |
38
|
|
|
}; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets a cache object based on the backend requested |
43
|
|
|
* |
44
|
|
|
* @param $backend |
45
|
|
|
* |
46
|
|
|
* @return \Cmp\Cache\Domain\Cache |
47
|
|
|
*/ |
48
|
|
|
private function getCache($backend) |
49
|
|
|
{ |
50
|
|
|
if (is_array($backend) && isset($backend['redis'])) { |
51
|
|
|
return $this->getRedis($backend['redis']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($backend == 'array') { |
55
|
|
|
return CacheFactory::arrayCache(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
throw new InvalidArgumentException("Invalid cache backend"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return a factory closure to build |
63
|
|
|
* |
64
|
|
|
* @param array|Redis $redis |
65
|
|
|
* |
66
|
|
|
* @return \Cmp\Cache\Domain\Cache |
67
|
|
|
*/ |
68
|
|
|
private function getRedis($redis) |
69
|
|
|
{ |
70
|
|
|
if ($redis instanceof Redis) { |
|
|
|
|
71
|
|
|
return CacheFactory::redisCache($redis); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$host = $this->getParameter($redis, 'host', RedisCache::DEFAULT_HOST); |
75
|
|
|
$port = $this->getParameter($redis, 'port', RedisCache::DEFAULT_PORT); |
76
|
|
|
$db = $this->getParameter($redis, 'db', RedisCache::DEFAULT_DB); |
77
|
|
|
$timeout = $this->getParameter($redis, 'timeout', RedisCache::DEFAULT_TIMEOUT); |
78
|
|
|
|
79
|
|
|
return CacheFactory::redisFromParams($host, $port, $db, $timeout); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $options |
84
|
|
|
* @param string $key |
85
|
|
|
* @param mixed $default |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
private function getParameter(array $options, $key, $default) |
90
|
|
|
{ |
91
|
|
|
return isset($options[$key]) ? $options[$key] : $default; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|