1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Annotation; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Config\DoctrineConfigInterface; |
8
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
9
|
|
|
use Doctrine\Common\Annotations\PsrCachedReader; |
10
|
|
|
use Doctrine\Common\Annotations\Reader; |
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
12
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
13
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
15
|
|
|
use Psr\Container\ContainerExceptionInterface; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
|
18
|
|
|
final class PsrCacheReaderFactory extends AbstractFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @throws ContainerExceptionInterface |
22
|
|
|
* @throws ServiceNotFoundException |
23
|
|
|
* @throws ServiceNotCreatedException |
24
|
|
|
*/ |
25
|
|
|
public function __invoke( |
26
|
|
|
ContainerInterface $container, |
27
|
|
|
string $requestedName, |
28
|
|
|
array $options = null |
29
|
|
|
): PsrCachedReader { |
30
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName); |
31
|
|
|
|
32
|
|
|
$reader = $options['reader'] ?? null; |
33
|
|
|
if (null === $reader) { |
34
|
|
|
throw new ServiceNotCreatedException( |
35
|
|
|
sprintf('The required \'reader\' configuration option is missing for service \'%s\'', $requestedName), |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$cache = $options['cache'] ?? null; |
40
|
|
|
if (null === $cache) { |
41
|
|
|
throw new ServiceNotCreatedException( |
42
|
|
|
sprintf('The required \'cache\' configuration option is missing for service \'%s\'', $requestedName), |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new PsrCachedReader( |
47
|
|
|
$this->getReader($container, $reader, $requestedName), |
48
|
|
|
$this->getCache($container, $cache, $requestedName), |
49
|
|
|
(bool)($options['debug'] ?? false), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws ContainerExceptionInterface |
55
|
|
|
* @throws ServiceNotCreatedException |
56
|
|
|
* @throws ServiceNotFoundException |
57
|
|
|
*/ |
58
|
|
|
private function getReader(ContainerInterface $container, Reader|string $reader, string $requestedName): Reader |
59
|
|
|
{ |
60
|
|
|
if ($reader instanceof Reader) { |
61
|
|
|
return $reader; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$reader = $this->getService($container, $reader, $requestedName); |
65
|
|
|
if (!$reader instanceof Reader) { |
66
|
|
|
throw new ServiceNotCreatedException( |
67
|
|
|
sprintf( |
68
|
|
|
'The reader must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
69
|
|
|
Reader::class, |
70
|
|
|
is_object($reader) ? get_class($reader) : gettype($reader), |
71
|
|
|
$requestedName, |
72
|
|
|
), |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $reader; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|array<string, mixed>|CacheItemPoolInterface $cache |
81
|
|
|
* |
82
|
|
|
* @throws ContainerExceptionInterface |
83
|
|
|
* @throws ServiceNotCreatedException |
84
|
|
|
* @throws ServiceNotFoundException |
85
|
|
|
*/ |
86
|
|
|
private function getCache( |
87
|
|
|
ContainerInterface|ServiceLocatorInterface $container, |
88
|
|
|
string|array|CacheItemPoolInterface $cache, |
89
|
|
|
string $requestedName |
90
|
|
|
): CacheItemPoolInterface { |
91
|
|
|
if (is_string($cache) && $container->has($cache)) { |
|
|
|
|
92
|
|
|
$cache = $container->get($cache); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (is_string($cache)) { |
|
|
|
|
96
|
|
|
$cache = $this->getCacheConfig($container, $cache, $requestedName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (is_array($cache)) { |
|
|
|
|
100
|
|
|
if (empty($cache['class'])) { |
101
|
|
|
throw new ServiceNotCreatedException(sprintf( |
102
|
|
|
'The required \'class\' configuration option is missing for service \'%s\'', |
103
|
|
|
$requestedName, |
104
|
|
|
)); |
105
|
|
|
} |
106
|
|
|
$cache = $this->getService($container, $cache['class'], $requestedName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$cache instanceof CacheItemPoolInterface) { |
110
|
|
|
throw new ServiceNotCreatedException( |
111
|
|
|
sprintf( |
112
|
|
|
'The cache must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
113
|
|
|
CacheItemPoolInterface::class, |
114
|
|
|
is_object($cache) ? get_class($cache) : gettype($cache), |
115
|
|
|
$requestedName, |
116
|
|
|
), |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $cache; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array<string, mixed> |
125
|
|
|
* |
126
|
|
|
* @throws ContainerExceptionInterface |
127
|
|
|
* @throws ServiceNotFoundException |
128
|
|
|
* @throws ServiceNotCreatedException |
129
|
|
|
*/ |
130
|
|
|
private function getCacheConfig(ContainerInterface $container, string $name, string $requestedName): array |
131
|
|
|
{ |
132
|
|
|
/** @var DoctrineConfigInterface $doctrineConfig */ |
133
|
|
|
$doctrineConfig = $this->getService($container, DoctrineConfigInterface::class, $requestedName); |
134
|
|
|
|
135
|
|
|
if (!$doctrineConfig instanceof DoctrineConfigInterface || !$doctrineConfig->hasCacheConfig($name)) { |
|
|
|
|
136
|
|
|
throw new ServiceNotCreatedException( |
137
|
|
|
sprintf( |
138
|
|
|
'The cache configuration \'%s\' could not be found for service \'%s\'', |
139
|
|
|
$name, |
140
|
|
|
$requestedName, |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $doctrineConfig->getCacheConfig($name); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|