Unverified Commit 984a2684 authored by larowlan's avatar larowlan

Issue #2931264 by markcarver, claudiu.cristea: Remove static \Drupal::$legacyMessenger property

parent 8946662d
...@@ -101,22 +101,6 @@ class Drupal { ...@@ -101,22 +101,6 @@ class Drupal {
*/ */
protected static $container; 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. * Sets a new global container.
* *
...@@ -783,10 +767,7 @@ public static function time() { ...@@ -783,10 +767,7 @@ public static function time() {
public static function messenger() { public static function messenger() {
// @todo Replace with service once LegacyMessenger is removed in 9.0.0. // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
// @see https://www.drupal.org/node/2928994 // @see https://www.drupal.org/node/2928994
if (!isset(static::$legacyMessenger)) { return new LegacyMessenger();
static::$legacyMessenger = new LegacyMessenger();
}
return static::$legacyMessenger;
} }
} }
...@@ -26,9 +26,13 @@ class LegacyMessenger implements MessengerInterface { ...@@ -26,9 +26,13 @@ class LegacyMessenger implements MessengerInterface {
/** /**
* The messages. * 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 * @var array
*/ */
protected $messages; protected static $messages;
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -46,8 +50,8 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) ...@@ -46,8 +50,8 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE)
return $messenger->addMessage($message, $type, $repeat); return $messenger->addMessage($message, $type, $repeat);
} }
if (!isset($this->messages[$type])) { if (!isset(static::$messages[$type])) {
$this->messages[$type] = []; static::$messages[$type] = [];
} }
if (!($message instanceof Markup) && $message instanceof MarkupInterface) { if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
...@@ -56,8 +60,8 @@ public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) ...@@ -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 // Do not use strict type checking so that equivalent string and
// MarkupInterface objects are detected. // MarkupInterface objects are detected.
if ($repeat || !in_array($message, $this->messages[$type])) { if ($repeat || !in_array($message, static::$messages[$type])) {
$this->messages[$type][] = $message; static::$messages[$type][] = $message;
} }
return $this; return $this;
...@@ -86,7 +90,7 @@ public function all() { ...@@ -86,7 +90,7 @@ public function all() {
return $messenger->all(); return $messenger->all();
} }
return $this->messages; return static::$messages;
} }
/** /**
...@@ -104,15 +108,15 @@ protected function getMessengerService() { ...@@ -104,15 +108,15 @@ protected function getMessengerService() {
$messenger = \Drupal::service('messenger'); $messenger = \Drupal::service('messenger');
// Transfer any messages into the service. // Transfer any messages into the service.
if (isset($this->messages)) { if (isset(static::$messages)) {
foreach ($this->messages as $type => $messages) { foreach (static::$messages as $type => $messages) {
foreach ($messages as $message) { foreach ($messages as $message) {
// Force repeat to TRUE since this is merging existing messages to // Force repeat to TRUE since this is merging existing messages to
// the Messenger service and would have already checked this prior. // the Messenger service and would have already checked this prior.
$messenger->addMessage($message, $type, TRUE); $messenger->addMessage($message, $type, TRUE);
} }
} }
unset($this->messages); static::$messages = NULL;
} }
return $messenger; return $messenger;
...@@ -128,18 +132,18 @@ protected function getMessengerService() { ...@@ -128,18 +132,18 @@ protected function getMessengerService() {
// reasonable to assume that if the container becomes available in a // reasonable to assume that if the container becomes available in a
// subsequent request, a new instance of this class will be created and // subsequent request, a new instance of this class will be created and
// this code will never be reached. This is merely for BC purposes. // 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 // A "session" was already created, perhaps to simply allow usage of
// the previous method core used to store messages, use it. // the previous method core used to store messages, use it.
if (isset($_SESSION)) { if (isset($_SESSION)) {
if (!isset($_SESSION['messages'])) { if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = []; $_SESSION['messages'] = [];
} }
$this->messages = &$_SESSION['messages']; static::$messages = &$_SESSION['messages'];
} }
// Otherwise, just set an empty array. // Otherwise, just set an empty array.
else { else {
$this->messages = []; static::$messages = [];
} }
} }
} }
...@@ -153,7 +157,7 @@ public function messagesByType($type) { ...@@ -153,7 +157,7 @@ public function messagesByType($type) {
return $messenger->messagesByType($type); return $messenger->messagesByType($type);
} }
return $this->messages[$type]; return static::$messages[$type];
} }
/** /**
...@@ -165,8 +169,8 @@ public function deleteAll() { ...@@ -165,8 +169,8 @@ public function deleteAll() {
return $messenger->deleteAll(); return $messenger->deleteAll();
} }
$messages = $this->messages; $messages = static::$messages;
unset($this->messages); static::$messages = NULL;
return $messages; return $messages;
} }
...@@ -179,8 +183,8 @@ public function deleteByType($type) { ...@@ -179,8 +183,8 @@ public function deleteByType($type) {
return $messenger->messagesByType($type); return $messenger->messagesByType($type);
} }
$messages = $this->messages[$type]; $messages = static::$messages[$type];
unset($this->messages[$type]); unset(static::$messages[$type]);
return $messages; return $messages;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment