Issue #2421005 by tim.plunkett: Add \Drupal::hasContainer() instead of checking if \Drupal::getContainer() === NULL

8.0.x
Alex Pott 2015-02-05 20:50:59 +00:00
parent 9407ae4ade
commit 2bf5573595
2 changed files with 15 additions and 3 deletions

View File

@ -733,7 +733,7 @@ function drupal_bootstrap($phase = NULL) {
$boot_level = $phase; $boot_level = $phase;
} }
return \Drupal::getContainer() ? DRUPAL_BOOTSTRAP_CODE : DRUPAL_BOOTSTRAP_CONFIGURATION; return \Drupal::hasContainer() ? DRUPAL_BOOTSTRAP_CODE : DRUPAL_BOOTSTRAP_CONFIGURATION;
} }
/** /**

View File

@ -123,6 +123,16 @@ class Drupal {
return static::$container; return static::$container;
} }
/**
* Returns TRUE if the container has been initialized, FALSE otherwise.
*
* @return bool
*/
public static function hasContainer() {
return static::$container !== NULL;
}
/** /**
* Retrieves a service from the container. * Retrieves a service from the container.
* *
@ -149,7 +159,8 @@ class Drupal {
* TRUE if the specified service exists, FALSE otherwise. * TRUE if the specified service exists, FALSE otherwise.
*/ */
public static function hasService($id) { public static function hasService($id) {
return static::$container && static::$container->has($id); // Check hasContainer() first in order to always return a Boolean.
return static::hasContainer() && static::getContainer()->has($id);
} }
/** /**
@ -168,7 +179,8 @@ class Drupal {
* TRUE if there is a currently active request object, FALSE otherwise. * TRUE if there is a currently active request object, FALSE otherwise.
*/ */
public static function hasRequest() { public static function hasRequest() {
return static::$container && static::$container->has('request_stack') && static::$container->get('request_stack')->getCurrentRequest() !== NULL; // Check hasContainer() first in order to always return a Boolean.
return static::hasContainer() && static::getContainer()->has('request_stack') && static::getContainer()->get('request_stack')->getCurrentRequest() !== NULL;
} }
/** /**