Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Kernel.php

function Kernel::initializeContainer

Initializes the service container.

The built version of the service container is used when fresh, otherwise the container is built.

1 call to Kernel::initializeContainer()
Kernel::preBoot in vendor/symfony/http-kernel/Kernel.php

File

vendor/symfony/http-kernel/Kernel.php, line 391

Class

Kernel
The Kernel is the heart of the Symfony system.

Namespace

Symfony\Component\HttpKernel

Code

protected function initializeContainer() : void {
    $class = $this->getContainerClass();
    $buildDir = $this->warmupDir ?: $this->getBuildDir();
    $skip = $_SERVER['SYMFONY_DISABLE_RESOURCE_TRACKING'] ?? '';
    $skip = filter_var($skip, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE) ?? explode(',', $skip);
    $cache = new ConfigCache($buildDir . '/' . $class . '.php', $this->debug, null, \is_array($skip) && [
        '*',
    ] !== $skip ? $skip : ($skip ? [] : null));
    $cachePath = $cache->getPath();
    // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
    $errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
    try {
        if (is_file($cachePath) && \is_object($this->container = (include $cachePath)) && (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh()))) {
            self::$freshCache[$cachePath] = true;
            $this->container
                ->set('kernel', $this);
            error_reporting($errorLevel);
            return;
        }
    } catch (\Throwable $e) {
    }
    $oldContainer = \is_object($this->container) ? new \ReflectionClass($this->container) : ($this->container = null);
    try {
        is_dir($buildDir) ?: mkdir($buildDir, 0777, true);
        if ($lock = fopen($cachePath . '.lock', 'w+')) {
            if (!flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock) && !flock($lock, $wouldBlock ? \LOCK_SH : \LOCK_EX)) {
                fclose($lock);
                $lock = null;
            }
            elseif (!is_file($cachePath) || !\is_object($this->container = (include $cachePath))) {
                $this->container = null;
            }
            elseif (!$oldContainer || $this->container::class !== $oldContainer->name) {
                flock($lock, \LOCK_UN);
                fclose($lock);
                $this->container
                    ->set('kernel', $this);
                return;
            }
        }
    } catch (\Throwable $e) {
    } finally {
        error_reporting($errorLevel);
    }
    if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
        $collectedLogs = [];
        $previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
            if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
                return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
            }
            if (isset($collectedLogs[$message])) {
                ++$collectedLogs[$message]['count'];
                return null;
            }
            $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 5);
            // Clean the trace by removing first frames added by the error handler itself.
            for ($i = 0; isset($backtrace[$i]); ++$i) {
                if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
                    $backtrace = \array_slice($backtrace, 1 + $i);
                    break;
                }
            }
            for ($i = 0; isset($backtrace[$i]); ++$i) {
                if (!isset($backtrace[$i]['file'], $backtrace[$i]['line'], $backtrace[$i]['function'])) {
                    continue;
                }
                if (!isset($backtrace[$i]['class']) && 'trigger_deprecation' === $backtrace[$i]['function']) {
                    $file = $backtrace[$i]['file'];
                    $line = $backtrace[$i]['line'];
                    $backtrace = \array_slice($backtrace, 1 + $i);
                    break;
                }
            }
            // Remove frames added by DebugClassLoader.
            for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
                if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
                    $backtrace = [
                        $backtrace[$i + 1],
                    ];
                    break;
                }
            }
            $collectedLogs[$message] = [
                'type' => $type,
                'message' => $message,
                'file' => $file,
                'line' => $line,
                'trace' => [
                    $backtrace[0],
                ],
                'count' => 1,
            ];
            return null;
        });
    }
    try {
        $container = null;
        $container = $this->buildContainer();
        $container->compile();
    } finally {
        if ($collectDeprecations) {
            restore_error_handler();
            @file_put_contents($buildDir . '/' . $class . 'Deprecations.log', serialize(array_values($collectedLogs)));
            @file_put_contents($buildDir . '/' . $class . 'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()
                ->getLog()) : '');
        }
    }
    $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
    if ($lock) {
        flock($lock, \LOCK_UN);
        fclose($lock);
    }
    $this->container = (require $cachePath);
    $this->container
        ->set('kernel', $this);
    if ($oldContainer && $this->container::class !== $oldContainer->name) {
        // Because concurrent requests might still be using them,
        // old container files are not removed immediately,
        // but on a next dump of the container.
        static $legacyContainers = [];
        $oldContainerDir = \dirname($oldContainer->getFileName());
        $legacyContainers[$oldContainerDir . '.legacy'] = true;
        foreach (glob(\dirname($oldContainerDir) . \DIRECTORY_SEPARATOR . '*.legacy', \GLOB_NOSORT) as $legacyContainer) {
            if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) {
                (new Filesystem())->remove(substr($legacyContainer, 0, -7));
            }
        }
        touch($oldContainerDir . '.legacy');
    }
    $buildDir = $this->container
        ->getParameter('kernel.build_dir');
    $cacheDir = $this->container
        ->getParameter('kernel.cache_dir');
    $preload = $this instanceof WarmableInterface ? $this->warmUp($cacheDir, $buildDir) : [];
    if ($this->container
        ->has('cache_warmer')) {
        $cacheWarmer = $this->container
            ->get('cache_warmer');
        if ($cacheDir !== $buildDir) {
            $cacheWarmer->enableOptionalWarmers();
        }
        $preload = array_merge($preload, $cacheWarmer->warmUp($cacheDir, $buildDir));
    }
    if ($preload && file_exists($preloadFile = $buildDir . '/' . $class . '.preload.php')) {
        Preloader::append($preloadFile, $preload);
    }
}
RSS feed
Powered by Drupal