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

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