From 2bf55735950bccf6032c2a5a7815a006e185b283 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 5 Feb 2015 20:50:59 +0000
Subject: [PATCH] Issue #2421005 by tim.plunkett: Add \Drupal::hasContainer()
 instead of checking if \Drupal::getContainer() === NULL

---
 core/includes/bootstrap.inc |  2 +-
 core/lib/Drupal.php         | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 6fae299bf1d0..13f71071c649 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -733,7 +733,7 @@ function drupal_bootstrap($phase = NULL) {
     $boot_level = $phase;
   }
 
-  return \Drupal::getContainer() ? DRUPAL_BOOTSTRAP_CODE : DRUPAL_BOOTSTRAP_CONFIGURATION;
+  return \Drupal::hasContainer() ? DRUPAL_BOOTSTRAP_CODE : DRUPAL_BOOTSTRAP_CONFIGURATION;
 }
 
 /**
diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php
index 34c641c0adf2..c3f2c903417a 100644
--- a/core/lib/Drupal.php
+++ b/core/lib/Drupal.php
@@ -123,6 +123,16 @@ public static function getContainer() {
     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.
    *
@@ -149,7 +159,8 @@ public static function service($id) {
    *   TRUE if the specified service exists, FALSE otherwise.
    */
   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 @@ public static function root() {
    *   TRUE if there is a currently active request object, FALSE otherwise.
    */
   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;
   }
 
   /**
-- 
GitLab