You are here

protected function DrupalKernel::compileContainer in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::compileContainer()
  2. 9 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::compileContainer()

Compiles a new service container.

Return value

\Drupal\Core\DependencyInjection\ContainerBuilder The compiled service container

1 call to DrupalKernel::compileContainer()
DrupalKernel::initializeContainer in core/lib/Drupal/Core/DrupalKernel.php
Initializes the service container.

File

core/lib/Drupal/Core/DrupalKernel.php, line 1164

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

protected function compileContainer() {

  // We are forcing a container build so it is reasonable to assume that the
  // calling method knows something about the system has changed requiring the
  // container to be dumped to the filesystem.
  if ($this->allowDumping) {
    $this->containerNeedsDumping = TRUE;
  }
  $this
    ->initializeServiceProviders();
  $container = $this
    ->getContainerBuilder();
  $container
    ->set('kernel', $this);
  $container
    ->setParameter('container.modules', $this
    ->getModulesParameter());
  $container
    ->setParameter('install_profile', $this
    ->getInstallProfile());

  // Get a list of namespaces and put it onto the container.
  $namespaces = $this
    ->getModuleNamespacesPsr4($this
    ->getModuleFileNames());

  // Add all components in \Drupal\Core and \Drupal\Component that have one of
  // the following directories:
  // - Element
  // - Entity
  // - Plugin
  foreach ([
    'Core',
    'Component',
  ] as $parent_directory) {
    $path = 'core/lib/Drupal/' . $parent_directory;
    $parent_namespace = 'Drupal\\' . $parent_directory;
    foreach (new \DirectoryIterator($this->root . '/' . $path) as $component) {

      /** @var \DirectoryIterator $component */
      $pathname = $component
        ->getPathname();
      if (!$component
        ->isDot() && $component
        ->isDir() && (is_dir($pathname . '/Plugin') || is_dir($pathname . '/Entity') || is_dir($pathname . '/Element'))) {
        $namespaces[$parent_namespace . '\\' . $component
          ->getFilename()] = $path . '/' . $component
          ->getFilename();
      }
    }
  }
  $container
    ->setParameter('container.namespaces', $namespaces);

  // Store the default language values on the container. This is so that the
  // default language can be configured using the configuration factory. This
  // avoids the circular dependencies that would created by
  // \Drupal\language\LanguageServiceProvider::alter() and allows the default
  // language to not be English in the installer.
  $default_language_values = Language::$defaultValues;
  if ($system = $this
    ->getConfigStorage()
    ->read('system.site')) {
    if ($default_language_values['id'] != $system['langcode']) {
      $default_language_values = [
        'id' => $system['langcode'],
      ];
    }
  }
  $container
    ->setParameter('language.default_values', $default_language_values);

  // Register synthetic services.
  $container
    ->register('class_loader')
    ->setSynthetic(TRUE);
  $container
    ->register('kernel', 'Symfony\\Component\\HttpKernel\\KernelInterface')
    ->setSynthetic(TRUE);
  $container
    ->register('service_container', 'Symfony\\Component\\DependencyInjection\\ContainerInterface')
    ->setSynthetic(TRUE);

  // Register application services.
  $yaml_loader = new YamlFileLoader($container);
  foreach ($this->serviceYamls['app'] as $filename) {
    $yaml_loader
      ->load($filename);
  }
  foreach ($this->serviceProviders['app'] as $provider) {
    if ($provider instanceof ServiceProviderInterface) {
      $provider
        ->register($container);
    }
  }

  // Register site-specific service overrides.
  foreach ($this->serviceYamls['site'] as $filename) {
    $yaml_loader
      ->load($filename);
  }
  foreach ($this->serviceProviders['site'] as $provider) {
    if ($provider instanceof ServiceProviderInterface) {
      $provider
        ->register($container);
    }
  }

  // Identify all services whose instances should be persisted when rebuilding
  // the container during the lifetime of the kernel (e.g., during a kernel
  // reboot). Include synthetic services, because by definition, they cannot
  // be automatically re-instantiated. Also include services tagged to
  // persist.
  $persist_ids = [];
  foreach ($container
    ->getDefinitions() as $id => $definition) {

    // It does not make sense to persist the container itself, exclude it.
    if ($id !== 'service_container' && ($definition
      ->isSynthetic() || $definition
      ->getTag('persist'))) {
      $persist_ids[] = $id;
    }
  }
  $container
    ->setParameter('persist_ids', $persist_ids);
  $container
    ->setParameter('app.root', $this
    ->getAppRoot());
  $container
    ->setParameter('site.path', $this
    ->getSitePath());
  $container
    ->compile();
  return $container;
}