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

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