protected function DrupalKernel::buildContainer

Builds the service container.

Return value

ContainerBuilder The compiled service container

Overrides Kernel::buildContainer

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

File

drupal/core/lib/Drupal/Core/DrupalKernel.php, line 418
Definition of Drupal\Core\DrupalKernel.

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

protected function buildContainer() {
  $this
    ->initializeBundles();
  $container = $this
    ->getContainerBuilder();
  $container
    ->setParameter('container.bundles', $this->bundleClasses);
  $container
    ->setParameter('container.modules', $this
    ->getModuleFileNames());

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

  // Add all components in \Drupal\Core and \Drupal\Component that have a
  // Plugin directory.
  foreach (array(
    'Core',
    'Component',
  ) as $parent_directory) {
    $path = DRUPAL_ROOT . '/core/lib/Drupal/' . $parent_directory;
    foreach (new \DirectoryIterator($path) as $component) {
      if (!$component
        ->isDot() && is_dir($component
        ->getPathname() . '/Plugin')) {
        $namespaces['Drupal\\' . $parent_directory . '\\' . $component
          ->getFilename()] = DRUPAL_ROOT . '/core/lib';
      }
    }
  }
  $container
    ->setParameter('container.namespaces', $namespaces);

  // Register synthetic services.
  $container
    ->register('class_loader', 'Symfony\\Component\\ClassLoader\\ClassLoader')
    ->setSynthetic(TRUE);
  $container
    ->register('kernel', 'Symfony\\Component\\HttpKernel\\KernelInterface')
    ->setSynthetic(TRUE);
  $container
    ->register('service_container', 'Symfony\\Component\\DependencyInjection\\ContainerInterface')
    ->setSynthetic(TRUE);
  $yaml_loader = new YamlFileLoader($container);
  foreach ($this->serviceYamls as $filename) {
    $yaml_loader
      ->load($filename);
  }
  foreach ($this->bundles as $bundle) {
    $bundle
      ->build($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) {
    if ($definition
      ->isSynthetic() || $definition
      ->getTag('persist')) {
      $persist_ids[] = $id;
    }
  }
  $container
    ->setParameter('persistIds', $persist_ids);
  $container
    ->compile();
  return $container;
}