|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Ctw\Middleware\PageCacheMiddleware; |
|
5
|
|
|
|
|
6
|
|
|
use Ctw\Middleware\PageCacheMiddleware\IdGenerator\IdGeneratorInterface; |
|
7
|
|
|
use Ctw\Middleware\PageCacheMiddleware\Strategy\StrategyInterface; |
|
8
|
|
|
use Laminas\Cache\Storage\Adapter\AbstractAdapter as StorageAdapter; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class PageCacheMiddlewareFactory |
|
12
|
|
|
{ |
|
13
|
2 |
|
public function __invoke(ContainerInterface $container): PageCacheMiddleware |
|
14
|
|
|
{ |
|
15
|
2 |
|
$config = $container->get('config'); |
|
16
|
2 |
|
assert(is_array($config)); |
|
17
|
|
|
|
|
18
|
2 |
|
$config = $config[PageCacheMiddleware::class]; |
|
19
|
|
|
|
|
20
|
2 |
|
$enabled = $this->getEnabled($config); |
|
21
|
2 |
|
$storageAdapter = $this->getStorageAdapter($container); |
|
22
|
2 |
|
$idGenerator = $this->getIdGenerator($container, $config); |
|
23
|
2 |
|
$strategy = $this->getStrategy($container, $config); |
|
24
|
|
|
|
|
25
|
2 |
|
$middleware = new PageCacheMiddleware(); |
|
26
|
|
|
|
|
27
|
2 |
|
$middleware->setEnabled($enabled); |
|
28
|
2 |
|
$middleware->setStorageAdapter($storageAdapter); |
|
29
|
2 |
|
$middleware->setIdGenerator($idGenerator); |
|
30
|
2 |
|
$middleware->setStrategy($strategy); |
|
31
|
|
|
|
|
32
|
2 |
|
return $middleware; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
private function getEnabled(array $config): bool |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $config['enabled']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
private function getStorageAdapter(ContainerInterface $container): StorageAdapter |
|
41
|
|
|
{ |
|
42
|
2 |
|
$storageAdapter = $container->get('ctw_cache_storage_adapter'); |
|
43
|
2 |
|
assert($storageAdapter instanceof StorageAdapter); |
|
44
|
|
|
|
|
45
|
2 |
|
return $storageAdapter; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
private function getIdGenerator(ContainerInterface $container, array $config): IdGeneratorInterface |
|
49
|
|
|
{ |
|
50
|
2 |
|
$factoryName = sprintf('%sFactory', $config['id_generator']); |
|
51
|
2 |
|
$factory = new $factoryName(); |
|
52
|
2 |
|
assert(is_callable($factory)); |
|
53
|
|
|
|
|
54
|
2 |
|
return $factory($container); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
private function getStrategy(ContainerInterface $container, array $config): StrategyInterface |
|
58
|
|
|
{ |
|
59
|
2 |
|
$keys = array_keys($config['strategy']); |
|
60
|
|
|
|
|
61
|
2 |
|
$factoryName = sprintf('%sFactory', array_shift($keys)); |
|
62
|
2 |
|
$factory = new $factoryName(); |
|
63
|
2 |
|
assert(is_callable($factory)); |
|
64
|
|
|
|
|
65
|
2 |
|
return $factory($container); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|