LegacyMessenger.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Drupal\Core\Messenger;
  3. use Drupal\Component\Render\MarkupInterface;
  4. use Drupal\Core\Render\Markup;
  5. /**
  6. * Provides a LegacyMessenger implementation.
  7. *
  8. * This implementation is for handling messages in a backwards compatible way
  9. * using core's previous $_SESSION storage method.
  10. *
  11. * You should not instantiate a new instance of this class directly. Instead,
  12. * you should inject the "messenger" service into your own services or use
  13. * \Drupal::messenger() in procedural functions.
  14. *
  15. * @see https://www.drupal.org/node/2774931
  16. * @see https://www.drupal.org/node/2928994
  17. *
  18. * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0.
  19. * Use \Drupal\Core\Messenger\Messenger instead.
  20. */
  21. class LegacyMessenger implements MessengerInterface {
  22. /**
  23. * The messages.
  24. *
  25. * Note: this property must remain static because it must behave in a
  26. * persistent manner, similar to $_SESSION['messages']. Creating a new class
  27. * each time would destroy any previously set messages.
  28. *
  29. * @var array
  30. */
  31. protected static $messages;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function addError($message, $repeat = FALSE) {
  36. return $this->addMessage($message, static::TYPE_ERROR, $repeat);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
  42. // Proxy to the Messenger service, if it exists.
  43. if ($messenger = $this->getMessengerService()) {
  44. return $messenger->addMessage($message, $type, $repeat);
  45. }
  46. if (!isset(static::$messages[$type])) {
  47. static::$messages[$type] = [];
  48. }
  49. if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
  50. $message = Markup::create((string) $message);
  51. }
  52. // Do not use strict type checking so that equivalent string and
  53. // MarkupInterface objects are detected.
  54. if ($repeat || !in_array($message, static::$messages[$type])) {
  55. static::$messages[$type][] = $message;
  56. }
  57. return $this;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function addStatus($message, $repeat = FALSE) {
  63. return $this->addMessage($message, static::TYPE_STATUS, $repeat);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function addWarning($message, $repeat = FALSE) {
  69. return $this->addMessage($message, static::TYPE_WARNING, $repeat);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function all() {
  75. // Proxy to the Messenger service, if it exists.
  76. if ($messenger = $this->getMessengerService()) {
  77. return $messenger->all();
  78. }
  79. return static::$messages;
  80. }
  81. /**
  82. * Returns the Messenger service.
  83. *
  84. * @return \Drupal\Core\Messenger\MessengerInterface|null
  85. * The Messenger service.
  86. */
  87. protected function getMessengerService() {
  88. // Use the Messenger service, if it exists.
  89. if (\Drupal::hasService('messenger')) {
  90. // Note: because the container has the potential to be rebuilt during
  91. // requests, this service cannot be directly stored on this class.
  92. /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
  93. $messenger = \Drupal::service('messenger');
  94. // Transfer any messages into the service.
  95. if (isset(static::$messages)) {
  96. foreach (static::$messages as $type => $messages) {
  97. foreach ($messages as $message) {
  98. // Force repeat to TRUE since this is merging existing messages to
  99. // the Messenger service and would have already checked this prior.
  100. $messenger->addMessage($message, $type, TRUE);
  101. }
  102. }
  103. static::$messages = NULL;
  104. }
  105. return $messenger;
  106. }
  107. // Otherwise, trigger an error.
  108. @trigger_error('Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.', E_USER_DEPRECATED);
  109. // Prematurely creating $_SESSION['messages'] in this class' constructor
  110. // causes issues when the container attempts to initialize its own session
  111. // later down the road. This can only be done after it has been determined
  112. // the Messenger service is not available (i.e. no container). It is also
  113. // reasonable to assume that if the container becomes available in a
  114. // subsequent request, a new instance of this class will be created and
  115. // this code will never be reached. This is merely for BC purposes.
  116. if (!isset(static::$messages)) {
  117. // A "session" was already created, perhaps to simply allow usage of
  118. // the previous method core used to store messages, use it.
  119. if (isset($_SESSION)) {
  120. if (!isset($_SESSION['messages'])) {
  121. $_SESSION['messages'] = [];
  122. }
  123. static::$messages = &$_SESSION['messages'];
  124. }
  125. // Otherwise, just set an empty array.
  126. else {
  127. static::$messages = [];
  128. }
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function messagesByType($type) {
  135. // Proxy to the Messenger service, if it exists.
  136. if ($messenger = $this->getMessengerService()) {
  137. return $messenger->messagesByType($type);
  138. }
  139. return static::$messages[$type];
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function deleteAll() {
  145. // Proxy to the Messenger service, if it exists.
  146. if ($messenger = $this->getMessengerService()) {
  147. return $messenger->deleteAll();
  148. }
  149. $messages = static::$messages;
  150. static::$messages = NULL;
  151. return $messages;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function deleteByType($type) {
  157. // Proxy to the Messenger service, if it exists.
  158. if ($messenger = $this->getMessengerService()) {
  159. return $messenger->deleteByType($type);
  160. }
  161. $messages = static::$messages[$type];
  162. unset(static::$messages[$type]);
  163. return $messages;
  164. }
  165. }