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.2';
  70.     const VERSION_ID 40302;
  71.     const MAJOR_VERSION 4;
  72.     const MINOR_VERSION 3;
  73.     const RELEASE_VERSION 2;
  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 HttpKernel
  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.             $dir $rootDir = \dirname($r->getFileName());
  300.             while (!file_exists($dir.'/composer.json')) {
  301.                 if ($dir === \dirname($dir)) {
  302.                     return $this->projectDir $rootDir;
  303.                 }
  304.                 $dir = \dirname($dir);
  305.             }
  306.             $this->projectDir $dir;
  307.         }
  308.         return $this->projectDir;
  309.     }
  310.     /**
  311.      * {@inheritdoc}
  312.      */
  313.     public function getContainer()
  314.     {
  315.         return $this->container;
  316.     }
  317.     /**
  318.      * @internal
  319.      */
  320.     public function setAnnotatedClassCache(array $annotatedClasses)
  321.     {
  322.         file_put_contents(($this->warmupDir ?: $this->getCacheDir()).'/annotations.map'sprintf('<?php return %s;'var_export($annotatedClassestrue)));
  323.     }
  324.     /**
  325.      * {@inheritdoc}
  326.      */
  327.     public function getStartTime()
  328.     {
  329.         return $this->debug $this->startTime : -INF;
  330.     }
  331.     /**
  332.      * {@inheritdoc}
  333.      */
  334.     public function getCacheDir()
  335.     {
  336.         return $this->getProjectDir().'/var/cache/'.$this->environment;
  337.     }
  338.     /**
  339.      * {@inheritdoc}
  340.      */
  341.     public function getLogDir()
  342.     {
  343.         return $this->getProjectDir().'/var/log';
  344.     }
  345.     /**
  346.      * {@inheritdoc}
  347.      */
  348.     public function getCharset()
  349.     {
  350.         return 'UTF-8';
  351.     }
  352.     /**
  353.      * Gets the patterns defining the classes to parse and cache for annotations.
  354.      */
  355.     public function getAnnotatedClassesToCompile(): array
  356.     {
  357.         return [];
  358.     }
  359.     /**
  360.      * Initializes bundles.
  361.      *
  362.      * @throws \LogicException if two bundles share a common name
  363.      */
  364.     protected function initializeBundles()
  365.     {
  366.         // init bundles
  367.         $this->bundles = [];
  368.         foreach ($this->registerBundles() as $bundle) {
  369.             $name $bundle->getName();
  370.             if (isset($this->bundles[$name])) {
  371.                 throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"'$name));
  372.             }
  373.             $this->bundles[$name] = $bundle;
  374.         }
  375.     }
  376.     /**
  377.      * The extension point similar to the Bundle::build() method.
  378.      *
  379.      * Use this method to register compiler passes and manipulate the container during the building process.
  380.      */
  381.     protected function build(ContainerBuilder $container)
  382.     {
  383.     }
  384.     /**
  385.      * Gets the container class.
  386.      *
  387.      * @throws \InvalidArgumentException If the generated classname is invalid
  388.      *
  389.      * @return string The container class
  390.      */
  391.     protected function getContainerClass()
  392.     {
  393.         $class = \get_class($this);
  394.         $class 'c' === $class[0] && === strpos($class"class@anonymous\0") ? get_parent_class($class).str_replace('.''_'ContainerBuilder::hash($class)) : $class;
  395.         $class $this->name.str_replace('\\''_'$class).ucfirst($this->environment).($this->debug 'Debug' '').'Container';
  396.         if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'$class)) {
  397.             throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.'$this->environment));
  398.         }
  399.         return $class;
  400.     }
  401.     /**
  402.      * Gets the container's base class.
  403.      *
  404.      * All names except Container must be fully qualified.
  405.      *
  406.      * @return string
  407.      */
  408.     protected function getContainerBaseClass()
  409.     {
  410.         return 'Container';
  411.     }
  412.     /**
  413.      * Initializes the service container.
  414.      *
  415.      * The cached version of the service container is used when fresh, otherwise the
  416.      * container is built.
  417.      */
  418.     protected function initializeContainer()
  419.     {
  420.         $class $this->getContainerClass();
  421.         $cacheDir $this->warmupDir ?: $this->getCacheDir();
  422.         $cache = new ConfigCache($cacheDir.'/'.$class.'.php'$this->debug);
  423.         $oldContainer null;
  424.         if ($fresh $cache->isFresh()) {
  425.             // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
  426.             $errorLevel error_reporting(\E_ALL ^ \E_WARNING);
  427.             $fresh $oldContainer false;
  428.             try {
  429.                 if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
  430.                     $this->container->set('kernel'$this);
  431.                     $oldContainer $this->container;
  432.                     $fresh true;
  433.                 }
  434.             } catch (\Throwable $e) {
  435.             } finally {
  436.                 error_reporting($errorLevel);
  437.             }
  438.         }
  439.         if ($fresh) {
  440.             return;
  441.         }
  442.         if ($this->debug) {
  443.             $collectedLogs = [];
  444.             $previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
  445.             $previousHandler $previousHandler ?: set_error_handler(function ($type$message$file$line) use (&$collectedLogs, &$previousHandler) {
  446.                 if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
  447.                     return $previousHandler $previousHandler($type$message$file$line) : false;
  448.                 }
  449.                 if (isset($collectedLogs[$message])) {
  450.                     ++$collectedLogs[$message]['count'];
  451.                     return;
  452.                 }
  453.                 $backtrace debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS5);
  454.                 // Clean the trace by removing first frames added by the error handler itself.
  455.                 for ($i 0; isset($backtrace[$i]); ++$i) {
  456.                     if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  457.                         $backtrace = \array_slice($backtrace$i);
  458.                         break;
  459.                     }
  460.                 }
  461.                 // Remove frames added by DebugClassLoader.
  462.                 for ($i = \count($backtrace) - 2$i; --$i) {
  463.                     if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
  464.                         $backtrace = [$backtrace[$i 1]];
  465.                         break;
  466.                     }
  467.                 }
  468.                 $collectedLogs[$message] = [
  469.                     'type' => $type,
  470.                     'message' => $message,
  471.                     'file' => $file,
  472.                     'line' => $line,
  473.                     'trace' => [$backtrace[0]],
  474.                     'count' => 1,
  475.                 ];
  476.             });
  477.         }
  478.         try {
  479.             $container null;
  480.             $container $this->buildContainer();
  481.             $container->compile();
  482.         } finally {
  483.             if ($this->debug && true !== $previousHandler) {
  484.                 restore_error_handler();
  485.                 file_put_contents($cacheDir.'/'.$class.'Deprecations.log'serialize(array_values($collectedLogs)));
  486.                 file_put_contents($cacheDir.'/'.$class.'Compiler.log'null !== $container implode("\n"$container->getCompiler()->getLog()) : '');
  487.             }
  488.         }
  489.         if (null === $oldContainer && file_exists($cache->getPath())) {
  490.             $errorLevel error_reporting(\E_ALL ^ \E_WARNING);
  491.             try {
  492.                 $oldContainer = include $cache->getPath();
  493.             } catch (\Throwable $e) {
  494.             } finally {
  495.                 error_reporting($errorLevel);
  496.             }
  497.         }
  498.         $oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
  499.         $this->dumpContainer($cache$container$class$this->getContainerBaseClass());
  500.         $this->container = require $cache->getPath();
  501.         $this->container->set('kernel'$this);
  502.         if ($oldContainer && \get_class($this->container) !== $oldContainer->name) {
  503.             // Because concurrent requests might still be using them,
  504.             // old container files are not removed immediately,
  505.             // but on a next dump of the container.
  506.             static $legacyContainers = [];
  507.             $oldContainerDir = \dirname($oldContainer->getFileName());
  508.             $legacyContainers[$oldContainerDir.'.legacy'] = true;
  509.             foreach (glob(\dirname($oldContainerDir).\DIRECTORY_SEPARATOR.'*.legacy') as $legacyContainer) {
  510.                 if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) {
  511.                     (new Filesystem())->remove(substr($legacyContainer0, -7));
  512.                 }
  513.             }
  514.             touch($oldContainerDir.'.legacy');
  515.         }
  516.         if ($this->container->has('cache_warmer')) {
  517.             $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
  518.         }
  519.     }
  520.     /**
  521.      * Returns the kernel parameters.
  522.      *
  523.      * @return array An array of kernel parameters
  524.      */
  525.     protected function getKernelParameters()
  526.     {
  527.         $bundles = [];
  528.         $bundlesMetadata = [];
  529.         foreach ($this->bundles as $name => $bundle) {
  530.             $bundles[$name] = \get_class($bundle);
  531.             $bundlesMetadata[$name] = [
  532.                 'path' => $bundle->getPath(),
  533.                 'namespace' => $bundle->getNamespace(),
  534.             ];
  535.         }
  536.         return [
  537.             /*
  538.              * @deprecated since Symfony 4.2, use kernel.project_dir instead
  539.              */
  540.             'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
  541.             'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
  542.             'kernel.environment' => $this->environment,
  543.             'kernel.debug' => $this->debug,
  544.             /*
  545.              * @deprecated since Symfony 4.2
  546.              */
  547.             'kernel.name' => $this->name,
  548.             'kernel.cache_dir' => realpath($cacheDir $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir,
  549.             'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
  550.             'kernel.bundles' => $bundles,
  551.             'kernel.bundles_metadata' => $bundlesMetadata,
  552.             'kernel.charset' => $this->getCharset(),
  553.             'kernel.container_class' => $this->getContainerClass(),
  554.         ];
  555.     }
  556.     /**
  557.      * Builds the service container.
  558.      *
  559.      * @return ContainerBuilder The compiled service container
  560.      *
  561.      * @throws \RuntimeException
  562.      */
  563.     protected function buildContainer()
  564.     {
  565.         foreach (['cache' => $this->warmupDir ?: $this->getCacheDir(), 'logs' => $this->getLogDir()] as $name => $dir) {
  566.             if (!is_dir($dir)) {
  567.                 if (false === @mkdir($dir0777true) && !is_dir($dir)) {
  568.                     throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n"$name$dir));
  569.                 }
  570.             } elseif (!is_writable($dir)) {
  571.                 throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n"$name$dir));
  572.             }
  573.         }
  574.         $container $this->getContainerBuilder();
  575.         $container->addObjectResource($this);
  576.         $this->prepareContainer($container);
  577.         if (null !== $cont $this->registerContainerConfiguration($this->getContainerLoader($container))) {
  578.             $container->merge($cont);
  579.         }
  580.         $container->addCompilerPass(new AddAnnotatedClassesToCachePass($this));
  581.         return $container;
  582.     }
  583.     /**
  584.      * Prepares the ContainerBuilder before it is compiled.
  585.      */
  586.     protected function prepareContainer(ContainerBuilder $container)
  587.     {
  588.         $extensions = [];
  589.         foreach ($this->bundles as $bundle) {
  590.             if ($extension $bundle->getContainerExtension()) {
  591.                 $container->registerExtension($extension);
  592.             }
  593.             if ($this->debug) {
  594.                 $container->addObjectResource($bundle);
  595.             }
  596.         }
  597.         foreach ($this->bundles as $bundle) {
  598.             $bundle->build($container);
  599.         }
  600.         $this->build($container);
  601.         foreach ($container->getExtensions() as $extension) {
  602.             $extensions[] = $extension->getAlias();
  603.         }
  604.         // ensure these extensions are implicitly loaded
  605.         $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
  606.     }
  607.     /**
  608.      * Gets a new ContainerBuilder instance used to build the service container.
  609.      *
  610.      * @return ContainerBuilder
  611.      */
  612.     protected function getContainerBuilder()
  613.     {
  614.         $container = new ContainerBuilder();
  615.         $container->getParameterBag()->add($this->getKernelParameters());
  616.         if ($this instanceof CompilerPassInterface) {
  617.             $container->addCompilerPass($thisPassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
  618.         }
  619.         if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) {
  620.             $container->setProxyInstantiator(new RuntimeInstantiator());
  621.         }
  622.         return $container;
  623.     }
  624.     /**
  625.      * Dumps the service container to PHP code in the cache.
  626.      *
  627.      * @param ConfigCache      $cache     The config cache
  628.      * @param ContainerBuilder $container The service container
  629.      * @param string           $class     The name of the class to generate
  630.      * @param string           $baseClass The name of the container's base class
  631.      */
  632.     protected function dumpContainer(ConfigCache $cacheContainerBuilder $container$class$baseClass)
  633.     {
  634.         // cache the container
  635.         $dumper = new PhpDumper($container);
  636.         if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) {
  637.             $dumper->setProxyDumper(new ProxyDumper());
  638.         }
  639.         $content $dumper->dump([
  640.             'class' => $class,
  641.             'base_class' => $baseClass,
  642.             'file' => $cache->getPath(),
  643.             'as_files' => true,
  644.             'debug' => $this->debug,
  645.             'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
  646.         ]);
  647.         $rootCode array_pop($content);
  648.         $dir = \dirname($cache->getPath()).'/';
  649.         $fs = new Filesystem();
  650.         foreach ($content as $file => $code) {
  651.             $fs->dumpFile($dir.$file$code);
  652.             @chmod($dir.$file0666 & ~umask());
  653.         }
  654.         $legacyFile = \dirname($dir.$file).'.legacy';
  655.         if (file_exists($legacyFile)) {
  656.             @unlink($legacyFile);
  657.         }
  658.         $cache->write($rootCode$container->getResources());
  659.     }
  660.     /**
  661.      * Returns a loader for the container.
  662.      *
  663.      * @return DelegatingLoader The loader
  664.      */
  665.     protected function getContainerLoader(ContainerInterface $container)
  666.     {
  667.         $locator = new FileLocator($this);
  668.         $resolver = new LoaderResolver([
  669.             new XmlFileLoader($container$locator),
  670.             new YamlFileLoader($container$locator),
  671.             new IniFileLoader($container$locator),
  672.             new PhpFileLoader($container$locator),
  673.             new GlobFileLoader($container$locator),
  674.             new DirectoryLoader($container$locator),
  675.             new ClosureLoader($container),
  676.         ]);
  677.         return new DelegatingLoader($resolver);
  678.     }
  679.     /**
  680.      * Removes comments from a PHP source string.
  681.      *
  682.      * We don't use the PHP php_strip_whitespace() function
  683.      * as we want the content to be readable and well-formatted.
  684.      *
  685.      * @param string $source A PHP string
  686.      *
  687.      * @return string The PHP string with the comments removed
  688.      */
  689.     public static function stripComments($source)
  690.     {
  691.         if (!\function_exists('token_get_all')) {
  692.             return $source;
  693.         }
  694.         $rawChunk '';
  695.         $output '';
  696.         $tokens token_get_all($source);
  697.         $ignoreSpace false;
  698.         for ($i 0; isset($tokens[$i]); ++$i) {
  699.             $token $tokens[$i];
  700.             if (!isset($token[1]) || 'b"' === $token) {
  701.                 $rawChunk .= $token;
  702.             } elseif (T_START_HEREDOC === $token[0]) {
  703.                 $output .= $rawChunk.$token[1];
  704.                 do {
  705.                     $token $tokens[++$i];
  706.                     $output .= isset($token[1]) && 'b"' !== $token $token[1] : $token;
  707.                 } while (T_END_HEREDOC !== $token[0]);
  708.                 $rawChunk '';
  709.             } elseif (T_WHITESPACE === $token[0]) {
  710.                 if ($ignoreSpace) {
  711.                     $ignoreSpace false;
  712.                     continue;
  713.                 }
  714.                 // replace multiple new lines with a single newline
  715.                 $rawChunk .= preg_replace(['/\n{2,}/S'], "\n"$token[1]);
  716.             } elseif (\in_array($token[0], [T_COMMENTT_DOC_COMMENT])) {
  717.                 $ignoreSpace true;
  718.             } else {
  719.                 $rawChunk .= $token[1];
  720.                 // The PHP-open tag already has a new-line
  721.                 if (T_OPEN_TAG === $token[0]) {
  722.                     $ignoreSpace true;
  723.                 }
  724.             }
  725.         }
  726.         $output .= $rawChunk;
  727.         unset($tokens$rawChunk);
  728.         gc_mem_caches();
  729.         return $output;
  730.     }
  731.     /**
  732.      * @deprecated since Symfony 4.3
  733.      */
  734.     public function serialize()
  735.     {
  736.         @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3.'__METHOD__), E_USER_DEPRECATED);
  737.         return serialize([$this->environment$this->debug]);
  738.     }
  739.     /**
  740.      * @deprecated since Symfony 4.3
  741.      */
  742.     public function unserialize($data)
  743.     {
  744.         @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3.'__METHOD__), E_USER_DEPRECATED);
  745.         list($environment$debug) = unserialize($data, ['allowed_classes' => false]);
  746.         $this->__construct($environment$debug);
  747.     }
  748.     public function __sleep()
  749.     {
  750.         if (__CLASS__ !== $c = (new \ReflectionMethod($this'serialize'))->getDeclaringClass()->name) {
  751.             @trigger_error(sprintf('Implementing the "%s::serialize()" method is deprecated since Symfony 4.3.'$c), E_USER_DEPRECATED);
  752.             $this->serialized $this->serialize();
  753.             return ['serialized'];
  754.         }
  755.         return ['environment''debug'];
  756.     }
  757.     public function __wakeup()
  758.     {
  759.         if (__CLASS__ !== $c = (new \ReflectionMethod($this'serialize'))->getDeclaringClass()->name) {
  760.             @trigger_error(sprintf('Implementing the "%s::serialize()" method is deprecated since Symfony 4.3.'$c), E_USER_DEPRECATED);
  761.             $this->unserialize($this->serialized);
  762.             unset($this->serialized);
  763.             return;
  764.         }
  765.         $this->__construct($this->environment$this->debug);
  766.     }
  767. }