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 the binary file cache service definition. |
19
|
|
|
* |
20
|
|
|
* @param array $cacheConfig |
21
|
|
|
* @param ContainerBuilder $container |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
private function createBinaryFileCache(array $cacheConfig, ContainerBuilder $container) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$cacheDir = $this->getFileCacheDir($cacheConfig, $container); |
27
|
|
|
Utility::appendParameter('dirs', 'metadata_cache_dir', $cacheDir, $container); |
28
|
|
|
|
29
|
|
|
return new Definition( |
30
|
|
|
Utility::getLibraryClass('Metadata\Cache\BinaryFileCache'), |
31
|
|
|
[$cacheDir] |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates the file cache service definition. |
37
|
|
|
* |
38
|
|
|
* @param array $cacheConfig |
39
|
|
|
* @param ContainerBuilder $container |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
private function createFileCache(array $cacheConfig, ContainerBuilder $container) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$cacheDir = $this->getFileCacheDir($cacheConfig, $container); |
45
|
|
|
Utility::appendParameter('dirs', 'metadata_cache_dir', $cacheDir, $container); |
46
|
|
|
|
47
|
|
|
return new Definition( |
48
|
|
|
Utility::getLibraryClass('Metadata\Cache\FileCache'), |
49
|
|
|
[$cacheDir] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates the redis cache service definition. |
55
|
|
|
* |
56
|
|
|
* @param array $cacheConfig |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
|
|
private function createRedisCache(array $cacheConfig, ContainerBuilder $container) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return new Definition( |
63
|
|
|
Utility::getLibraryClass('Metadata\Cache\RedisCache'), |
64
|
|
|
[new Reference($cacheConfig['parameters']['handler'])] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the file cache directory. |
70
|
|
|
* |
71
|
|
|
* @param array $cacheConfig |
72
|
|
|
* @param ContainerBuilder $container |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
private function getFileCacheDir(array $cacheConfig, ContainerBuilder $container) |
76
|
|
|
{ |
77
|
|
|
$dir = sprintf('%s/as3_modlr', $container->getParameter('kernel.cache_dir')); |
78
|
|
|
if (isset($cacheConfig['parameters']['dir'])) { |
79
|
|
|
$dir = $cacheConfig['parameters']['dir']; |
80
|
|
|
} |
81
|
|
|
return $dir; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function load(array $config, ContainerBuilder $container) |
88
|
|
|
{ |
89
|
|
|
$cacheName = Utility::getAliasedName('metadata.cache'); |
90
|
|
|
$cacheConfig = $config['metadata']['cache']; |
91
|
|
|
if (false === $cacheConfig['enabled']) { |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Load cache warming services. |
96
|
|
|
$this->loadCacheWarming($container); |
97
|
|
|
|
98
|
|
|
if (isset($cacheConfig['service'])) { |
99
|
|
|
// Custom cache service. |
100
|
|
|
$container->setAlias($cacheName, Utility::cleanServiceName($cacheConfig['service'])); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Built-in cache service. |
105
|
|
|
switch ($cacheConfig['type']) { |
106
|
|
|
case 'file': |
107
|
|
|
$definition = $this->createFileCache($cacheConfig, $container); |
108
|
|
|
break; |
109
|
|
|
case 'binary_file': |
110
|
|
|
$definition = $this->createBinaryFileCache($cacheConfig, $container); |
111
|
|
|
break; |
112
|
|
|
case 'redis': |
113
|
|
|
$definition = $this->createRedisCache($cacheConfig, $container); |
114
|
|
|
break; |
115
|
|
|
default: |
116
|
|
|
throw new \RuntimeException(sprintf('Unable to create a metadata cache service for type "%s"', $cacheConfig['type'])); |
117
|
|
|
} |
118
|
|
|
$container->setDefinition($cacheName, $definition); |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Loads cache warming services. |
124
|
|
|
* |
125
|
|
|
* @param ContainerBuilder $container |
126
|
|
|
* @return self |
127
|
|
|
*/ |
128
|
|
|
private function loadCacheWarming(ContainerBuilder $container) |
129
|
|
|
{ |
130
|
|
|
$warmerName = Utility::getAliasedName('metadata.cache.warmer'); |
131
|
|
|
$definition = new Definition( |
132
|
|
|
Utility::getLibraryClass('Metadata\Cache\CacheWarmer'), |
133
|
|
|
[new Reference(Utility::getAliasedName('metadata.factory'))] |
134
|
|
|
); |
135
|
|
|
$definition->setPublic(false); |
136
|
|
|
$container->setDefinition($warmerName, $definition); |
137
|
|
|
|
138
|
|
|
$definition = new Definition( |
139
|
|
|
Utility::getBundleClass('CacheWarmer\MetadataWarmer'), |
140
|
|
|
[new Reference($warmerName)] |
141
|
|
|
); |
142
|
|
|
$definition->setPublic(false); |
143
|
|
|
$definition->addTag('kernel.cache_warmer'); |
144
|
|
|
$container->setDefinition(Utility::getAliasedName('bundle.cache.warmer'), $definition); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.