Skip to content

Commit 331f81e

Browse files
Merge branch 'MDVA-373' into 2-0-8-backlog
2 parents 9d8048f + e49975d commit 331f81e

File tree

11 files changed

+799
-106
lines changed

11 files changed

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

0 commit comments

Comments
 (0)