|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Bram Gerritsen [email protected] |
|
4
|
|
|
* @copyright (c) Bram Gerritsen 2013 |
|
5
|
|
|
* @license http://opensource.org/licenses/mit-license.php |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace StrokerCache\Factory; |
|
9
|
|
|
|
|
10
|
|
|
use Interop\Container\ContainerInterface; |
|
11
|
|
|
use StrokerCache\Exception\RuntimeException; |
|
12
|
|
|
use StrokerCache\IdGenerator\IdGeneratorPluginManager; |
|
13
|
|
|
use StrokerCache\Listener\ShouldCacheStrategyListener; |
|
14
|
|
|
use StrokerCache\Options\ModuleOptions; |
|
15
|
|
|
use StrokerCache\Service\CacheService; |
|
16
|
|
|
use StrokerCache\Strategy\CacheStrategyPluginManager; |
|
17
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
|
18
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
|
19
|
|
|
|
|
20
|
|
|
class CacheServiceFactory implements FactoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritDoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$options = $container->get(ModuleOptions::class); |
|
28
|
|
|
$cacheStorage = $container->get('StrokerCache\Storage\CacheStorage'); |
|
29
|
|
|
|
|
30
|
|
|
$cacheService = new CacheService($cacheStorage, $options); |
|
31
|
|
|
|
|
32
|
|
|
$this->setupIdGenerator($cacheService, $options, $container); |
|
33
|
|
|
$this->attachStrategiesToEventManager($cacheService, $options, $container); |
|
34
|
|
|
|
|
35
|
|
|
return $cacheService; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param CacheService $cacheService |
|
40
|
|
|
* @param ModuleOptions $options |
|
41
|
|
|
* @param ContainerInterface $container |
|
42
|
|
|
* @throws RuntimeException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function setupIdGenerator( |
|
45
|
|
|
CacheService $cacheService, |
|
46
|
|
|
ModuleOptions $options, |
|
47
|
|
|
ContainerInterface $container |
|
48
|
|
|
) { |
|
49
|
|
|
$idGenerator = $options->getIdGenerator(); |
|
50
|
|
|
$idGeneratorManager = $container->get(IdGeneratorPluginManager::class); |
|
51
|
|
|
|
|
52
|
|
|
if ($idGeneratorManager->has($idGenerator)) { |
|
53
|
|
|
$cacheService->setIdGenerator($idGeneratorManager->get($idGenerator)); |
|
54
|
|
|
} else { |
|
55
|
|
|
throw new RuntimeException('No IdGenerator register for key ' . $idGenerator); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param CacheService $cacheService |
|
61
|
|
|
* @param ModuleOptions $options |
|
62
|
|
|
* @param ContainerInterface $container |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function attachStrategiesToEventManager( |
|
65
|
|
|
CacheService $cacheService, |
|
66
|
|
|
ModuleOptions $options, |
|
67
|
|
|
ContainerInterface $container |
|
68
|
|
|
) { |
|
69
|
|
|
// Register enabled strategies on the cacheListener |
|
70
|
|
|
$strategies = $options->getStrategies(); |
|
71
|
|
|
if (isset($strategies['enabled'])) { |
|
72
|
|
|
/** @var $strategyPluginManager CacheStrategyPluginManager */ |
|
73
|
|
|
$strategyPluginManager = $container->get(CacheStrategyPluginManager::class); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($strategies['enabled'] as $alias => $options) { |
|
76
|
|
|
if (is_numeric($alias)) { |
|
77
|
|
|
$alias = $options; |
|
78
|
|
|
} |
|
79
|
|
|
$strategy = $strategyPluginManager->get($alias, $options); |
|
80
|
|
|
|
|
81
|
|
|
if ($strategy instanceof ListenerAggregateInterface) { |
|
82
|
|
|
$listener = $strategy; |
|
83
|
|
|
} else { |
|
84
|
|
|
$listener = new ShouldCacheStrategyListener($strategy); |
|
85
|
|
|
} |
|
86
|
|
|
$listener->attach($cacheService->getEventManager()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|