annotate vendor/symfony/http-kernel/Kernel.php @ 12:7a779792577d

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