vendor/symfony/http-kernel/Kernel.php line 200

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