diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 9efc0a1..cd11ce5 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -5,6 +5,7 @@ * Contains Drupal. */ +use Drupal\Core\DependencyInjection\ContainerNotInitializedException; use Drupal\Core\DependencyInjection\PlaceholderContainer; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; @@ -28,7 +29,7 @@ * Note: PHP does not care whether this is called before or after the class * declaration. It is called before only for better visibility. */ -\Drupal::initStaticProperties(NULL); +\Drupal::initStaticProperties(); /** * Static Service Container wrapper. @@ -135,11 +136,10 @@ public static function setContainer(ContainerInterface $container) { * Unsets the global container. * * @param string|null $message + * The message to pass to the placeholder container. */ public static function unsetContainer($message = NULL) { - if (!isset($message)) { - $message = '\Drupal::$container was unset with \Drupal::unsetContainer().'; - } + $message = $message ?: '\Drupal::$container was unset with \Drupal::unsetContainer().'; static::setContainer(new PlaceholderContainer($message)); } @@ -161,11 +161,13 @@ public static function initStaticProperties() { */ public static function getContainer() { if (static::$container instanceof PlaceholderContainer) { - // Currently, some components depend on this method returning NULL if not - // initialized. - // @todo Throw an exception instead, and change all code that expects - // NULL. - return NULL; + // @todo Currently drush depends on this method returning NULL if not + // initialized. Once drush is fixed, remove this workaround. + if (PHP_SAPI === 'cli') { + return NULL; + } + // Trigger the exception from the placeholder container. + static::$container->throwException(); } return static::$container; } diff --git a/core/lib/Drupal/Core/DependencyInjection/ContainerNotInitializedException.php b/core/lib/Drupal/Core/DependencyInjection/ContainerNotInitializedException.php index 6de8116..e593243 100644 --- a/core/lib/Drupal/Core/DependencyInjection/ContainerNotInitializedException.php +++ b/core/lib/Drupal/Core/DependencyInjection/ContainerNotInitializedException.php @@ -1,5 +1,9 @@ message = isset($exception_message) - ? $exception_message - : 'Container not initialized.'; + $this->message = $exception_message ?: 'Container not initialized.'; } /** * {@inheritdoc} */ public function set($id, $service, $scope = self::SCOPE_CONTAINER) { + $this->throwException(); + } + + /** + * Throws an exception. + * + * @throws \Drupal\Core\DependencyInjection\ContainerNotInitializedException + */ + public function throwException() { throw new ContainerNotInitializedException($this->message); } @@ -45,7 +54,7 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER) { * {@inheritdoc} */ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** @@ -59,55 +68,56 @@ public function has($id) { * {@inheritdoc} */ public function getParameter($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function hasParameter($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function setParameter($name, $value) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function enterScope($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function leaveScope($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function addScope(ScopeInterface $scope) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function hasScope($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } /** * {@inheritdoc} */ public function isScopeActive($name) { - throw new ContainerNotInitializedException($this->message); + $this->throwException(); } + }