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