1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Cache; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Config\DoctrineConfigInterface; |
8
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
9
|
|
|
use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
10
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
13
|
|
|
use Psr\Container\ContainerExceptionInterface; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
16
|
|
|
|
17
|
|
|
final class CacheFactory extends AbstractFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @throws ServiceNotCreatedException |
21
|
|
|
* @throws ContainerExceptionInterface |
22
|
|
|
* @throws ServiceNotFoundException |
23
|
|
|
*/ |
24
|
|
|
public function __invoke( |
25
|
|
|
ContainerInterface $container, |
26
|
|
|
string $requestedName, |
27
|
|
|
array $options = null |
28
|
|
|
): DoctrineProvider { |
29
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName, 'cache'); |
30
|
|
|
|
31
|
|
|
$name = $options['name'] ?? null; |
32
|
|
|
if (empty($name)) { |
33
|
|
|
throw new ServiceNotCreatedException( |
34
|
|
|
sprintf('The required \'name\' configuration option is missing for service \'%s\'', $requestedName) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$config = $this->getCacheConfig($container, $name, $requestedName); |
39
|
|
|
|
40
|
|
|
$cache = $config['class'] ?? null; |
41
|
|
|
if (empty($cache)) { |
42
|
|
|
throw new ServiceNotCreatedException( |
43
|
|
|
sprintf('The required \'class\' configuration option is missing for service \'%s\'', $requestedName) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (is_string($cache)) { |
48
|
|
|
$cache = $this->getService($container, $cache, $requestedName); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!$cache instanceof CacheItemPoolInterface) { |
52
|
|
|
throw new ServiceNotCreatedException( |
53
|
|
|
sprintf( |
54
|
|
|
'The cache should be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
55
|
|
|
CacheItemPoolInterface::class, |
56
|
|
|
is_object($cache) ? get_class($cache) : gettype($cache), |
57
|
|
|
$requestedName, |
58
|
|
|
), |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var DoctrineProvider $provider */ |
63
|
|
|
$provider = DoctrineProvider::wrap($cache); |
64
|
|
|
|
65
|
|
|
if (!empty($config['namespace'])) { |
66
|
|
|
$provider->setNamespace($config['namespace']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $provider; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array<string, string> |
74
|
|
|
* |
75
|
|
|
* @throws ContainerExceptionInterface |
76
|
|
|
* @throws NotFoundExceptionInterface |
77
|
|
|
* @throws ServiceNotCreatedException |
78
|
|
|
*/ |
79
|
|
|
private function getCacheConfig(ContainerInterface $container, string $cacheName, string $requestedName): array |
80
|
|
|
{ |
81
|
|
|
/** @var DoctrineConfigInterface $doctrineConfig */ |
82
|
|
|
$doctrineConfig = $container->get(DoctrineConfigInterface::class); |
83
|
|
|
|
84
|
|
|
if (!$doctrineConfig->hasCacheConfig($cacheName)) { |
85
|
|
|
throw new ServiceNotCreatedException( |
86
|
|
|
sprintf( |
87
|
|
|
'Unable to find cache configuration for \'%s\' for service \'%s\'', |
88
|
|
|
$cacheName, |
89
|
|
|
$requestedName |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $doctrineConfig->getCacheConfig($cacheName); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|