vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169

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