You are here

class Drupal in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal.php \Drupal

Static Service Container wrapper.

Generally, code in Drupal should accept its dependencies via either constructor injection or setter method injection. However, there are cases, particularly in legacy procedural code, where that is infeasible. This class acts as a unified global accessor to arbitrary services within the system in order to ease the transition from procedural code to injected OO code.

Hierarchy

Expanded class hierarchy of Drupal

File

lib/Drupal.php, line 18
Contains Drupal 7 foreward compatibility layer for Drupal 8.

View source
class Drupal {

  /**
   * The currently active container object.
   *
   * @var \Drupal\service_container\DependencyInjection\ContainerInterface
   */
  protected static $container;

  /**
   * Returns the currently active global container.
   *
   * @deprecated This method is only useful for the testing environment. It
   * should not be used otherwise.
   *
   * @return \Drupal\service_container\DependencyInjection\ContainerInterface
   */
  public static function getContainer() {
    return static::$container;
  }

  /**
   * Retrieves a service from the container.
   *
   * Use this method if the desired service is not one of those with a dedicated
   * accessor method below. If it is listed below, those methods are preferred
   * as they can return useful type hints.
   *
   * @param string $id
   *   The ID of the service to retrieve.
   * @return mixed
   *   The specified service.
   */
  public static function service($id) {
    return static::$container
      ->get($id);
  }

  /**
   * Indicates if a service is defined in the container.
   *
   * @param string $id
   *   The ID of the service to check.
   *
   * @return bool
   *   TRUE if the specified service exists, FALSE otherwise.
   */
  public static function hasService($id) {

    // @todo Add ->has method to the container to be compatible.
    return static::$container && static::$container
      ->hasDefinition($id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Drupal::$container protected static property The currently active container object.
Drupal::getContainer Deprecated public static function Returns the currently active global container.
Drupal::hasService public static function Indicates if a service is defined in the container.
Drupal::service public static function Retrieves a service from the container.