DrupalKernel.php 3.21 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
  /**
   * Returns an array of available bundles.
   */
31
  public function registerBundles() {
32
    $bundles = array(
33
      new CoreBundle(),
34
    );
35

36
    // @todo Remove the necessity of calling system_list() to find out which
katbailey's avatar
katbailey committed
37
    // bundles exist. See http://drupal.org/node/1331486
38 39
    $modules = array_keys(system_list('module_enabled'));
    foreach ($modules as $module) {
katbailey's avatar
katbailey committed
40
      $camelized = ContainerBuilder::camelize($module);
41
      $class = "Drupal\\{$module}\\{$camelized}Bundle";
42 43 44
      if (class_exists($class)) {
        $bundles[] = new $class();
      }
45
    }
46 47
    return $bundles;
  }
48 49


50 51 52
  /**
   * Initializes the service container.
   */
53
  protected function initializeContainer() {
katbailey's avatar
katbailey committed
54
    // @todo We should be compiling the container and dumping to php so we don't
55 56
    //   have to recompile every time. There is a separate issue for this, see
    //   http://drupal.org/node/1668892.
57 58 59 60
    $this->container = $this->buildContainer();
    $this->container->set('kernel', $this);
    drupal_container($this->container);
  }
61

62 63 64 65 66
  /**
   * Builds the service container.
   *
   * @return ContainerBuilder The compiled service container
   */
67
  protected function buildContainer() {
68
    $container = $this->getContainerBuilder();
katbailey's avatar
katbailey committed
69

70
    // Merge in the minimal bootstrap container.
katbailey's avatar
katbailey committed
71 72 73
    if ($bootstrap_container = drupal_container()) {
      $container->merge($bootstrap_container);
    }
74 75
    foreach ($this->bundles as $bundle) {
      $bundle->build($container);
76
    }
77 78 79 80

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

81 82
    return $container;
  }
83

84 85 86 87 88
  /**
   * Gets a new ContainerBuilder instance used to build the service container.
   *
   * @return ContainerBuilder
   */
89
  protected function getContainerBuilder() {
90 91 92
    return new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
  }

katbailey's avatar
katbailey committed
93
  /**
Crell's avatar
Crell committed
94 95
   * Overrides and eliminates this method from the parent class. Do not use.
   *
katbailey's avatar
katbailey committed
96 97 98 99 100 101 102
   * 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.
   */
103
  public function registerContainerConfiguration(LoaderInterface $loader) {
104
  }
105
}