comparison core/lib/Drupal/Core/Messenger/Messenger.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
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\PageCache\ResponsePolicy\KillSwitch;
7 use Drupal\Core\Render\Markup;
8 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
9
10 /**
11 * The messenger service.
12 */
13 class Messenger implements MessengerInterface {
14
15 /**
16 * The flash bag.
17 *
18 * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
19 */
20 protected $flashBag;
21
22 /**
23 * The kill switch.
24 *
25 * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
26 */
27 protected $killSwitch;
28
29 /**
30 * Messenger constructor.
31 *
32 * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flash_bag
33 * The flash bag.
34 * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $killSwitch
35 * The kill switch.
36 */
37 public function __construct(FlashBagInterface $flash_bag, KillSwitch $killSwitch) {
38 $this->flashBag = $flash_bag;
39 $this->killSwitch = $killSwitch;
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function addError($message, $repeat = FALSE) {
46 return $this->addMessage($message, static::TYPE_ERROR, $repeat);
47 }
48
49 /**
50 * {@inheritdoc}
51 */
52 public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
53 if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
54 $message = Markup::create((string) $message);
55 }
56
57 // Do not use strict type checking so that equivalent string and
58 // MarkupInterface objects are detected.
59 if ($repeat || !in_array($message, $this->flashBag->peek($type))) {
60 $this->flashBag->add($type, $message);
61 }
62
63 // Mark this page as being uncacheable.
64 $this->killSwitch->trigger();
65
66 return $this;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function addStatus($message, $repeat = FALSE) {
73 return $this->addMessage($message, static::TYPE_STATUS, $repeat);
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function addWarning($message, $repeat = FALSE) {
80 return $this->addMessage($message, static::TYPE_WARNING, $repeat);
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function all() {
87 return $this->flashBag->peekAll();
88 }
89
90 /**
91 * {@inheritdoc}
92 */
93 public function deleteAll() {
94 return $this->flashBag->clear();
95 }
96
97 /**
98 * {@inheritdoc}
99 */
100 public function deleteByType($type) {
101 // Flash bag gets and clears flash messages from the stack.
102 return $this->flashBag->get($type);
103 }
104
105 /**
106 * {@inheritdoc}
107 */
108 public function messagesByType($type) {
109 return $this->flashBag->peek($type);
110 }
111
112 }