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.9';
  60.     const VERSION_ID 40009;
  61.     const MAJOR_VERSION 4;
  62.     const MINOR_VERSION 0;
  63.     const RELEASE_VERSION 9;
  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.             static $legacyContainers = array();
  474.             $oldContainerDir dirname($oldContainer->getFileName());
  475.             $legacyContainers[$oldContainerDir.'.legacy'] = true;
  476.             foreach (glob(dirname($oldContainerDir).DIRECTORY_SEPARATOR.'*.legacy') as $legacyContainer) {
  477.                 if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) {
  478.                     (new Filesystem())->remove(substr($legacyContainer0, -7));
  479.                 }
  480.             }
  481.             touch($oldContainerDir.'.legacy');
  482.         }
  483.         if ($this->container->has('cache_warmer')) {
  484.             $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
  485.         }
  486.     }
  487.     /**
  488.      * Returns the kernel parameters.
  489.      *
  490.      * @return array An array of kernel parameters
  491.      */
  492.     protected function getKernelParameters()
  493.     {
  494.         $bundles = array();
  495.         $bundlesMetadata = array();
  496.         foreach ($this->bundles as $name => $bundle) {
  497.             $bundles[$name] = get_class($bundle);
  498.             $bundlesMetadata[$name] = array(
  499.                 'path' => $bundle->getPath(),
  500.                 'namespace' => $bundle->getNamespace(),
  501.             );
  502.         }
  503.         return array(
  504.             'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
  505.             'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
  506.             'kernel.environment' => $this->environment,
  507.             'kernel.debug' => $this->debug,
  508.             'kernel.name' => $this->name,
  509.             'kernel.cache_dir' => realpath($cacheDir $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir,
  510.             'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
  511.             'kernel.bundles' => $bundles,
  512.             'kernel.bundles_metadata' => $bundlesMetadata,
  513.             'kernel.charset' => $this->getCharset(),
  514.             'kernel.container_class' => $this->getContainerClass(),
  515.         );
  516.     }
  517.     /**
  518.      * Builds the service container.
  519.      *
  520.      * @return ContainerBuilder The compiled service container
  521.      *
  522.      * @throws \RuntimeException
  523.      */
  524.     protected function buildContainer()
  525.     {
  526.         foreach (array('cache' => $this->warmupDir ?: $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) {
  527.             if (!is_dir($dir)) {
  528.                 if (false === @mkdir($dir0777true) && !is_dir($dir)) {
  529.                     throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n"$name$dir));
  530.                 }
  531.             } elseif (!is_writable($dir)) {
  532.                 throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n"$name$dir));
  533.             }
  534.         }
  535.         $container $this->getContainerBuilder();
  536.         $container->addObjectResource($this);
  537.         $this->prepareContainer($container);
  538.         if (null !== $cont $this->registerContainerConfiguration($this->getContainerLoader($container))) {
  539.             $container->merge($cont);
  540.         }
  541.         $container->addCompilerPass(new AddAnnotatedClassesToCachePass($this));
  542.         return $container;
  543.     }
  544.     /**
  545.      * Prepares the ContainerBuilder before it is compiled.
  546.      */
  547.     protected function prepareContainer(ContainerBuilder $container)
  548.     {
  549.         $extensions = array();
  550.         foreach ($this->bundles as $bundle) {
  551.             if ($extension $bundle->getContainerExtension()) {
  552.                 $container->registerExtension($extension);
  553.             }
  554.             if ($this->debug) {
  555.                 $container->addObjectResource($bundle);
  556.             }
  557.         }
  558.         foreach ($this->bundles as $bundle) {
  559.             $bundle->build($container);
  560.         }
  561.         $this->build($container);
  562.         foreach ($container->getExtensions() as $extension) {
  563.             $extensions[] = $extension->getAlias();
  564.         }
  565.         // ensure these extensions are implicitly loaded
  566.         $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
  567.     }
  568.     /**
  569.      * Gets a new ContainerBuilder instance used to build the service container.
  570.      *
  571.      * @return ContainerBuilder
  572.      */
  573.     protected function getContainerBuilder()
  574.     {
  575.         $container = new ContainerBuilder();
  576.         $container->getParameterBag()->add($this->getKernelParameters());
  577.         if ($this instanceof CompilerPassInterface) {
  578.             $container->addCompilerPass($thisPassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
  579.         }
  580.         if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) {
  581.             $container->setProxyInstantiator(new RuntimeInstantiator());
  582.         }
  583.         return $container;
  584.     }
  585.     /**
  586.      * Dumps the service container to PHP code in the cache.
  587.      *
  588.      * @param ConfigCache      $cache     The config cache
  589.      * @param ContainerBuilder $container The service container
  590.      * @param string           $class     The name of the class to generate
  591.      * @param string           $baseClass The name of the container's base class
  592.      */
  593.     protected function dumpContainer(ConfigCache $cacheContainerBuilder $container$class$baseClass)
  594.     {
  595.         // cache the container
  596.         $dumper = new PhpDumper($container);
  597.         if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) {
  598.             $dumper->setProxyDumper(new ProxyDumper());
  599.         }
  600.         $content $dumper->dump(array(
  601.             'class' => $class,
  602.             'base_class' => $baseClass,
  603.             'file' => $cache->getPath(),
  604.             'as_files' => true,
  605.             'debug' => $this->debug,
  606.             'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
  607.         ));
  608.         $rootCode array_pop($content);
  609.         $dir dirname($cache->getPath()).'/';
  610.         $fs = new Filesystem();
  611.         foreach ($content as $file => $code) {
  612.             $fs->dumpFile($dir.$file$code);
  613.             @chmod($dir.$file0666 & ~umask());
  614.         }
  615.         @unlink(dirname($dir.$file).'.legacy');
  616.         $cache->write($rootCode$container->getResources());
  617.     }
  618.     /**
  619.      * Returns a loader for the container.
  620.      *
  621.      * @return DelegatingLoader The loader
  622.      */
  623.     protected function getContainerLoader(ContainerInterface $container)
  624.     {
  625.         $locator = new FileLocator($this);
  626.         $resolver = new LoaderResolver(array(
  627.             new XmlFileLoader($container$locator),
  628.             new YamlFileLoader($container$locator),
  629.             new IniFileLoader($container$locator),
  630.             new PhpFileLoader($container$locator),
  631.             new GlobFileLoader($container$locator),
  632.             new DirectoryLoader($container$locator),
  633.             new ClosureLoader($container),
  634.         ));
  635.         return new DelegatingLoader($resolver);
  636.     }
  637.     /**
  638.      * Removes comments from a PHP source string.
  639.      *
  640.      * We don't use the PHP php_strip_whitespace() function
  641.      * as we want the content to be readable and well-formatted.
  642.      *
  643.      * @param string $source A PHP string
  644.      *
  645.      * @return string The PHP string with the comments removed
  646.      */
  647.     public static function stripComments($source)
  648.     {
  649.         if (!function_exists('token_get_all')) {
  650.             return $source;
  651.         }
  652.         $rawChunk '';
  653.         $output '';
  654.         $tokens token_get_all($source);
  655.         $ignoreSpace false;
  656.         for ($i 0; isset($tokens[$i]); ++$i) {
  657.             $token $tokens[$i];
  658.             if (!isset($token[1]) || 'b"' === $token) {
  659.                 $rawChunk .= $token;
  660.             } elseif (T_START_HEREDOC === $token[0]) {
  661.                 $output .= $rawChunk.$token[1];
  662.                 do {
  663.                     $token $tokens[++$i];
  664.                     $output .= isset($token[1]) && 'b"' !== $token $token[1] : $token;
  665.                 } while (T_END_HEREDOC !== $token[0]);
  666.                 $rawChunk '';
  667.             } elseif (T_WHITESPACE === $token[0]) {
  668.                 if ($ignoreSpace) {
  669.                     $ignoreSpace false;
  670.                     continue;
  671.                 }
  672.                 // replace multiple new lines with a single newline
  673.                 $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n"$token[1]);
  674.             } elseif (in_array($token[0], array(T_COMMENTT_DOC_COMMENT))) {
  675.                 $ignoreSpace true;
  676.             } else {
  677.                 $rawChunk .= $token[1];
  678.                 // The PHP-open tag already has a new-line
  679.                 if (T_OPEN_TAG === $token[0]) {
  680.                     $ignoreSpace true;
  681.                 }
  682.             }
  683.         }
  684.         $output .= $rawChunk;
  685.         // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
  686.         unset($tokens$rawChunk);
  687.         gc_mem_caches();
  688.         return $output;
  689.     }
  690.     public function serialize()
  691.     {
  692.         return serialize(array($this->environment$this->debug));
  693.     }
  694.     public function unserialize($data)
  695.     {
  696.         list($environment$debug) = unserialize($data, array('allowed_classes' => false));
  697.         $this->__construct($environment$debug);
  698.     }
  699. }