|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Theme\Controller\Result; |
| 7 | + |
| 8 | +use Magento\Framework\Controller\Result\Json; |
| 9 | +use Magento\Framework\Controller\ResultInterface; |
| 10 | +use Magento\Framework\Json\Helper\Data; |
| 11 | +use Magento\Framework\Message\ManagerInterface; |
| 12 | +use Magento\Framework\Message\MessageInterface; |
| 13 | +use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory; |
| 14 | +use Magento\Framework\Stdlib\CookieManagerInterface; |
| 15 | +use Magento\Framework\View\Element\Message\InterpretationStrategyInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin for putting messages to cookies |
| 19 | + */ |
| 20 | +class MessagePlugin |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Cookies name for messages |
| 24 | + */ |
| 25 | + const MESSAGES_COOKIES_NAME = 'mage-messages'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CookieManagerInterface |
| 29 | + */ |
| 30 | + private $cookieManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CookieMetadataFactory |
| 34 | + */ |
| 35 | + private $cookieMetadataFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ManagerInterface |
| 39 | + */ |
| 40 | + private $messageManager; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var InterpretationStrategyInterface |
| 44 | + */ |
| 45 | + private $interpretationStrategy; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Data |
| 49 | + */ |
| 50 | + private $jsonHelper; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param CookieManagerInterface $cookieManager |
| 54 | + * @param CookieMetadataFactory $cookieMetadataFactory |
| 55 | + * @param ManagerInterface $messageManager |
| 56 | + * @param InterpretationStrategyInterface $interpretationStrategy |
| 57 | + * @param Data $jsonHelper |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + CookieManagerInterface $cookieManager, |
| 61 | + CookieMetadataFactory $cookieMetadataFactory, |
| 62 | + ManagerInterface $messageManager, |
| 63 | + InterpretationStrategyInterface $interpretationStrategy, |
| 64 | + Data $jsonHelper |
| 65 | + ) { |
| 66 | + $this->cookieManager = $cookieManager; |
| 67 | + $this->cookieMetadataFactory = $cookieMetadataFactory; |
| 68 | + $this->messageManager = $messageManager; |
| 69 | + $this->jsonHelper = $jsonHelper; |
| 70 | + $this->interpretationStrategy = $interpretationStrategy; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param ResultInterface $subject |
| 75 | + * @param ResultInterface $result |
| 76 | + * @return ResultInterface |
| 77 | + */ |
| 78 | + public function afterRenderResult( |
| 79 | + ResultInterface $subject, |
| 80 | + ResultInterface $result |
| 81 | + ) { |
| 82 | + if (!($subject instanceof Json)) { |
| 83 | + $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); |
| 84 | + $publicCookieMetadata->setDurationOneYear(); |
| 85 | + $publicCookieMetadata->setPath('/'); |
| 86 | + $publicCookieMetadata->setHttpOnly(false); |
| 87 | + $this->cookieManager->setPublicCookie( |
| 88 | + self::MESSAGES_COOKIES_NAME, |
| 89 | + $this->jsonHelper->jsonEncode($this->getMessages()), |
| 90 | + $publicCookieMetadata |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + return $result; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Return messages array and clean message manager messages |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + protected function getMessages() |
| 103 | + { |
| 104 | + $messages = $this->getCookiesMessages(); |
| 105 | + /** @var MessageInterface $message */ |
| 106 | + foreach ($this->messageManager->getMessages(true)->getItems() as $message) { |
| 107 | + $messages []= [ |
| 108 | + 'type' => $message->getType(), |
| 109 | + 'text' => $this->interpretationStrategy->interpret($message), |
| 110 | + ]; |
| 111 | + } |
| 112 | + return $messages; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Return messages stored in cookies |
| 117 | + * |
| 118 | + * @return array |
| 119 | + */ |
| 120 | + protected function getCookiesMessages() |
| 121 | + { |
| 122 | + try { |
| 123 | + $messages = $this->jsonHelper->jsonDecode( |
| 124 | + $this->cookieManager->getCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode([])) |
| 125 | + ); |
| 126 | + if (!is_array($messages)) { |
| 127 | + $messages = []; |
| 128 | + } |
| 129 | + } catch (\Zend_Json_Exception $e) { |
| 130 | + $messages = []; |
| 131 | + } |
| 132 | + return $messages; |
| 133 | + } |
| 134 | +} |
0 commit comments