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

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