|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
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 Symfony\Component\HttpKernel; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; |
|
15
|
|
|
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Loader\IniFileLoader; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Loader\ClosureLoader; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
27
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
28
|
|
|
use Symfony\Component\HttpKernel\Config\EnvParametersResource; |
|
29
|
|
|
use Symfony\Component\HttpKernel\Config\FileLocator; |
|
30
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; |
|
31
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass; |
|
32
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver; |
|
33
|
|
|
use Symfony\Component\Config\Loader\DelegatingLoader; |
|
34
|
|
|
use Symfony\Component\Config\ConfigCache; |
|
35
|
|
|
use Symfony\Component\ClassLoader\ClassCollectionLoader; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The Kernel is the heart of the Symfony system. |
|
39
|
|
|
* |
|
40
|
|
|
* It manages an environment made of bundles. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Fabien Potencier <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract class Kernel implements KernelInterface, TerminableInterface |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var BundleInterface[] |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $bundles = array(); |
|
50
|
|
|
|
|
51
|
|
|
protected $bundleMap; |
|
52
|
|
|
protected $container; |
|
53
|
|
|
protected $rootDir; |
|
54
|
|
|
protected $environment; |
|
55
|
|
|
protected $debug; |
|
56
|
|
|
protected $booted = false; |
|
57
|
|
|
protected $name; |
|
58
|
|
|
protected $startTime; |
|
59
|
|
|
protected $loadClassCache; |
|
60
|
|
|
|
|
61
|
|
|
const VERSION = '2.7.30-DEV'; |
|
62
|
|
|
const VERSION_ID = 20730; |
|
63
|
|
|
const MAJOR_VERSION = 2; |
|
64
|
|
|
const MINOR_VERSION = 7; |
|
65
|
|
|
const RELEASE_VERSION = 30; |
|
66
|
|
|
const EXTRA_VERSION = 'DEV'; |
|
67
|
|
|
|
|
68
|
|
|
const END_OF_MAINTENANCE = '05/2018'; |
|
69
|
|
|
const END_OF_LIFE = '05/2019'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Constructor. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $environment The environment |
|
75
|
|
|
* @param bool $debug Whether to enable debugging or not |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct($environment, $debug) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->environment = $environment; |
|
80
|
|
|
$this->debug = (bool) $debug; |
|
81
|
|
|
$this->rootDir = $this->getRootDir(); |
|
82
|
|
|
$this->name = $this->getName(); |
|
83
|
|
|
|
|
84
|
|
|
if ($this->debug) { |
|
85
|
|
|
$this->startTime = microtime(true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$defClass = new \ReflectionMethod($this, 'init'); |
|
89
|
|
|
$defClass = $defClass->getDeclaringClass()->name; |
|
90
|
|
|
|
|
91
|
|
|
if (__CLASS__ !== $defClass) { |
|
92
|
|
|
@trigger_error(sprintf('Calling the %s::init() method is deprecated since version 2.3 and will be removed in 3.0. Move your logic to the constructor method instead.', $defClass), E_USER_DEPRECATED); |
|
|
|
|
|
|
93
|
|
|
$this->init(); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @deprecated since version 2.3, to be removed in 3.0. Move your logic in the constructor instead. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function init() |
|
101
|
|
|
{ |
|
102
|
|
|
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0. Move your logic to the constructor method instead.', E_USER_DEPRECATED); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function __clone() |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->debug) { |
|
108
|
|
|
$this->startTime = microtime(true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->booted = false; |
|
112
|
|
|
$this->container = null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Boots the current kernel. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function boot() |
|
119
|
|
|
{ |
|
120
|
|
|
if (true === $this->booted) { |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this->loadClassCache) { |
|
125
|
|
|
$this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// init bundles |
|
129
|
|
|
$this->initializeBundles(); |
|
130
|
|
|
|
|
131
|
|
|
// init container |
|
132
|
|
|
$this->initializeContainer(); |
|
133
|
|
|
|
|
134
|
|
|
foreach ($this->getBundles() as $bundle) { |
|
135
|
|
|
$bundle->setContainer($this->container); |
|
136
|
|
|
$bundle->boot(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->booted = true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function terminate(Request $request, Response $response) |
|
146
|
|
|
{ |
|
147
|
|
|
if (false === $this->booted) { |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($this->getHttpKernel() instanceof TerminableInterface) { |
|
152
|
|
|
$this->getHttpKernel()->terminate($request, $response); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function shutdown() |
|
160
|
|
|
{ |
|
161
|
|
|
if (false === $this->booted) { |
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->booted = false; |
|
166
|
|
|
|
|
167
|
|
|
foreach ($this->getBundles() as $bundle) { |
|
168
|
|
|
$bundle->shutdown(); |
|
169
|
|
|
$bundle->setContainer(null); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$this->container = null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
|
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
|
179
|
|
|
{ |
|
180
|
|
|
if (false === $this->booted) { |
|
181
|
|
|
$this->boot(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $this->getHttpKernel()->handle($request, $type, $catch); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Gets a HTTP kernel from the container. |
|
189
|
|
|
* |
|
190
|
|
|
* @return HttpKernel |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function getHttpKernel() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->container->get('http_kernel'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getBundles() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->bundles; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* {@inheritdoc} |
|
207
|
|
|
* |
|
208
|
|
|
* @deprecated since version 2.6, to be removed in 3.0. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function isClassInActiveBundle($class) |
|
211
|
|
|
{ |
|
212
|
|
|
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in version 3.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
foreach ($this->getBundles() as $bundle) { |
|
215
|
|
|
if (0 === strpos($class, $bundle->getNamespace())) { |
|
216
|
|
|
return true; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return false; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getBundle($name, $first = true) |
|
227
|
|
|
{ |
|
228
|
|
|
if (!isset($this->bundleMap[$name])) { |
|
229
|
|
|
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your %s.php file?', $name, get_class($this))); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if (true === $first) { |
|
233
|
|
|
return $this->bundleMap[$name][0]; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $this->bundleMap[$name]; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritdoc} |
|
241
|
|
|
* |
|
242
|
|
|
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
|
243
|
|
|
*/ |
|
244
|
|
|
public function locateResource($name, $dir = null, $first = true) |
|
245
|
|
|
{ |
|
246
|
|
|
if ('@' !== $name[0]) { |
|
247
|
|
|
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
if (false !== strpos($name, '..')) { |
|
251
|
|
|
throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$bundleName = substr($name, 1); |
|
255
|
|
|
$path = ''; |
|
256
|
|
|
if (false !== strpos($bundleName, '/')) { |
|
257
|
|
|
list($bundleName, $path) = explode('/', $bundleName, 2); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$isResource = 0 === strpos($path, 'Resources') && null !== $dir; |
|
261
|
|
|
$overridePath = substr($path, 9); |
|
262
|
|
|
$resourceBundle = null; |
|
263
|
|
|
$bundles = $this->getBundle($bundleName, false); |
|
264
|
|
|
$files = array(); |
|
265
|
|
|
|
|
266
|
|
|
foreach ($bundles as $bundle) { |
|
267
|
|
|
if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) { |
|
268
|
|
|
if (null !== $resourceBundle) { |
|
269
|
|
|
throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived bundle. Create a "%s" file to override the bundle resource.', |
|
270
|
|
|
$file, |
|
271
|
|
|
$resourceBundle, |
|
272
|
|
|
$dir.'/'.$bundles[0]->getName().$overridePath |
|
273
|
|
|
)); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
if ($first) { |
|
277
|
|
|
return $file; |
|
278
|
|
|
} |
|
279
|
|
|
$files[] = $file; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
if (file_exists($file = $bundle->getPath().'/'.$path)) { |
|
283
|
|
|
if ($first && !$isResource) { |
|
284
|
|
|
return $file; |
|
285
|
|
|
} |
|
286
|
|
|
$files[] = $file; |
|
287
|
|
|
$resourceBundle = $bundle->getName(); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
if (count($files) > 0) { |
|
292
|
|
|
return $first && $isResource ? $files[0] : $files; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* {@inheritdoc} |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getName() |
|
302
|
|
|
{ |
|
303
|
|
|
if (null === $this->name) { |
|
304
|
|
|
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); |
|
305
|
|
|
if (ctype_digit($this->name[0])) { |
|
306
|
|
|
$this->name = '_'.$this->name; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return $this->name; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* {@inheritdoc} |
|
315
|
|
|
*/ |
|
316
|
|
|
public function getEnvironment() |
|
317
|
|
|
{ |
|
318
|
|
|
return $this->environment; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* {@inheritdoc} |
|
323
|
|
|
*/ |
|
324
|
|
|
public function isDebug() |
|
325
|
|
|
{ |
|
326
|
|
|
return $this->debug; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* {@inheritdoc} |
|
331
|
|
|
*/ |
|
332
|
|
View Code Duplication |
public function getRootDir() |
|
|
|
|
|
|
333
|
|
|
{ |
|
334
|
|
|
if (null === $this->rootDir) { |
|
335
|
|
|
$r = new \ReflectionObject($this); |
|
336
|
|
|
$this->rootDir = dirname($r->getFileName()); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
return $this->rootDir; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* {@inheritdoc} |
|
344
|
|
|
*/ |
|
345
|
|
|
public function getContainer() |
|
346
|
|
|
{ |
|
347
|
|
|
return $this->container; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* Loads the PHP class cache. |
|
352
|
|
|
* |
|
353
|
|
|
* This methods only registers the fact that you want to load the cache classes. |
|
354
|
|
|
* The cache will actually only be loaded when the Kernel is booted. |
|
355
|
|
|
* |
|
356
|
|
|
* That optimization is mainly useful when using the HttpCache class in which |
|
357
|
|
|
* case the class cache is not loaded if the Response is in the cache. |
|
358
|
|
|
* |
|
359
|
|
|
* @param string $name The cache name prefix |
|
360
|
|
|
* @param string $extension File extension of the resulting file |
|
361
|
|
|
*/ |
|
362
|
|
|
public function loadClassCache($name = 'classes', $extension = '.php') |
|
363
|
|
|
{ |
|
364
|
|
|
$this->loadClassCache = array($name, $extension); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Used internally. |
|
369
|
|
|
*/ |
|
370
|
|
|
public function setClassCache(array $classes) |
|
371
|
|
|
{ |
|
372
|
|
|
file_put_contents($this->getCacheDir().'/classes.map', sprintf('<?php return %s;', var_export($classes, true))); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* {@inheritdoc} |
|
377
|
|
|
*/ |
|
378
|
|
|
public function getStartTime() |
|
379
|
|
|
{ |
|
380
|
|
|
return $this->debug ? $this->startTime : -INF; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* {@inheritdoc} |
|
385
|
|
|
*/ |
|
386
|
|
|
public function getCacheDir() |
|
387
|
|
|
{ |
|
388
|
|
|
return $this->rootDir.'/cache/'.$this->environment; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* {@inheritdoc} |
|
393
|
|
|
*/ |
|
394
|
|
|
public function getLogDir() |
|
395
|
|
|
{ |
|
396
|
|
|
return $this->rootDir.'/logs'; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* {@inheritdoc} |
|
401
|
|
|
*/ |
|
402
|
|
|
public function getCharset() |
|
403
|
|
|
{ |
|
404
|
|
|
return 'UTF-8'; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
protected function doLoadClassCache($name, $extension) |
|
408
|
|
|
{ |
|
409
|
|
|
if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) { |
|
410
|
|
|
ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Initializes the data structures related to the bundle management. |
|
416
|
|
|
* |
|
417
|
|
|
* - the bundles property maps a bundle name to the bundle instance, |
|
418
|
|
|
* - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first). |
|
419
|
|
|
* |
|
420
|
|
|
* @throws \LogicException if two bundles share a common name |
|
421
|
|
|
* @throws \LogicException if a bundle tries to extend a non-registered bundle |
|
422
|
|
|
* @throws \LogicException if a bundle tries to extend itself |
|
423
|
|
|
* @throws \LogicException if two bundles extend the same ancestor |
|
424
|
|
|
*/ |
|
425
|
|
|
protected function initializeBundles() |
|
426
|
|
|
{ |
|
427
|
|
|
// init bundles |
|
428
|
|
|
$this->bundles = array(); |
|
429
|
|
|
$topMostBundles = array(); |
|
430
|
|
|
$directChildren = array(); |
|
431
|
|
|
|
|
432
|
|
|
foreach ($this->registerBundles() as $bundle) { |
|
433
|
|
|
$name = $bundle->getName(); |
|
434
|
|
|
if (isset($this->bundles[$name])) { |
|
435
|
|
|
throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name)); |
|
436
|
|
|
} |
|
437
|
|
|
$this->bundles[$name] = $bundle; |
|
438
|
|
|
|
|
439
|
|
|
if ($parentName = $bundle->getParent()) { |
|
440
|
|
|
if (isset($directChildren[$parentName])) { |
|
441
|
|
|
throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName])); |
|
442
|
|
|
} |
|
443
|
|
|
if ($parentName == $name) { |
|
444
|
|
|
throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name)); |
|
445
|
|
|
} |
|
446
|
|
|
$directChildren[$parentName] = $name; |
|
447
|
|
|
} else { |
|
448
|
|
|
$topMostBundles[$name] = $bundle; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
// look for orphans |
|
453
|
|
|
if (!empty($directChildren) && count($diff = array_diff_key($directChildren, $this->bundles))) { |
|
454
|
|
|
$diff = array_keys($diff); |
|
455
|
|
|
|
|
456
|
|
|
throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0])); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
// inheritance |
|
460
|
|
|
$this->bundleMap = array(); |
|
461
|
|
|
foreach ($topMostBundles as $name => $bundle) { |
|
462
|
|
|
$bundleMap = array($bundle); |
|
463
|
|
|
$hierarchy = array($name); |
|
464
|
|
|
|
|
465
|
|
|
while (isset($directChildren[$name])) { |
|
466
|
|
|
$name = $directChildren[$name]; |
|
467
|
|
|
array_unshift($bundleMap, $this->bundles[$name]); |
|
468
|
|
|
$hierarchy[] = $name; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
foreach ($hierarchy as $hierarchyBundle) { |
|
472
|
|
|
$this->bundleMap[$hierarchyBundle] = $bundleMap; |
|
473
|
|
|
array_pop($bundleMap); |
|
474
|
|
|
} |
|
475
|
|
|
} |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Gets the container class. |
|
480
|
|
|
* |
|
481
|
|
|
* @return string The container class |
|
482
|
|
|
*/ |
|
483
|
|
|
protected function getContainerClass() |
|
484
|
|
|
{ |
|
485
|
|
|
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Gets the container's base class. |
|
490
|
|
|
* |
|
491
|
|
|
* All names except Container must be fully qualified. |
|
492
|
|
|
* |
|
493
|
|
|
* @return string |
|
494
|
|
|
*/ |
|
495
|
|
|
protected function getContainerBaseClass() |
|
496
|
|
|
{ |
|
497
|
|
|
return 'Container'; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Initializes the service container. |
|
502
|
|
|
* |
|
503
|
|
|
* The cached version of the service container is used when fresh, otherwise the |
|
504
|
|
|
* container is built. |
|
505
|
|
|
*/ |
|
506
|
|
|
protected function initializeContainer() |
|
507
|
|
|
{ |
|
508
|
|
|
$class = $this->getContainerClass(); |
|
509
|
|
|
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug); |
|
510
|
|
|
$fresh = true; |
|
511
|
|
|
if (!$cache->isFresh()) { |
|
512
|
|
|
$container = $this->buildContainer(); |
|
513
|
|
|
$container->compile(); |
|
514
|
|
|
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass()); |
|
515
|
|
|
|
|
516
|
|
|
$fresh = false; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
require_once $cache->getPath(); |
|
520
|
|
|
|
|
521
|
|
|
$this->container = new $class(); |
|
522
|
|
|
$this->container->set('kernel', $this); |
|
523
|
|
|
|
|
524
|
|
|
if (!$fresh && $this->container->has('cache_warmer')) { |
|
525
|
|
|
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')); |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Returns the kernel parameters. |
|
531
|
|
|
* |
|
532
|
|
|
* @return array An array of kernel parameters |
|
533
|
|
|
*/ |
|
534
|
|
|
protected function getKernelParameters() |
|
535
|
|
|
{ |
|
536
|
|
|
$bundles = array(); |
|
537
|
|
|
$bundlesMetadata = array(); |
|
538
|
|
|
|
|
539
|
|
|
foreach ($this->bundles as $name => $bundle) { |
|
540
|
|
|
$bundles[$name] = get_class($bundle); |
|
541
|
|
|
$bundlesMetadata[$name] = array( |
|
542
|
|
|
'parent' => $bundle->getParent(), |
|
543
|
|
|
'path' => $bundle->getPath(), |
|
544
|
|
|
'namespace' => $bundle->getNamespace(), |
|
545
|
|
|
); |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
return array_merge( |
|
549
|
|
|
array( |
|
550
|
|
|
'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir, |
|
551
|
|
|
'kernel.environment' => $this->environment, |
|
552
|
|
|
'kernel.debug' => $this->debug, |
|
553
|
|
|
'kernel.name' => $this->name, |
|
554
|
|
|
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(), |
|
555
|
|
|
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(), |
|
556
|
|
|
'kernel.bundles' => $bundles, |
|
557
|
|
|
'kernel.bundles_metadata' => $bundlesMetadata, |
|
558
|
|
|
'kernel.charset' => $this->getCharset(), |
|
559
|
|
|
'kernel.container_class' => $this->getContainerClass(), |
|
560
|
|
|
), |
|
561
|
|
|
$this->getEnvParameters() |
|
562
|
|
|
); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* Gets the environment parameters. |
|
567
|
|
|
* |
|
568
|
|
|
* Only the parameters starting with "SYMFONY__" are considered. |
|
569
|
|
|
* |
|
570
|
|
|
* @return array An array of parameters |
|
571
|
|
|
*/ |
|
572
|
|
|
protected function getEnvParameters() |
|
573
|
|
|
{ |
|
574
|
|
|
$parameters = array(); |
|
575
|
|
|
foreach ($_SERVER as $key => $value) { |
|
576
|
|
|
if (0 === strpos($key, 'SYMFONY__')) { |
|
577
|
|
|
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value; |
|
578
|
|
|
} |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
return $parameters; |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Builds the service container. |
|
586
|
|
|
* |
|
587
|
|
|
* @return ContainerBuilder The compiled service container |
|
588
|
|
|
* |
|
589
|
|
|
* @throws \RuntimeException |
|
590
|
|
|
*/ |
|
591
|
|
|
protected function buildContainer() |
|
592
|
|
|
{ |
|
593
|
|
|
foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) { |
|
594
|
|
View Code Duplication |
if (!is_dir($dir)) { |
|
|
|
|
|
|
595
|
|
|
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) { |
|
596
|
|
|
throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir)); |
|
597
|
|
|
} |
|
598
|
|
|
} elseif (!is_writable($dir)) { |
|
599
|
|
|
throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir)); |
|
600
|
|
|
} |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
$container = $this->getContainerBuilder(); |
|
604
|
|
|
$container->addObjectResource($this); |
|
605
|
|
|
$this->prepareContainer($container); |
|
606
|
|
|
|
|
607
|
|
|
if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) { |
|
608
|
|
|
$container->merge($cont); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
$container->addCompilerPass(new AddClassesToCachePass($this)); |
|
612
|
|
|
$container->addResource(new EnvParametersResource('SYMFONY__')); |
|
613
|
|
|
|
|
614
|
|
|
return $container; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
/** |
|
618
|
|
|
* Prepares the ContainerBuilder before it is compiled. |
|
619
|
|
|
* |
|
620
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
|
621
|
|
|
*/ |
|
622
|
|
|
protected function prepareContainer(ContainerBuilder $container) |
|
623
|
|
|
{ |
|
624
|
|
|
$extensions = array(); |
|
625
|
|
|
foreach ($this->bundles as $bundle) { |
|
626
|
|
|
if ($extension = $bundle->getContainerExtension()) { |
|
627
|
|
|
$container->registerExtension($extension); |
|
628
|
|
|
$extensions[] = $extension->getAlias(); |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
if ($this->debug) { |
|
632
|
|
|
$container->addObjectResource($bundle); |
|
633
|
|
|
} |
|
634
|
|
|
} |
|
635
|
|
|
foreach ($this->bundles as $bundle) { |
|
636
|
|
|
$bundle->build($container); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
// ensure these extensions are implicitly loaded |
|
640
|
|
|
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions)); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
/** |
|
644
|
|
|
* Gets a new ContainerBuilder instance used to build the service container. |
|
645
|
|
|
* |
|
646
|
|
|
* @return ContainerBuilder |
|
647
|
|
|
*/ |
|
648
|
|
|
protected function getContainerBuilder() |
|
649
|
|
|
{ |
|
650
|
|
|
$container = new ContainerBuilder(new ParameterBag($this->getKernelParameters())); |
|
651
|
|
|
|
|
652
|
|
|
if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { |
|
653
|
|
|
$container->setProxyInstantiator(new RuntimeInstantiator()); |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
|
|
return $container; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
|
|
/** |
|
660
|
|
|
* Dumps the service container to PHP code in the cache. |
|
661
|
|
|
* |
|
662
|
|
|
* @param ConfigCache $cache The config cache |
|
663
|
|
|
* @param ContainerBuilder $container The service container |
|
664
|
|
|
* @param string $class The name of the class to generate |
|
665
|
|
|
* @param string $baseClass The name of the container's base class |
|
666
|
|
|
*/ |
|
667
|
|
|
protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass) |
|
668
|
|
|
{ |
|
669
|
|
|
// cache the container |
|
670
|
|
|
$dumper = new PhpDumper($container); |
|
671
|
|
|
|
|
672
|
|
|
if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) { |
|
673
|
|
|
$dumper->setProxyDumper(new ProxyDumper(md5($cache->getPath()))); |
|
674
|
|
|
} |
|
675
|
|
|
|
|
676
|
|
|
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->getPath(), 'debug' => $this->debug)); |
|
677
|
|
|
|
|
678
|
|
|
$cache->write($content, $container->getResources()); |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
/** |
|
682
|
|
|
* Returns a loader for the container. |
|
683
|
|
|
* |
|
684
|
|
|
* @param ContainerInterface $container The service container |
|
685
|
|
|
* |
|
686
|
|
|
* @return DelegatingLoader The loader |
|
687
|
|
|
*/ |
|
688
|
|
|
protected function getContainerLoader(ContainerInterface $container) |
|
689
|
|
|
{ |
|
690
|
|
|
$locator = new FileLocator($this); |
|
691
|
|
|
$resolver = new LoaderResolver(array( |
|
692
|
|
|
new XmlFileLoader($container, $locator), |
|
693
|
|
|
new YamlFileLoader($container, $locator), |
|
694
|
|
|
new IniFileLoader($container, $locator), |
|
695
|
|
|
new PhpFileLoader($container, $locator), |
|
696
|
|
|
new ClosureLoader($container), |
|
697
|
|
|
)); |
|
698
|
|
|
|
|
699
|
|
|
return new DelegatingLoader($resolver); |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
/** |
|
703
|
|
|
* Removes comments from a PHP source string. |
|
704
|
|
|
* |
|
705
|
|
|
* We don't use the PHP php_strip_whitespace() function |
|
706
|
|
|
* as we want the content to be readable and well-formatted. |
|
707
|
|
|
* |
|
708
|
|
|
* @param string $source A PHP string |
|
709
|
|
|
* |
|
710
|
|
|
* @return string The PHP string with the comments removed |
|
711
|
|
|
*/ |
|
712
|
|
|
public static function stripComments($source) |
|
713
|
|
|
{ |
|
714
|
|
|
if (!function_exists('token_get_all')) { |
|
715
|
|
|
return $source; |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
$rawChunk = ''; |
|
719
|
|
|
$output = ''; |
|
720
|
|
|
$tokens = token_get_all($source); |
|
721
|
|
|
$ignoreSpace = false; |
|
722
|
|
|
for ($i = 0; isset($tokens[$i]); ++$i) { |
|
723
|
|
|
$token = $tokens[$i]; |
|
724
|
|
|
if (!isset($token[1]) || 'b"' === $token) { |
|
725
|
|
|
$rawChunk .= $token; |
|
726
|
|
|
} elseif (T_START_HEREDOC === $token[0]) { |
|
727
|
|
|
$output .= $rawChunk.$token[1]; |
|
728
|
|
|
do { |
|
729
|
|
|
$token = $tokens[++$i]; |
|
730
|
|
|
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; |
|
731
|
|
|
} while ($token[0] !== T_END_HEREDOC); |
|
732
|
|
|
$rawChunk = ''; |
|
733
|
|
|
} elseif (T_WHITESPACE === $token[0]) { |
|
734
|
|
|
if ($ignoreSpace) { |
|
735
|
|
|
$ignoreSpace = false; |
|
736
|
|
|
|
|
737
|
|
|
continue; |
|
738
|
|
|
} |
|
739
|
|
|
|
|
740
|
|
|
// replace multiple new lines with a single newline |
|
741
|
|
|
$rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]); |
|
742
|
|
|
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { |
|
743
|
|
|
$ignoreSpace = true; |
|
744
|
|
|
} else { |
|
745
|
|
|
$rawChunk .= $token[1]; |
|
746
|
|
|
|
|
747
|
|
|
// The PHP-open tag already has a new-line |
|
748
|
|
|
if (T_OPEN_TAG === $token[0]) { |
|
749
|
|
|
$ignoreSpace = true; |
|
750
|
|
|
} |
|
751
|
|
|
} |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
|
|
$output .= $rawChunk; |
|
755
|
|
|
|
|
756
|
|
|
if (\PHP_VERSION_ID >= 70000) { |
|
757
|
|
|
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
758
|
|
|
unset($tokens, $rawChunk); |
|
759
|
|
|
gc_mem_caches(); |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
return $output; |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
public function serialize() |
|
766
|
|
|
{ |
|
767
|
|
|
return serialize(array($this->environment, $this->debug)); |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
|
|
public function unserialize($data) |
|
771
|
|
|
{ |
|
772
|
|
|
list($environment, $debug) = unserialize($data); |
|
773
|
|
|
|
|
774
|
|
|
$this->__construct($environment, $debug); |
|
775
|
|
|
} |
|
776
|
|
|
} |
|
777
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: