DrupalKernel::compileContainer

protected DrupalKernel::compileContainer()

Compiles a new service container.

Return value

ContainerBuilder The compiled service container

File

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

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());

  // 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 (array('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 $component \DirectoryIterator */
      $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 = array('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 reinstantiated. Also include services tagged to persist.
  $persist_ids = array();
  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->compile();
  return $container;
}
doc_Drupal
2016-10-29 09:02:59
Comments
Leave a Comment

Please login to continue.