Messenger.php 2.5 KB

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