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

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