diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index d9f9672bd9..825d2730d0 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -6,9 +6,9 @@ */ use Drupal\Core\DependencyInjection\ContainerNotInitializedException; -use Drupal\Core\Messenger\MemoryMessenger; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Messenger\ChainedMessenger; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Static Service Container wrapper. @@ -101,6 +101,13 @@ class Drupal { */ protected static $container; + /** + * The messenger. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected static $messenger; + /** * Sets a new global container. * @@ -765,10 +772,10 @@ public static function time() { * The messenger. */ public static function messenger() { - if (static::hasService('messenger')) { - return static::getContainer()->get('messenger'); + if (static::$messenger === NULL) { + static::$messenger = new ChainedMessenger(); } - return new MemoryMessenger(); + return static::$messenger; } } diff --git a/core/lib/Drupal/Core/Messenger/BaseMessenger.php b/core/lib/Drupal/Core/Messenger/BaseMessenger.php new file mode 100644 index 0000000000..ce45ebf3af --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/BaseMessenger.php @@ -0,0 +1,50 @@ +addMessage($message, static::TYPE_STATUS); + } + + /** + * {@inheritdoc} + */ + public function addError($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_ERROR); + } + + /** + * {@inheritdoc} + */ + public function addWarning($message, $repeat = FALSE) { + return $this->addMessage($message, static::TYPE_WARNING); + } + + /** + * Convert any safe strings to markup. + * + * @param string|\Drupal\Component\Render\MarkupInterface $message + * The message. + * + * @return string|\Drupal\Component\Render\MarkupInterface + * The markup. + */ + protected function convertToMarkup($message) { + if (!($message instanceof Markup) && $message instanceof MarkupInterface) { + $message = Markup::create((string) $message); + } + return $message; + } + +} diff --git a/core/lib/Drupal/Core/Messenger/ChainedMessenger.php b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php new file mode 100644 index 0000000000..e69f8472e9 --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/ChainedMessenger.php @@ -0,0 +1,109 @@ +memoryMessenger = new MemoryMessenger(); + } + + /** + * {@inheritdoc} + */ + public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { + return $this->getMessenger()->addMessage($message, $type, $repeat); + } + + /** + * {@inheritdoc} + */ + public function all() { + return $this->getMessenger()->all(); + } + + /** + * {@inheritdoc} + */ + public function messagesByType($type) { + return $this->getMessenger()->messagesByType($type); + } + + /** + * {@inheritdoc} + */ + public function deleteAll() { + return $this->getMessenger()->deleteAll(); + } + + /** + * {@inheritdoc} + */ + public function deleteByType($type) { + return $this->getMessenger()->deleteByType($type); + } + + /** + * Get the messenger to use. + * + * @return \Drupal\Core\Messenger\MessengerInterface + * The messenger. + */ + protected function getMessenger() { + // Shortcut if we have a messenger service. + if (isset($this->serviceMessenger)) { + return $this->serviceMessenger; + } + + // If the service exists, but we haven't set it yet. + if (\Drupal::hasService('messenger')) { + $this->serviceMessenger = \Drupal::service('messenger'); + $this->mergeMessages($this->memoryMessenger, $this->serviceMessenger); + return $this->serviceMessenger; + } + + // Fallback to memory messenger. + return $this->memoryMessenger; + } + + /** + * Merges messages from one messenger to another. + * + * @param \Drupal\Core\Messenger\MessengerInterface $from + * The messenger to merge from. + * @param \Drupal\Core\Messenger\MessengerInterface $to + * The messenger to merge to. + */ + protected function mergeMessages(MessengerInterface $from, MessengerInterface $to) { + foreach ($from->all() as $type => $messages) { + foreach ($messages as $message) { + $to->addMessage($message, $type); + } + } + } + +} diff --git a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php index a433cab1b0..be97620acf 100644 --- a/core/lib/Drupal/Core/Messenger/MemoryMessenger.php +++ b/core/lib/Drupal/Core/Messenger/MemoryMessenger.php @@ -10,7 +10,7 @@ * * Used for early the bootstrap phase when there is no service container. */ -class MemoryMessenger implements MessengerInterface { +class MemoryMessenger extends BaseMessenger { /** * The messages. @@ -23,10 +23,7 @@ class MemoryMessenger implements MessengerInterface { * {@inheritdoc} */ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - // Convert strings which are safe to the simplest Markup objects. - if (!($message instanceof Markup) && $message instanceof MarkupInterface) { - $message = Markup::create((string) $message); - } + $message = $this->convertToMarkup($message); // Do not use strict type checking so that equivalent string and // MarkupInterface objects are detected. if ($repeat || !in_array($message, $this->messages[$type])) { @@ -36,27 +33,6 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) return $this; } - /** - * {@inheritdoc} - */ - public function addStatus($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_STATUS); - } - - /** - * {@inheritdoc} - */ - public function addError($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_ERROR); - } - - /** - * {@inheritdoc} - */ - public function addWarning($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_WARNING); - } - /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index 04ecf9c7bc..5dc8fa8bb7 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -12,7 +12,7 @@ * * @internal */ -class Messenger implements MessengerInterface { +class Messenger extends BaseMessenger { /** * The flash bag. @@ -45,10 +45,7 @@ public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch * {@inheritdoc} */ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) { - // Convert strings which are safe to the simplest Markup objects. - if (!($message instanceof Markup) && $message instanceof MarkupInterface) { - $message = Markup::create((string) $message); - } + $message = $this->convertToMarkup($message); // Do not use strict type checking so that equivalent string and // MarkupInterface objects are detected. if ($repeat || !in_array($message, $this->flashBag->peek($type))) { @@ -61,27 +58,6 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) return $this; } - /** - * {@inheritdoc} - */ - public function addStatus($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_STATUS); - } - - /** - * {@inheritdoc} - */ - public function addError($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_ERROR); - } - - /** - * {@inheritdoc} - */ - public function addWarning($message, $repeat = FALSE) { - return $this->addMessage($message, static::TYPE_WARNING); - } - /** * {@inheritdoc} */