4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel;
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\Loader\XmlFileLoader;
20 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
22 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
23 use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
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;
38 * The Kernel is the heart of the Symfony system.
40 * It manages an environment made of bundles.
42 * @author Fabien Potencier <fabien@symfony.com>
44 abstract class Kernel implements KernelInterface, TerminableInterface
47 * @var BundleInterface[]
49 protected $bundles = array();
54 protected $environment;
56 protected $booted = false;
59 protected $loadClassCache;
61 const VERSION = '3.2.13';
62 const VERSION_ID = 30213;
63 const MAJOR_VERSION = 3;
64 const MINOR_VERSION = 2;
65 const RELEASE_VERSION = 13;
66 const EXTRA_VERSION = '';
68 const END_OF_MAINTENANCE = '07/2017';
69 const END_OF_LIFE = '01/2018';
74 * @param string $environment The environment
75 * @param bool $debug Whether to enable debugging or not
77 public function __construct($environment, $debug)
79 $this->environment = $environment;
80 $this->debug = (bool) $debug;
81 $this->rootDir = $this->getRootDir();
82 $this->name = $this->getName();
85 $this->startTime = microtime(true);
89 public function __clone()
92 $this->startTime = microtime(true);
95 $this->booted = false;
96 $this->container = null;
100 * Boots the current kernel.
102 public function boot()
104 if (true === $this->booted) {
108 if ($this->loadClassCache) {
109 $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
113 $this->initializeBundles();
116 $this->initializeContainer();
118 foreach ($this->getBundles() as $bundle) {
119 $bundle->setContainer($this->container);
123 $this->booted = true;
129 public function terminate(Request $request, Response $response)
131 if (false === $this->booted) {
135 if ($this->getHttpKernel() instanceof TerminableInterface) {
136 $this->getHttpKernel()->terminate($request, $response);
143 public function shutdown()
145 if (false === $this->booted) {
149 $this->booted = false;
151 foreach ($this->getBundles() as $bundle) {
153 $bundle->setContainer(null);
156 $this->container = null;
162 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
164 if (false === $this->booted) {
168 return $this->getHttpKernel()->handle($request, $type, $catch);
172 * Gets a HTTP kernel from the container.
176 protected function getHttpKernel()
178 return $this->container->get('http_kernel');
184 public function getBundles()
186 return $this->bundles;
192 public function getBundle($name, $first = true)
194 if (!isset($this->bundleMap[$name])) {
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)));
198 if (true === $first) {
199 return $this->bundleMap[$name][0];
202 return $this->bundleMap[$name];
208 * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
210 public function locateResource($name, $dir = null, $first = true)
212 if ('@' !== $name[0]) {
213 throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
216 if (false !== strpos($name, '..')) {
217 throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
220 $bundleName = substr($name, 1);
222 if (false !== strpos($bundleName, '/')) {
223 list($bundleName, $path) = explode('/', $bundleName, 2);
226 $isResource = 0 === strpos($path, 'Resources') && null !== $dir;
227 $overridePath = substr($path, 9);
228 $resourceBundle = null;
229 $bundles = $this->getBundle($bundleName, false);
232 foreach ($bundles as $bundle) {
233 if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
234 if (null !== $resourceBundle) {
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.',
238 $dir.'/'.$bundles[0]->getName().$overridePath
248 if (file_exists($file = $bundle->getPath().'/'.$path)) {
249 if ($first && !$isResource) {
253 $resourceBundle = $bundle->getName();
257 if (count($files) > 0) {
258 return $first && $isResource ? $files[0] : $files;
261 throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
267 public function getName()
269 if (null === $this->name) {
270 $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
271 if (ctype_digit($this->name[0])) {
272 $this->name = '_'.$this->name;
282 public function getEnvironment()
284 return $this->environment;
290 public function isDebug()
298 public function getRootDir()
300 if (null === $this->rootDir) {
301 $r = new \ReflectionObject($this);
302 $this->rootDir = dirname($r->getFileName());
305 return $this->rootDir;
311 public function getContainer()
313 return $this->container;
317 * Loads the PHP class cache.
319 * This methods only registers the fact that you want to load the cache classes.
320 * The cache will actually only be loaded when the Kernel is booted.
322 * That optimization is mainly useful when using the HttpCache class in which
323 * case the class cache is not loaded if the Response is in the cache.
325 * @param string $name The cache name prefix
326 * @param string $extension File extension of the resulting file
328 public function loadClassCache($name = 'classes', $extension = '.php')
330 $this->loadClassCache = array($name, $extension);
336 public function setClassCache(array $classes)
338 file_put_contents($this->getCacheDir().'/classes.map', sprintf('<?php return %s;', var_export($classes, true)));
344 public function setAnnotatedClassCache(array $annotatedClasses)
346 file_put_contents($this->getCacheDir().'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true)));
352 public function getStartTime()
354 return $this->debug ? $this->startTime : -INF;
360 public function getCacheDir()
362 return $this->rootDir.'/cache/'.$this->environment;
368 public function getLogDir()
370 return $this->rootDir.'/logs';
376 public function getCharset()
381 protected function doLoadClassCache($name, $extension)
383 if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) {
384 ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension);
389 * Initializes the data structures related to the bundle management.
391 * - the bundles property maps a bundle name to the bundle instance,
392 * - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first).
394 * @throws \LogicException if two bundles share a common name
395 * @throws \LogicException if a bundle tries to extend a non-registered bundle
396 * @throws \LogicException if a bundle tries to extend itself
397 * @throws \LogicException if two bundles extend the same ancestor
399 protected function initializeBundles()
402 $this->bundles = array();
403 $topMostBundles = array();
404 $directChildren = array();
406 foreach ($this->registerBundles() as $bundle) {
407 $name = $bundle->getName();
408 if (isset($this->bundles[$name])) {
409 throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name));
411 $this->bundles[$name] = $bundle;
413 if ($parentName = $bundle->getParent()) {
414 if (isset($directChildren[$parentName])) {
415 throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName]));
417 if ($parentName == $name) {
418 throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name));
420 $directChildren[$parentName] = $name;
422 $topMostBundles[$name] = $bundle;
427 if (!empty($directChildren) && count($diff = array_diff_key($directChildren, $this->bundles))) {
428 $diff = array_keys($diff);
430 throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0]));
434 $this->bundleMap = array();
435 foreach ($topMostBundles as $name => $bundle) {
436 $bundleMap = array($bundle);
437 $hierarchy = array($name);
439 while (isset($directChildren[$name])) {
440 $name = $directChildren[$name];
441 array_unshift($bundleMap, $this->bundles[$name]);
442 $hierarchy[] = $name;
445 foreach ($hierarchy as $hierarchyBundle) {
446 $this->bundleMap[$hierarchyBundle] = $bundleMap;
447 array_pop($bundleMap);
453 * Gets the container class.
455 * @return string The container class
457 protected function getContainerClass()
459 return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
463 * Gets the container's base class.
465 * All names except Container must be fully qualified.
469 protected function getContainerBaseClass()
475 * Initializes the service container.
477 * The cached version of the service container is used when fresh, otherwise the
478 * container is built.
480 protected function initializeContainer()
482 $class = $this->getContainerClass();
483 $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
485 if (!$cache->isFresh()) {
486 $container = $this->buildContainer();
487 $container->compile();
488 $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
493 require_once $cache->getPath();
495 $this->container = new $class();
496 $this->container->set('kernel', $this);
498 if (!$fresh && $this->container->has('cache_warmer')) {
499 $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
504 * Returns the kernel parameters.
506 * @return array An array of kernel parameters
508 protected function getKernelParameters()
511 $bundlesMetadata = array();
513 foreach ($this->bundles as $name => $bundle) {
514 $bundles[$name] = get_class($bundle);
515 $bundlesMetadata[$name] = array(
516 'parent' => $bundle->getParent(),
517 'path' => $bundle->getPath(),
518 'namespace' => $bundle->getNamespace(),
524 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
525 'kernel.environment' => $this->environment,
526 'kernel.debug' => $this->debug,
527 'kernel.name' => $this->name,
528 'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
529 'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
530 'kernel.bundles' => $bundles,
531 'kernel.bundles_metadata' => $bundlesMetadata,
532 'kernel.charset' => $this->getCharset(),
533 'kernel.container_class' => $this->getContainerClass(),
535 $this->getEnvParameters()
540 * Gets the environment parameters.
542 * Only the parameters starting with "SYMFONY__" are considered.
544 * @return array An array of parameters
546 protected function getEnvParameters()
548 $parameters = array();
549 foreach ($_SERVER as $key => $value) {
550 if (0 === strpos($key, 'SYMFONY__')) {
551 $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
559 * Builds the service container.
561 * @return ContainerBuilder The compiled service container
563 * @throws \RuntimeException
565 protected function buildContainer()
567 foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) {
569 if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
570 throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir));
572 } elseif (!is_writable($dir)) {
573 throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir));
577 $container = $this->getContainerBuilder();
578 $container->addObjectResource($this);
579 $this->prepareContainer($container);
581 if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
582 $container->merge($cont);
585 $container->addCompilerPass(new AddClassesToCachePass($this));
586 $container->addResource(new EnvParametersResource('SYMFONY__'));
592 * Prepares the ContainerBuilder before it is compiled.
594 * @param ContainerBuilder $container A ContainerBuilder instance
596 protected function prepareContainer(ContainerBuilder $container)
598 $extensions = array();
599 foreach ($this->bundles as $bundle) {
600 if ($extension = $bundle->getContainerExtension()) {
601 $container->registerExtension($extension);
602 $extensions[] = $extension->getAlias();
606 $container->addObjectResource($bundle);
609 foreach ($this->bundles as $bundle) {
610 $bundle->build($container);
613 // ensure these extensions are implicitly loaded
614 $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
618 * Gets a new ContainerBuilder instance used to build the service container.
620 * @return ContainerBuilder
622 protected function getContainerBuilder()
624 $container = new ContainerBuilder();
625 $container->getParameterBag()->add($this->getKernelParameters());
627 if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) {
628 $container->setProxyInstantiator(new RuntimeInstantiator());
635 * Dumps the service container to PHP code in the cache.
637 * @param ConfigCache $cache The config cache
638 * @param ContainerBuilder $container The service container
639 * @param string $class The name of the class to generate
640 * @param string $baseClass The name of the container's base class
642 protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass)
644 // cache the container
645 $dumper = new PhpDumper($container);
647 if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) {
648 $dumper->setProxyDumper(new ProxyDumper(md5($cache->getPath())));
651 $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->getPath(), 'debug' => $this->debug));
653 $cache->write($content, $container->getResources());
657 * Returns a loader for the container.
659 * @param ContainerInterface $container The service container
661 * @return DelegatingLoader The loader
663 protected function getContainerLoader(ContainerInterface $container)
665 $locator = new FileLocator($this);
666 $resolver = new LoaderResolver(array(
667 new XmlFileLoader($container, $locator),
668 new YamlFileLoader($container, $locator),
669 new IniFileLoader($container, $locator),
670 new PhpFileLoader($container, $locator),
671 new DirectoryLoader($container, $locator),
672 new ClosureLoader($container),
675 return new DelegatingLoader($resolver);
679 * Removes comments from a PHP source string.
681 * We don't use the PHP php_strip_whitespace() function
682 * as we want the content to be readable and well-formatted.
684 * @param string $source A PHP string
686 * @return string The PHP string with the comments removed
688 public static function stripComments($source)
690 if (!function_exists('token_get_all')) {
696 $tokens = token_get_all($source);
697 $ignoreSpace = false;
698 for ($i = 0; isset($tokens[$i]); ++$i) {
699 $token = $tokens[$i];
700 if (!isset($token[1]) || 'b"' === $token) {
702 } elseif (T_START_HEREDOC === $token[0]) {
703 $output .= $rawChunk.$token[1];
705 $token = $tokens[++$i];
706 $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
707 } while ($token[0] !== T_END_HEREDOC);
709 } elseif (T_WHITESPACE === $token[0]) {
711 $ignoreSpace = false;
716 // replace multiple new lines with a single newline
717 $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]);
718 } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
721 $rawChunk .= $token[1];
723 // The PHP-open tag already has a new-line
724 if (T_OPEN_TAG === $token[0]) {
730 $output .= $rawChunk;
732 if (\PHP_VERSION_ID >= 70000) {
733 // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
734 unset($tokens, $rawChunk);
741 public function serialize()
743 return serialize(array($this->environment, $this->debug));
746 public function unserialize($data)
748 list($environment, $debug) = unserialize($data);
750 $this->__construct($environment, $debug);