|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015-2016 Yahoo Japan Corporation |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace YahooJapan\ConfigCacheBundle\ConfigCache; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
17
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Register\RegisterFactory; |
|
18
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Register\ServiceIdBuilder; |
|
19
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\DirectoryResource; |
|
20
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\FileResource; |
|
21
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\ResourceInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register registers a cache service by some bundles. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Register |
|
27
|
|
|
{ |
|
28
|
|
|
protected $extension; |
|
29
|
|
|
protected $configuration; |
|
30
|
|
|
protected $container; |
|
31
|
|
|
protected $resources; |
|
32
|
|
|
protected $file; |
|
33
|
|
|
protected $directory; |
|
34
|
|
|
protected $idBuilder; |
|
35
|
|
|
protected $serviceRegister; |
|
36
|
|
|
protected $factory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ExtensionInterface $extension An Extension |
|
42
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
|
43
|
|
|
* @param array $resources Array of Resource key and Configuration instance value |
|
44
|
|
|
* @param array $excludes Exclude file names |
|
45
|
|
|
*/ |
|
46
|
13 |
|
public function __construct( |
|
47
|
|
|
ExtensionInterface $extension, |
|
48
|
|
|
ContainerBuilder $container, |
|
49
|
|
|
array $resources, |
|
50
|
|
|
array $excludes = array() |
|
51
|
|
|
) { |
|
52
|
11 |
|
$this->extension = $extension; |
|
53
|
11 |
|
$this->container = $container; |
|
54
|
11 |
|
$this->resources = $resources; |
|
55
|
|
|
|
|
56
|
13 |
|
$this->initialize($excludes); |
|
57
|
11 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Registers a service by a bundle. |
|
61
|
|
|
*/ |
|
62
|
18 |
|
public function register() |
|
63
|
|
|
{ |
|
64
|
18 |
|
$this |
|
65
|
18 |
|
->initializeResources() |
|
66
|
18 |
|
->registerInternal() |
|
67
|
|
|
; |
|
68
|
17 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Registers a service by all bundles. |
|
72
|
|
|
*/ |
|
73
|
13 |
|
public function registerAll() |
|
74
|
|
|
{ |
|
75
|
13 |
|
$this |
|
76
|
13 |
|
->initializeAllResources($this->container->getParameter('kernel.bundles')) |
|
77
|
13 |
|
->registerInternal() |
|
78
|
|
|
; |
|
79
|
13 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Sets a master configuration. |
|
83
|
|
|
* |
|
84
|
|
|
* @param ConfigurationInterface $configuration |
|
85
|
|
|
* |
|
86
|
|
|
* @return Register |
|
87
|
|
|
*/ |
|
88
|
25 |
|
public function setConfiguration(ConfigurationInterface $configuration) |
|
89
|
|
|
{ |
|
90
|
25 |
|
$this->configuration->setConfiguration($configuration); |
|
91
|
|
|
|
|
92
|
25 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sets an application config. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $appConfig |
|
99
|
|
|
* |
|
100
|
|
|
* @return Register |
|
101
|
|
|
*/ |
|
102
|
9 |
|
public function setAppConfig(array $appConfig) |
|
103
|
|
|
{ |
|
104
|
9 |
|
$this->serviceRegister->setAppConfig($appConfig); |
|
105
|
|
|
|
|
106
|
9 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Sets a config_cache service tag. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $tag |
|
113
|
|
|
* |
|
114
|
|
|
* @return Register |
|
115
|
|
|
*/ |
|
116
|
7 |
|
public function setTag($tag) |
|
117
|
|
|
{ |
|
118
|
7 |
|
$this->serviceRegister->setTag($tag); |
|
119
|
|
|
|
|
120
|
7 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Initializes. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $excludes |
|
127
|
|
|
*/ |
|
128
|
11 |
|
protected function initialize(array $excludes = array()) |
|
129
|
|
|
{ |
|
130
|
11 |
|
$this->factory = $this->createRegisterFactory(); |
|
131
|
11 |
|
$this->idBuilder = $this->factory->createIdBuilder(); |
|
132
|
11 |
|
$this->configuration = $this->factory->createConfigurationRegister(); |
|
133
|
11 |
|
$this->serviceRegister = $this->factory->setContainer($this->container)->createServiceRegister(); |
|
134
|
11 |
|
$this->file = $this->factory->createFileRegister(); |
|
135
|
11 |
|
$this->directory = $this->factory->createDirectoryRegister()->setExcludes($excludes); |
|
136
|
|
|
|
|
137
|
|
|
// set bundleId, configuration based on extension |
|
138
|
11 |
|
$this->setBundleId(); |
|
139
|
11 |
|
$this->setConfigurationByExtension(); |
|
140
|
|
|
|
|
141
|
|
|
// validate set data on constructor |
|
142
|
11 |
|
$this->validateResources(); |
|
143
|
11 |
|
$this->validateCacheId(); |
|
144
|
11 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Register services internal processing. |
|
148
|
|
|
*/ |
|
149
|
19 |
|
protected function registerInternal() |
|
150
|
|
|
{ |
|
151
|
19 |
|
$this->file->register(); |
|
152
|
18 |
|
$this->directory->register(); |
|
153
|
18 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Initializes resources by a bundle. |
|
157
|
|
|
* |
|
158
|
|
|
* @return Register |
|
159
|
|
|
*/ |
|
160
|
24 |
|
protected function initializeResources() |
|
161
|
|
|
{ |
|
162
|
24 |
|
foreach ($this->resources as $resource) { |
|
163
|
23 |
|
if ($this->file->enabled($resource)) { |
|
164
|
19 |
|
$this->file->add($resource); |
|
165
|
23 |
|
} elseif ($this->directory->enabled($resource)) { |
|
166
|
3 |
|
$this->directory->add($resource); |
|
167
|
3 |
|
} |
|
168
|
24 |
|
} |
|
169
|
|
|
|
|
170
|
24 |
|
$this->postInitializeResources(); |
|
171
|
|
|
|
|
172
|
24 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Initializes resources by all bundles. |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $bundles |
|
179
|
|
|
* |
|
180
|
|
|
* @return Register |
|
181
|
|
|
*/ |
|
182
|
19 |
|
protected function initializeAllResources(array $bundles) |
|
183
|
|
|
{ |
|
184
|
|
|
// extract resources without FileResource with alias |
|
185
|
19 |
|
$resources = array(); |
|
186
|
19 |
|
foreach ($this->resources as $resource) { |
|
187
|
17 |
|
if ($this->file->hasAlias($resource)) { |
|
188
|
1 |
|
$this->file->add($resource); |
|
189
|
1 |
|
} else { |
|
190
|
16 |
|
$resources[] = $resource; |
|
191
|
|
|
} |
|
192
|
19 |
|
} |
|
193
|
|
|
|
|
194
|
19 |
|
foreach ($bundles as $fqcn) { |
|
195
|
19 |
|
$reflection = new \ReflectionClass($fqcn); |
|
196
|
19 |
|
$this->extractAllResources($resources, $reflection->getFilename()); |
|
197
|
19 |
|
} |
|
198
|
|
|
|
|
199
|
19 |
|
$this->postInitializeResources(); |
|
200
|
|
|
|
|
201
|
19 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Extracts all resources to FileRegister, DirectoryRegister. |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $resources |
|
208
|
|
|
* @param string $classPath |
|
209
|
|
|
*/ |
|
210
|
19 |
|
protected function extractAllResources(array $resources, $classPath) |
|
211
|
|
|
{ |
|
212
|
19 |
|
foreach ($resources as $resource) { |
|
213
|
16 |
|
$path = dirname($classPath).$resource->getResource(); |
|
214
|
16 |
|
if (is_dir($path)) { |
|
215
|
1 |
|
$this->directory->add(new DirectoryResource($path, $this->configuration->find($resource))); |
|
216
|
16 |
|
} elseif (file_exists($path)) { |
|
217
|
13 |
|
$this->file->add(new FileResource($path, $this->configuration->find($resource))); |
|
218
|
13 |
|
} |
|
219
|
19 |
|
} |
|
220
|
19 |
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Initializes resources postprocessing. |
|
224
|
|
|
*/ |
|
225
|
36 |
|
protected function postInitializeResources() |
|
226
|
|
|
{ |
|
227
|
36 |
|
if ($this->file->hasNoAlias() || $this->directory->has()) { |
|
228
|
23 |
|
$this->serviceRegister->registerConfigCache(); |
|
229
|
23 |
|
} |
|
230
|
36 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Sets bundle ID. |
|
234
|
|
|
* |
|
235
|
|
|
* @return Register |
|
236
|
|
|
*/ |
|
237
|
11 |
|
protected function setBundleId() |
|
238
|
|
|
{ |
|
239
|
11 |
|
$this->idBuilder->setBundleId($this->extension->getAlias()); |
|
240
|
|
|
|
|
241
|
11 |
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Sets a master configuration by extension. |
|
246
|
|
|
*/ |
|
247
|
12 |
|
protected function setConfigurationByExtension() |
|
248
|
|
|
{ |
|
249
|
12 |
|
$configuration = $this->extension->getConfiguration(array(), $this->container); |
|
250
|
12 |
|
if ($configuration instanceof ConfigurationInterface) { |
|
251
|
11 |
|
$this->setConfiguration($configuration); |
|
252
|
11 |
|
} |
|
253
|
|
|
|
|
254
|
12 |
|
return $this; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Validates resources. |
|
259
|
|
|
* |
|
260
|
|
|
* @throws \Exception Throws if the resource is not ResourceInterface. |
|
261
|
|
|
*/ |
|
262
|
13 |
|
protected function validateResources() |
|
263
|
|
|
{ |
|
264
|
13 |
|
if ($this->resources === array()) { |
|
265
|
1 |
|
throw new \Exception("Resources must be required more than one."); |
|
266
|
|
|
} |
|
267
|
12 |
|
foreach ($this->resources as $resource) { |
|
268
|
12 |
|
if (!($resource instanceof ResourceInterface)) { |
|
269
|
1 |
|
throw new \Exception("Resources are not instance of ResourceInterface."); |
|
270
|
|
|
} |
|
271
|
12 |
|
} |
|
272
|
11 |
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Validates a cache ID. |
|
276
|
|
|
* |
|
277
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
278
|
|
|
* |
|
279
|
|
|
* @throws \Exception Throws if the cacheId and a bundle name are duplicated. |
|
280
|
|
|
*/ |
|
281
|
12 |
|
protected function validateCacheId() |
|
282
|
|
|
{ |
|
283
|
12 |
|
foreach ($this->container->getParameter('kernel.bundles') as $className => $fqcn) { |
|
284
|
12 |
|
$id = ServiceIdBuilder::parseServiceId($className); |
|
285
|
12 |
|
$serviceIdPrefix = $this->idBuilder->getPrefix(); |
|
286
|
12 |
|
if ($serviceIdPrefix === $id) { |
|
287
|
1 |
|
throw new \Exception( |
|
288
|
1 |
|
"Cache ID[{$serviceIdPrefix}] and Service ID[{$id}] ". |
|
289
|
1 |
|
"based Bundle name[{$className}] are duplicated" |
|
290
|
1 |
|
); |
|
291
|
|
|
} |
|
292
|
12 |
|
} |
|
293
|
11 |
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Creates a RegisterFactory. |
|
297
|
|
|
* |
|
298
|
|
|
* @return RegisterFactory |
|
299
|
|
|
*/ |
|
300
|
11 |
|
protected function createRegisterFactory() |
|
301
|
|
|
{ |
|
302
|
11 |
|
return new RegisterFactory(); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|