DrupalKernel.php 3.57 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\Core\DrupalKernel.
 */

namespace Drupal\Core;

10
use Drupal\Core\CoreBundle;
11 12 13 14 15 16 17
use Symfony\Component\HttpKernel\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

/**
 * The DrupalKernel class is the core of Drupal itself.
18 19 20 21 22 23 24
 *
 * This class is responsible for building the Dependency Injection Container and
 * also deals with the registration of bundles. It allows registered bundles to
 * add their services to the container. Core provides the CoreBundle, which adds
 * the services required for all core subsystems. Each module can then add its
 * own bundle, i.e. a subclass of Symfony\Component\HttpKernel\Bundle, to
 * register services to the container.
25 26 27
 */
class DrupalKernel extends Kernel {

28 29 30 31 32 33 34 35 36 37
  /**
   * Overrides Kernel::init().
   */
  public function init() {
    // Intentionally empty. The sole purpose is to not execute Kernel::init(),
    // since that overrides/breaks Drupal's current error handling.
    // @todo Investigate whether it is possible to migrate Drupal's error
    //   handling to the one of Kernel without losing functionality.
  }

38 39 40
  /**
   * Returns an array of available bundles.
   */
41
  public function registerBundles() {
42
    $bundles = array(
43
      new CoreBundle(),
44
    );
45

46
    // @todo Remove the necessity of calling system_list() to find out which
katbailey's avatar
katbailey committed
47
    // bundles exist. See http://drupal.org/node/1331486
48 49
    $modules = array_keys(system_list('module_enabled'));
    foreach ($modules as $module) {
katbailey's avatar
katbailey committed
50
      $camelized = ContainerBuilder::camelize($module);
51
      $class = "Drupal\\{$module}\\{$camelized}Bundle";
52 53 54
      if (class_exists($class)) {
        $bundles[] = new $class();
      }
55
    }
56 57
    return $bundles;
  }
58 59


60 61 62
  /**
   * Initializes the service container.
   */
63
  protected function initializeContainer() {
katbailey's avatar
katbailey committed
64
    // @todo We should be compiling the container and dumping to php so we don't
65 66
    //   have to recompile every time. There is a separate issue for this, see
    //   http://drupal.org/node/1668892.
67 68 69 70
    $this->container = $this->buildContainer();
    $this->container->set('kernel', $this);
    drupal_container($this->container);
  }
71

72 73 74 75 76
  /**
   * Builds the service container.
   *
   * @return ContainerBuilder The compiled service container
   */
77
  protected function buildContainer() {
78
    $container = $this->getContainerBuilder();
katbailey's avatar
katbailey committed
79

80
    // Merge in the minimal bootstrap container.
katbailey's avatar
katbailey committed
81 82 83
    if ($bootstrap_container = drupal_container()) {
      $container->merge($bootstrap_container);
    }
84 85
    foreach ($this->bundles as $bundle) {
      $bundle->build($container);
86
    }
87 88 89 90

    // @todo Compile the container: http://drupal.org/node/1706064.
    //$container->compile();

91 92
    return $container;
  }
93

94 95 96 97 98
  /**
   * Gets a new ContainerBuilder instance used to build the service container.
   *
   * @return ContainerBuilder
   */
99
  protected function getContainerBuilder() {
100 101 102
    return new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
  }

katbailey's avatar
katbailey committed
103
  /**
Crell's avatar
Crell committed
104 105
   * Overrides and eliminates this method from the parent class. Do not use.
   *
katbailey's avatar
katbailey committed
106 107 108 109 110 111 112
   * This method is part of the KernelInterface interface, but takes an object
   * implementing LoaderInterface as its only parameter. This is part of the
   * Config compoment from Symfony, which is not provided by Drupal core.
   *
   * Modules wishing to provide an extension to this class which uses this
   * method are responsible for ensuring the Config component exists.
   */
113
  public function registerContainerConfiguration(LoaderInterface $loader) {
114
  }
115
}