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 Interop\Container\Exception\ContainerException; |
12
|
|
|
use StrokerCache\Exception\RuntimeException; |
13
|
|
|
use StrokerCache\IdGenerator\IdGeneratorPluginManager; |
14
|
|
|
use StrokerCache\Listener\ShouldCacheStrategyListener; |
15
|
|
|
use StrokerCache\Options\ModuleOptions; |
16
|
|
|
use StrokerCache\Service\CacheService; |
17
|
|
|
use StrokerCache\Strategy\CacheStrategyPluginManager; |
18
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
19
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
20
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
21
|
|
|
use Zend\ServiceManager\FactoryInterface; |
22
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
23
|
|
|
|
24
|
|
|
class CacheServiceFactory implements FactoryInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
30
|
|
|
{ |
31
|
|
|
return $this($serviceLocator, CacheService::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
38
|
|
|
{ |
39
|
|
|
$options = $container->get(ModuleOptions::class); |
40
|
|
|
$cacheStorage = $container->get('StrokerCache\Storage\CacheStorage'); |
41
|
|
|
|
42
|
|
|
$cacheService = new CacheService($cacheStorage, $options); |
43
|
|
|
|
44
|
|
|
$this->setupIdGenerator($cacheService, $options, $container); |
45
|
|
|
$this->attachStrategiesToEventManager($cacheService, $options, $container); |
46
|
|
|
|
47
|
|
|
return $cacheService; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param CacheService $cacheService |
52
|
|
|
* @param ModuleOptions $options |
53
|
|
|
* @param ContainerInterface $container |
54
|
|
|
* @throws RuntimeException |
55
|
|
|
*/ |
56
|
|
|
protected function setupIdGenerator( |
57
|
|
|
CacheService $cacheService, |
58
|
|
|
ModuleOptions $options, |
59
|
|
|
ContainerInterface $container |
60
|
|
|
) { |
61
|
|
|
$idGenerator = $options->getIdGenerator(); |
62
|
|
|
$idGeneratorManager = $container->get(IdGeneratorPluginManager::class); |
63
|
|
|
|
64
|
|
|
if ($idGeneratorManager->has($idGenerator)) { |
65
|
|
|
$cacheService->setIdGenerator($idGeneratorManager->get($idGenerator)); |
66
|
|
|
} else { |
67
|
|
|
throw new RuntimeException('No IdGenerator register for key ' . $idGenerator); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param CacheService $cacheService |
73
|
|
|
* @param ModuleOptions $options |
74
|
|
|
* @param ContainerInterface $container |
75
|
|
|
*/ |
76
|
|
|
protected function attachStrategiesToEventManager( |
77
|
|
|
CacheService $cacheService, |
78
|
|
|
ModuleOptions $options, |
79
|
|
|
ContainerInterface $container |
80
|
|
|
) { |
81
|
|
|
// Register enabled strategies on the cacheListener |
82
|
|
|
$strategies = $options->getStrategies(); |
83
|
|
|
if (isset($strategies['enabled'])) { |
84
|
|
|
/** @var $strategyPluginManager CacheStrategyPluginManager */ |
85
|
|
|
$strategyPluginManager = $container->get(CacheStrategyPluginManager::class); |
86
|
|
|
|
87
|
|
|
foreach ($strategies['enabled'] as $alias => $options) { |
88
|
|
|
if (is_numeric($alias)) { |
89
|
|
|
$alias = $options; |
90
|
|
|
} |
91
|
|
|
$strategy = $strategyPluginManager->get($alias, $options); |
92
|
|
|
|
93
|
|
|
if ($strategy instanceof ListenerAggregateInterface) { |
94
|
|
|
$listener = $strategy; |
95
|
|
|
} else { |
96
|
|
|
$listener = new ShouldCacheStrategyListener($strategy); |
97
|
|
|
} |
98
|
|
|
$listener->attach($cacheService->getEventManager()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.