From ea973b3827ae5cdd0d217391ff6b853d8c0640d3 Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Sat, 23 Dec 2017 18:33:07 -0600
Subject: [PATCH] Issue #2931264 by markcarver, claudiu.cristea: Remove static
 \Drupal::$legacyMessenger property

Signed-off-by: Mark Carver <mark.carver@me.com>
---
 core/lib/Drupal.php                                | 21 +-----------
 core/lib/Drupal/Core/Messenger/LegacyMessenger.php | 38 ++++++++++++----------
 2 files changed, 22 insertions(+), 37 deletions(-)

diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php
index 8b98ccd1dc..1697ff62a2 100644
--- a/core/lib/Drupal.php
+++ b/core/lib/Drupal.php
@@ -101,22 +101,6 @@ class Drupal {
    */
   protected static $container;
 
-  /**
-   * The LegacyMessenger instance.
-   *
-   * Note: this is merely used to ensure that the instance survives when
-   * \Drupal::messenger() is invoked. It is required to ensure that messages
-   * are properly transferred to the Messenger service once the container has
-   * been initialized. Do not store the Messenger service here.
-   *
-   * @todo Remove once LegacyMessenger has been removed before 9.0.0.
-   *
-   * @see https://www.drupal.org/node/2928994
-   *
-   * @var \Drupal\Core\Messenger\LegacyMessenger|null
-   */
-  protected static $legacyMessenger;
-
   /**
    * Sets a new global container.
    *
@@ -783,10 +767,7 @@ public static function time() {
   public static function messenger() {
     // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
     // @see https://www.drupal.org/node/2928994
-    if (!isset(static::$legacyMessenger)) {
-      static::$legacyMessenger = new LegacyMessenger();
-    }
-    return static::$legacyMessenger;
+    return new LegacyMessenger();
   }
 
 }
diff --git a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php
index 8603425831..ff323d20f9 100644
--- a/core/lib/Drupal/Core/Messenger/LegacyMessenger.php
+++ b/core/lib/Drupal/Core/Messenger/LegacyMessenger.php
@@ -26,9 +26,13 @@ class LegacyMessenger implements MessengerInterface {
   /**
    * The messages.
    *
+   * Note: this property must remain static because it must behave in a
+   * persistent manner, similar to $_SESSION['messages']. Creating a new class
+   * each time would destroy any previously set messages.
+   *
    * @var array
    */
-  protected $messages;
+  protected static $messages;
 
   /**
    * {@inheritdoc}
@@ -46,8 +50,8 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE)
       return $messenger->addMessage($message, $type, $repeat);
     }
 
-    if (!isset($this->messages[$type])) {
-      $this->messages[$type] = [];
+    if (!isset(static::$messages[$type])) {
+      static::$messages[$type] = [];
     }
 
     if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
@@ -56,8 +60,8 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE)
 
     // Do not use strict type checking so that equivalent string and
     // MarkupInterface objects are detected.
-    if ($repeat || !in_array($message, $this->messages[$type])) {
-      $this->messages[$type][] = $message;
+    if ($repeat || !in_array($message, static::$messages[$type])) {
+      static::$messages[$type][] = $message;
     }
 
     return $this;
@@ -86,7 +90,7 @@ public function all() {
       return $messenger->all();
     }
 
-    return $this->messages;
+    return static::$messages;
   }
 
   /**
@@ -104,15 +108,15 @@ protected function getMessengerService() {
       $messenger = \Drupal::service('messenger');
 
       // Transfer any messages into the service.
-      if (isset($this->messages)) {
-        foreach ($this->messages as $type => $messages) {
+      if (isset(static::$messages)) {
+        foreach (static::$messages as $type => $messages) {
           foreach ($messages as $message) {
             // Force repeat to TRUE since this is merging existing messages to
             // the Messenger service and would have already checked this prior.
             $messenger->addMessage($message, $type, TRUE);
           }
         }
-        unset($this->messages);
+        static::$messages = NULL;
       }
 
       return $messenger;
@@ -128,18 +132,18 @@ protected function getMessengerService() {
     // reasonable to assume that if the container becomes available in a
     // subsequent request, a new instance of this class will be created and
     // this code will never be reached. This is merely for BC purposes.
-    if (!isset($this->messages)) {
+    if (!isset(static::$messages)) {
       // A "session" was already created, perhaps to simply allow usage of
       // the previous method core used to store messages, use it.
       if (isset($_SESSION)) {
         if (!isset($_SESSION['messages'])) {
           $_SESSION['messages'] = [];
         }
-        $this->messages = &$_SESSION['messages'];
+        static::$messages = &$_SESSION['messages'];
       }
       // Otherwise, just set an empty array.
       else {
-        $this->messages = [];
+        static::$messages = [];
       }
     }
   }
@@ -153,7 +157,7 @@ public function messagesByType($type) {
       return $messenger->messagesByType($type);
     }
 
-    return $this->messages[$type];
+    return static::$messages[$type];
   }
 
   /**
@@ -165,8 +169,8 @@ public function deleteAll() {
       return $messenger->deleteAll();
     }
 
-    $messages = $this->messages;
-    unset($this->messages);
+    $messages = static::$messages;
+    static::$messages = NULL;
     return $messages;
   }
 
@@ -179,8 +183,8 @@ public function deleteByType($type) {
       return $messenger->messagesByType($type);
     }
 
-    $messages = $this->messages[$type];
-    unset($this->messages[$type]);
+    $messages = static::$messages[$type];
+    unset(static::$messages[$type]);
     return $messages;
   }
 
-- 
2.14.1

