Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessagePlugin.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
23  const MESSAGES_COOKIES_NAME = 'mage-messages';
24 
28  private $cookieManager;
29 
33  private $cookieMetadataFactory;
34 
38  private $messageManager;
39 
43  private $interpretationStrategy;
44 
48  private $serializer;
49 
53  private $inlineTranslate;
54 
63  public function __construct(
64  \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
65  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
66  \Magento\Framework\Message\ManagerInterface $messageManager,
67  \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
68  \Magento\Framework\Serialize\Serializer\Json $serializer = null,
69  InlineInterface $inlineTranslate = null
70  ) {
71  $this->cookieManager = $cookieManager;
72  $this->cookieMetadataFactory = $cookieMetadataFactory;
73  $this->messageManager = $messageManager;
74  $this->serializer = $serializer ?: ObjectManager::getInstance()
75  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
76  $this->interpretationStrategy = $interpretationStrategy;
77  $this->inlineTranslate = $inlineTranslate ?: ObjectManager::getInstance()->get(InlineInterface::class);
78  }
79 
90  public function afterRenderResult(
91  ResultInterface $subject,
93  ) {
94  if (!($subject instanceof Json)) {
95  $this->setCookie($this->getMessages());
96  }
97  return $result;
98  }
99 
123  private function setCookie(array $messages)
124  {
125  if (!empty($messages)) {
126  if ($this->inlineTranslate->isAllowed()) {
127  foreach ($messages as &$message) {
128  $message['text'] = $this->convertMessageText($message['text']);
129  }
130  }
131 
132  $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
133  $publicCookieMetadata->setDurationOneYear();
134  $publicCookieMetadata->setPath('/');
135  $publicCookieMetadata->setHttpOnly(false);
136 
137  $this->cookieManager->setPublicCookie(
138  self::MESSAGES_COOKIES_NAME,
139  $this->serializer->serialize($messages),
140  $publicCookieMetadata
141  );
142  }
143  }
144 
151  private function convertMessageText(string $text): string
152  {
153  if (preg_match('#' . ParserInterface::REGEXP_TOKEN . '#', $text, $matches)) {
154  $text = $matches[1];
155  }
156 
157  return $text;
158  }
159 
165  protected function getMessages()
166  {
167  $messages = $this->getCookiesMessages();
169  foreach ($this->messageManager->getMessages(true)->getItems() as $message) {
170  $messages[] = [
171  'type' => $message->getType(),
172  'text' => $this->interpretationStrategy->interpret($message),
173  ];
174  }
175  return $messages;
176  }
177 
183  protected function getCookiesMessages()
184  {
185  $messages = $this->cookieManager->getCookie(self::MESSAGES_COOKIES_NAME);
186  if (!$messages) {
187  return [];
188  }
189  $messages = $this->serializer->unserialize($messages);
190  if (!is_array($messages)) {
191  $messages = [];
192  }
193  return $messages;
194  }
195 }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$message
__construct(\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy, \Magento\Framework\Serialize\Serializer\Json $serializer=null, InlineInterface $inlineTranslate=null)
afterRenderResult(ResultInterface $subject, ResultInterface $result)