1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This File is Part of the Validus package. |
4
|
|
|
* |
5
|
|
|
* @copyright (c) 2018 Validus <https://github.com/ValidusPHP/> |
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
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Validus\Cache; |
14
|
|
|
|
15
|
|
|
use Doctrine\DBAL\Connection; |
16
|
|
|
use PDO; |
17
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
20
|
|
|
use Symfony\Component\Cache\Adapter\ApcuAdapter; |
21
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
22
|
|
|
use Symfony\Component\Cache\Adapter\ChainAdapter; |
23
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
24
|
|
|
use Symfony\Component\Cache\Adapter\NullAdapter; |
25
|
|
|
use Symfony\Component\Cache\Adapter\PdoAdapter; |
26
|
|
|
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
27
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter; |
28
|
|
|
|
29
|
|
|
class CacheItemPoolFactory |
30
|
|
|
{ |
31
|
|
|
public const DEFAULT_ADAPTERS_CONFIG = [ |
32
|
|
|
'array' => [ |
33
|
|
|
'default_lifetime' => 0, |
34
|
|
|
'store_serialized' => true, |
35
|
|
|
], |
36
|
|
|
'apcu' => [ |
37
|
|
|
'namespace' => '', |
38
|
|
|
'default_lifetime' => 0, |
39
|
|
|
'version' => null, |
40
|
|
|
], |
41
|
|
|
'filesystem' => [ |
42
|
|
|
'namespace' => '', |
43
|
|
|
'default_lifetime' => 0, |
44
|
|
|
'directory' => null, |
45
|
|
|
], |
46
|
|
|
'redis' => [ |
47
|
|
|
'instance' => null, |
48
|
|
|
'namespace' => '', |
49
|
|
|
'default_lifetime' => 0, |
50
|
|
|
], |
51
|
|
|
'pdo' => [ |
52
|
|
|
'instance' => null, |
53
|
|
|
'dns' => null, |
54
|
|
|
'namespace' => '', |
55
|
|
|
'default_lifetime' => 0, |
56
|
|
|
'options' => [], |
57
|
|
|
], |
58
|
|
|
'php_files' => [ |
59
|
|
|
'namespace' => '', |
60
|
|
|
'default_lifetime' => 0, |
61
|
|
|
'directory' => null, |
62
|
|
|
], |
63
|
|
|
'chain' => [ |
64
|
|
|
'adapters' => ['array'], |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ContainerInterface $container |
70
|
|
|
* |
71
|
|
|
* @return CacheItemPoolInterface |
72
|
|
|
* |
73
|
|
|
* @throws \Symfony\Component\Cache\Exception\CacheException |
74
|
|
|
*/ |
75
|
|
|
public function __invoke(ContainerInterface $container): CacheItemPoolInterface |
76
|
|
|
{ |
77
|
|
|
$config = $container->has('config') ? $container->get('config') : []; |
78
|
|
|
|
79
|
|
|
$adapter = $config['cache']['adapter'] ?? 'array'; |
80
|
|
|
|
81
|
|
|
return $this->createAdapter($container, $adapter); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ContainerInterface $container |
86
|
|
|
* @param string $adapter |
87
|
|
|
* |
88
|
|
|
* @return AdapterInterface |
89
|
|
|
* |
90
|
|
|
* @throws \Symfony\Component\Cache\Exception\CacheException |
91
|
|
|
*/ |
92
|
|
|
protected function createAdapter(ContainerInterface $container, string $adapter): AdapterInterface |
93
|
|
|
{ |
94
|
|
|
$config = $this->retrieveAdapterConfig($container, $adapter); |
95
|
|
|
|
96
|
|
|
switch ($adapter) { |
97
|
|
|
case 'array': |
98
|
|
|
return new ArrayAdapter($config['default_lifetime'], $config['store_serialized']); |
99
|
|
|
case 'apcu': |
100
|
|
|
// @codeCoverageIgnoreStart |
101
|
|
|
return new ApcuAdapter($config['namespace'], $config['default_lifetime'], $config['version']); |
102
|
|
|
// @codeCoverageIgnoreEnd |
103
|
|
|
case 'filesystem': |
104
|
|
|
return new FilesystemAdapter($config['namespace'], $config['default_lifetime'], $config['directory']); |
105
|
|
|
case 'redis': |
106
|
|
|
// @codeCoverageIgnoreStart |
107
|
|
|
$instance = $config['instance']; |
108
|
|
|
|
109
|
|
|
if (\is_string($instance) && $container->has($instance)) { |
110
|
|
|
$instance = $container->get($instance); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new RedisAdapter($instance, $config['namespace'], $config['default_lifetime']); |
114
|
|
|
// @codeCoverageIgnoreEnd |
115
|
|
|
case 'pdo': |
116
|
|
|
// @codeCoverageIgnoreStart |
117
|
|
|
$instance = $config['instance']; |
118
|
|
|
|
119
|
|
|
if (\is_string($instance) && $container->has($instance)) { |
120
|
|
|
$instance = $container->get($instance); |
121
|
|
|
} elseif (!$instance instanceof PDO && !$instance instanceof Connection) { |
122
|
|
|
$instance = $config['dns']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new PdoAdapter($instance, $config['namespace'], $config['default_lifetime'], $config['options']); |
126
|
|
|
// @codeCoverageIgnoreEnd |
127
|
|
|
case 'php_files': |
128
|
|
|
return new PhpFilesAdapter($config['namespace'], $config['default_lifetime'], $config['directory']); |
129
|
|
|
case 'chain': |
130
|
|
|
$adapters = []; |
131
|
|
|
|
132
|
|
|
foreach ($config['adapters'] as $pool) { |
133
|
|
|
$adapters[] = $this->createAdapter($container, $pool); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new ChainAdapter($adapters); |
137
|
|
|
default: |
138
|
|
|
return new NullAdapter(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieves the config for a specific adapter. |
144
|
|
|
* |
145
|
|
|
* @param ContainerInterface $container |
146
|
|
|
* @param string $adapter |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
protected function retrieveAdapterConfig(ContainerInterface $container, string $adapter): array |
151
|
|
|
{ |
152
|
|
|
$applicationConfig = $container->has('config') ? $container->get('config') : []; |
153
|
|
|
$cacheConfig = $applicationConfig['cache'] ?? []; |
154
|
|
|
$adaptersConfig = $cacheConfig['adapters'] ?? []; |
155
|
|
|
|
156
|
|
|
return array_merge( |
157
|
|
|
static::DEFAULT_ADAPTERS_CONFIG[$adapter] ?? [], |
158
|
|
|
$adaptersConfig[$adapter] ?? [] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|