1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader; |
4
|
|
|
|
5
|
|
|
use As3\Bundle\ModlrBundle\DependencyInjection\Utility; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Loads the metadata cache service. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MetadataCache implements ServiceLoaderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Creates a file cache service definition. |
19
|
|
|
* |
20
|
|
|
* @param string $subClassName |
21
|
|
|
* @param array $cacheConfig |
22
|
|
|
* @param ContainerBuilder $container |
23
|
|
|
* @return Definition |
24
|
|
|
*/ |
25
|
|
|
private function createFileCache($subClassName, array $cacheConfig, ContainerBuilder $container) |
26
|
|
|
{ |
27
|
|
|
$cacheDir = $this->getFileCacheDir($cacheConfig, $container); |
28
|
|
|
Utility::appendParameter('dirs', 'metadata_cache_dir', $cacheDir, $container); |
29
|
|
|
|
30
|
|
|
return new Definition( |
31
|
|
|
Utility::getLibraryClass($subClassName), |
32
|
|
|
[$cacheDir] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates the redis cache service definition. |
38
|
|
|
* |
39
|
|
|
* @param array $cacheConfig |
40
|
|
|
* @return Definition |
41
|
|
|
*/ |
42
|
|
|
private function createRedisCache(array $cacheConfig) |
43
|
|
|
{ |
44
|
|
|
return new Definition( |
45
|
|
|
Utility::getLibraryClass('Metadata\Cache\RedisCache'), |
46
|
|
|
[new Reference($cacheConfig['parameters']['handler'])] |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the file cache directory. |
52
|
|
|
* |
53
|
|
|
* @param array $cacheConfig |
54
|
|
|
* @param ContainerBuilder $container |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
private function getFileCacheDir(array $cacheConfig, ContainerBuilder $container) |
58
|
|
|
{ |
59
|
|
|
$dir = sprintf('%s/as3_modlr', $container->getParameter('kernel.cache_dir')); |
60
|
|
|
if (isset($cacheConfig['parameters']['dir'])) { |
61
|
|
|
$dir = $cacheConfig['parameters']['dir']; |
62
|
|
|
} |
63
|
|
|
return $dir; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function load(array $config, ContainerBuilder $container) |
70
|
|
|
{ |
71
|
|
|
$cacheName = Utility::getAliasedName('metadata.cache'); |
72
|
|
|
$cacheConfig = $config['metadata']['cache']; |
73
|
|
|
if (false === $cacheConfig['enabled']) { |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Load cache warming services. |
78
|
|
|
$this->loadCacheWarming($container); |
79
|
|
|
|
80
|
|
|
if (isset($cacheConfig['service'])) { |
81
|
|
|
// Custom cache service. |
82
|
|
|
$container->setAlias($cacheName, Utility::cleanServiceName($cacheConfig['service'])); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Built-in cache service. |
87
|
|
|
switch ($cacheConfig['type']) { |
88
|
|
|
case 'file': |
89
|
|
|
$definition = $this->createFileCache('Metadata\Cache\FileCache', $cacheConfig, $container); |
90
|
|
|
break; |
91
|
|
|
case 'binary_file': |
92
|
|
|
$definition = $this->createFileCache('Metadata\Cache\BinaryFileCache', $cacheConfig, $container); |
93
|
|
|
break; |
94
|
|
|
case 'redis': |
95
|
|
|
$definition = $this->createRedisCache($cacheConfig); |
96
|
|
|
break; |
97
|
|
|
default: |
98
|
|
|
throw new \RuntimeException(sprintf('Unable to create a metadata cache service for type "%s"', $cacheConfig['type'])); |
99
|
|
|
} |
100
|
|
|
$container->setDefinition($cacheName, $definition); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Loads cache warming services. |
106
|
|
|
* |
107
|
|
|
* @param ContainerBuilder $container |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
|
|
private function loadCacheWarming(ContainerBuilder $container) |
111
|
|
|
{ |
112
|
|
|
$warmerName = Utility::getAliasedName('metadata.cache.warmer'); |
113
|
|
|
$definition = new Definition( |
114
|
|
|
Utility::getLibraryClass('Metadata\Cache\CacheWarmer'), |
115
|
|
|
[new Reference(Utility::getAliasedName('metadata.factory'))] |
116
|
|
|
); |
117
|
|
|
$definition->setPublic(false); |
118
|
|
|
$container->setDefinition($warmerName, $definition); |
119
|
|
|
|
120
|
|
|
$definition = new Definition( |
121
|
|
|
Utility::getBundleClass('CacheWarmer\MetadataWarmer'), |
122
|
|
|
[new Reference($warmerName)] |
123
|
|
|
); |
124
|
|
|
$definition->setPublic(false); |
125
|
|
|
$definition->addTag('kernel.cache_warmer'); |
126
|
|
|
$container->setDefinition(Utility::getAliasedName('bundle.cache.warmer'), $definition); |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|