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 (is_string($reader)) { |
61
|
|
|
$reader = $this->getService($container, $reader, $requestedName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$reader instanceof Reader) { |
65
|
|
|
throw new ServiceNotCreatedException( |
66
|
|
|
sprintf( |
67
|
|
|
'The reader must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
68
|
|
|
Reader::class, |
69
|
|
|
is_object($reader) ? get_class($reader) : gettype($reader), |
70
|
|
|
$requestedName, |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $reader; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|array<string, mixed>|CacheItemPoolInterface $cache |
80
|
|
|
* |
81
|
|
|
* @throws ContainerExceptionInterface |
82
|
|
|
* @throws ServiceNotCreatedException |
83
|
|
|
* @throws ServiceNotFoundException |
84
|
|
|
*/ |
85
|
|
|
private function getCache( |
86
|
|
|
ContainerInterface|ServiceLocatorInterface $container, |
87
|
|
|
string|array|CacheItemPoolInterface $cache, |
88
|
|
|
string $requestedName |
89
|
|
|
): CacheItemPoolInterface { |
90
|
|
|
if (is_string($cache)) { |
|
|
|
|
91
|
|
|
/** @var DoctrineConfigInterface $doctrineConfig */ |
92
|
|
|
$doctrineConfig = $this->getService($container, DoctrineConfigInterface::class, $requestedName); |
93
|
|
|
|
94
|
|
|
if (!$doctrineConfig instanceof DoctrineConfigInterface || !$doctrineConfig->hasCacheConfig($cache)) { |
95
|
|
|
throw new ServiceNotCreatedException( |
96
|
|
|
sprintf( |
97
|
|
|
'The cache configuration \'%s\' could not be found for service \'%s\'', |
98
|
|
|
$cache, |
99
|
|
|
$requestedName, |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$cache = $doctrineConfig->getCacheConfig($cache); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (is_array($cache) && $container instanceof ServiceLocatorInterface) { |
108
|
|
|
if (empty($cache['class'])) { |
109
|
|
|
throw new ServiceNotCreatedException( |
110
|
|
|
sprintf( |
111
|
|
|
'The required cache \'class\' configuration option is missing for service \'%s\'', |
112
|
|
|
$requestedName |
113
|
|
|
), |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$cache = $this->buildService($container, $cache['class'], $cache['options'] ?? [], $requestedName); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!$cache instanceof CacheItemPoolInterface) { |
121
|
|
|
throw new ServiceNotCreatedException( |
122
|
|
|
sprintf( |
123
|
|
|
'The cache must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
124
|
|
|
CacheItemPoolInterface::class, |
125
|
|
|
is_object($cache) ? get_class($cache) : gettype($cache), |
126
|
|
|
$requestedName, |
127
|
|
|
), |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $cache; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|