Skip to content

Commit 5a0aa91

Browse files
author
Oleksii Korshenko
authored
MAGETWO-67511: Remove zend json from theme #9262
2 parents beeae93 + a43dae3 commit 5a0aa91

File tree

6 files changed

+175
-79
lines changed

6 files changed

+175
-79
lines changed

app/code/Magento/Theme/Block/Adminhtml/Wysiwyg/Files/Tree.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,30 @@ class Tree extends \Magento\Backend\Block\Template
2222
*/
2323
protected $urlEncoder;
2424

25+
/**
26+
* @var \Magento\Framework\Serialize\Serializer\Json
27+
*/
28+
private $serializer;
29+
2530
/**
2631
* @param \Magento\Backend\Block\Template\Context $context
2732
* @param \Magento\Theme\Helper\Storage $storageHelper
2833
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
2934
* @param array $data
35+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
36+
* @throws \RuntimeException
3037
*/
3138
public function __construct(
3239
\Magento\Backend\Block\Template\Context $context,
3340
\Magento\Theme\Helper\Storage $storageHelper,
3441
\Magento\Framework\Url\EncoderInterface $urlEncoder,
35-
array $data = []
42+
array $data = [],
43+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
3644
) {
3745
$this->_storageHelper = $storageHelper;
3846
$this->urlEncoder = $urlEncoder;
47+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
48+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3949
parent::__construct($context, $data);
4050
}
4151

@@ -57,7 +67,7 @@ public function getTreeLoaderUrl()
5767
*/
5868
public function getTreeJson($data)
5969
{
60-
return \Zend_Json::encode($data);
70+
return $this->serializer->serialize($data);
6171
}
6272

6373
/**

app/code/Magento/Theme/Controller/Result/MessagePlugin.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,29 @@ class MessagePlugin
4040
private $interpretationStrategy;
4141

4242
/**
43-
* @var \Magento\Framework\Json\Helper\Data
43+
* @var \Magento\Framework\Serialize\Serializer\Json
4444
*/
45-
private $jsonHelper;
45+
private $serializer;
4646

4747
/**
4848
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
4949
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
5050
* @param \Magento\Framework\Message\ManagerInterface $messageManager
5151
* @param \Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy
52-
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
52+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
5353
*/
5454
public function __construct(
5555
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
5656
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
5757
\Magento\Framework\Message\ManagerInterface $messageManager,
5858
\Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
59-
\Magento\Framework\Json\Helper\Data $jsonHelper
59+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
6060
) {
6161
$this->cookieManager = $cookieManager;
6262
$this->cookieMetadataFactory = $cookieMetadataFactory;
6363
$this->messageManager = $messageManager;
64-
$this->jsonHelper = $jsonHelper;
64+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
65+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
6566
$this->interpretationStrategy = $interpretationStrategy;
6667
}
6768

@@ -118,7 +119,7 @@ private function setCookie(array $messages)
118119

119120
$this->cookieManager->setPublicCookie(
120121
self::MESSAGES_COOKIES_NAME,
121-
$this->jsonHelper->jsonEncode($messages),
122+
$this->serializer->serialize($messages),
122123
$publicCookieMetadata
123124
);
124125
}
@@ -149,14 +150,13 @@ protected function getMessages()
149150
*/
150151
protected function getCookiesMessages()
151152
{
152-
try {
153-
$messages = $this->jsonHelper->jsonDecode(
154-
$this->cookieManager->getCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode([]))
155-
);
156-
if (!is_array($messages)) {
157-
$messages = [];
158-
}
159-
} catch (\Zend_Json_Exception $e) {
153+
$messages = $this->serializer->unserialize(
154+
$this->cookieManager->getCookie(
155+
self::MESSAGES_COOKIES_NAME,
156+
$this->serializer->serialize([])
157+
)
158+
);
159+
if (!is_array($messages)) {
160160
$messages = [];
161161
}
162162
return $messages;

app/code/Magento/Theme/Model/Theme/ThemePackageInfo.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,33 @@ class ThemePackageInfo
2929
private $packageNameToFullPathMap = [];
3030

3131
/**
32-
* Constructor
32+
* @var \Magento\Framework\Serialize\Serializer\Json
33+
*/
34+
private $serializer;
35+
36+
/**
37+
* Initialize dependencies.
3338
*
3439
* @param ComponentRegistrar $componentRegistrar
3540
* @param ReadFactory $readDirFactory
41+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
3642
*/
3743
public function __construct(
3844
ComponentRegistrar $componentRegistrar,
39-
ReadFactory $readDirFactory
45+
ReadFactory $readDirFactory,
46+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
4047
) {
4148
$this->componentRegistrar = $componentRegistrar;
4249
$this->readDirFactory = $readDirFactory;
50+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
51+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
4352
}
4453

4554
/**
4655
* Get package name of a theme by its full theme path
4756
*
4857
* @param string $themePath
4958
* @return string
50-
* @throws \Zend_Json_Exception
5159
*/
5260
public function getPackageName($themePath)
5361
{
@@ -57,7 +65,7 @@ public function getPackageName($themePath)
5765
$rawData = [];
5866
$themeFile = $themeDir->readFile('composer.json');
5967
if ($themeFile) {
60-
$rawData = \Zend_Json::decode($themeFile);
68+
$rawData = $this->serializer->unserialize($themeFile);
6169
}
6270
return isset($rawData['name']) ? $rawData['name'] : '';
6371
}
@@ -83,7 +91,6 @@ public function getFullThemePath($packageName)
8391
* Initialize package name to full theme path map
8492
*
8593
* @return void
86-
* @throws \Zend_Json_Exception
8794
*/
8895
private function initializeMap()
8996
{
@@ -92,7 +99,7 @@ private function initializeMap()
9299
foreach ($themePaths as $fullThemePath => $themeDir) {
93100
$themeDirRead = $this->readDirFactory->create($themeDir);
94101
if ($themeDirRead->isExist('composer.json')) {
95-
$rawData = \Zend_Json::decode($themeDirRead->readFile('composer.json'));
102+
$rawData = $this->serializer->unserialize($themeDirRead->readFile('composer.json'));
96103
if (isset($rawData['name'])) {
97104
$this->packageNameToFullPathMap[$rawData['name']] = $fullThemePath;
98105
}

app/code/Magento/Theme/Test/Unit/Controller/Result/MessagePluginTest.php

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class MessagePluginTest extends \PHPUnit_Framework_TestCase
3737
/** @var InterpretationStrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
3838
protected $interpretationStrategyMock;
3939

40-
/** @var Data|\PHPUnit_Framework_MockObject_MockObject */
41-
protected $dataMock;
40+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
41+
private $serializerMock;
4242

4343
protected function setUp()
4444
{
@@ -51,16 +51,15 @@ protected function setUp()
5151
->getMockForAbstractClass();
5252
$this->interpretationStrategyMock = $this->getMockBuilder(InterpretationStrategyInterface::class)
5353
->getMockForAbstractClass();
54-
$this->dataMock = $this->getMockBuilder(Data::class)
55-
->disableOriginalConstructor()
54+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
5655
->getMock();
5756

5857
$this->model = new MessagePlugin(
5958
$this->cookieManagerMock,
6059
$this->cookieMetadataFactoryMock,
6160
$this->managerMock,
6261
$this->interpretationStrategyMock,
63-
$this->dataMock
62+
$this->serializerMock
6463
);
6564
}
6665

@@ -113,29 +112,29 @@ public function testAfterRenderResult()
113112
->method('setPublicCookie')
114113
->with(
115114
MessagePlugin::MESSAGES_COOKIES_NAME,
116-
\Zend_Json::encode($messages),
115+
json_encode($messages),
117116
$cookieMetadataMock
118117
);
119118
$this->cookieManagerMock->expects($this->once())
120119
->method('getCookie')
121120
->with(
122121
MessagePlugin::MESSAGES_COOKIES_NAME,
123-
\Zend_Json::encode([])
122+
json_encode([])
124123
)
125-
->willReturn(\Zend_Json::encode($existingMessages));
124+
->willReturn(json_encode($existingMessages));
126125

127-
$this->dataMock->expects($this->once())
128-
->method('jsonDecode')
126+
$this->serializerMock->expects($this->once())
127+
->method('unserialize')
129128
->willReturnCallback(
130129
function ($data) {
131-
return \Zend_Json::decode($data);
130+
return json_decode($data, true);
132131
}
133132
);
134-
$this->dataMock->expects($this->exactly(2))
135-
->method('jsonEncode')
133+
$this->serializerMock->expects($this->exactly(2))
134+
->method('serialize')
136135
->willReturnCallback(
137136
function ($data) {
138-
return \Zend_Json::encode($data);
137+
return json_encode($data);
139138
}
140139
);
141140

@@ -178,22 +177,22 @@ public function testAfterRenderResultWithNoMessages()
178177
->method('getCookie')
179178
->with(
180179
MessagePlugin::MESSAGES_COOKIES_NAME,
181-
\Zend_Json::encode([])
180+
json_encode([])
182181
)
183-
->willReturn(\Zend_Json::encode([]));
182+
->willReturn(json_encode([]));
184183

185-
$this->dataMock->expects($this->once())
186-
->method('jsonDecode')
184+
$this->serializerMock->expects($this->once())
185+
->method('unserialize')
187186
->willReturnCallback(
188187
function ($data) {
189-
return \Zend_Json::decode($data);
188+
return json_decode($data, true);
190189
}
191190
);
192-
$this->dataMock->expects($this->once())
193-
->method('jsonEncode')
191+
$this->serializerMock->expects($this->once())
192+
->method('serialize')
194193
->willReturnCallback(
195194
function ($data) {
196-
return \Zend_Json::encode($data);
195+
return json_encode($data);
197196
}
198197
);
199198

@@ -246,29 +245,29 @@ public function testAfterRenderResultWithoutExisting()
246245
->method('setPublicCookie')
247246
->with(
248247
MessagePlugin::MESSAGES_COOKIES_NAME,
249-
\Zend_Json::encode($messages),
248+
json_encode($messages),
250249
$cookieMetadataMock
251250
);
252251
$this->cookieManagerMock->expects($this->once())
253252
->method('getCookie')
254253
->with(
255254
MessagePlugin::MESSAGES_COOKIES_NAME,
256-
\Zend_Json::encode([])
255+
json_encode([])
257256
)
258-
->willReturn(\Zend_Json::encode([]));
257+
->willReturn(json_encode([]));
259258

260-
$this->dataMock->expects($this->any())
261-
->method('jsonDecode')
259+
$this->serializerMock->expects($this->once())
260+
->method('unserialize')
262261
->willReturnCallback(
263262
function ($data) {
264-
return \Zend_Json::decode($data);
263+
return json_decode($data, true);
265264
}
266265
);
267-
$this->dataMock->expects($this->any())
268-
->method('jsonEncode')
266+
$this->serializerMock->expects($this->exactly(2))
267+
->method('serialize')
269268
->willReturnCallback(
270269
function ($data) {
271-
return \Zend_Json::encode($data);
270+
return json_encode($data);
272271
}
273272
);
274273

@@ -329,25 +328,29 @@ public function testAfterRenderResultWithWrongJson()
329328
->method('setPublicCookie')
330329
->with(
331330
MessagePlugin::MESSAGES_COOKIES_NAME,
332-
\Zend_Json::encode($messages),
331+
json_encode($messages),
333332
$cookieMetadataMock
334333
);
335334
$this->cookieManagerMock->expects($this->once())
336335
->method('getCookie')
337336
->with(
338337
MessagePlugin::MESSAGES_COOKIES_NAME,
339-
\Zend_Json::encode([])
338+
json_encode([])
340339
)
341-
->willReturn(\Zend_Json::encode([]));
340+
->willReturn(json_encode([]));
342341

343-
$this->dataMock->expects($this->any())
344-
->method('jsonDecode')
345-
->willThrowException(new \Zend_Json_Exception);
346-
$this->dataMock->expects($this->any())
347-
->method('jsonEncode')
342+
$this->serializerMock->expects($this->once())
343+
->method('unserialize')
344+
->willReturnCallback(
345+
function ($data) {
346+
return json_decode($data, true);
347+
}
348+
);
349+
$this->serializerMock->expects($this->exactly(2))
350+
->method('serialize')
348351
->willReturnCallback(
349352
function ($data) {
350-
return \Zend_Json::encode($data);
353+
return json_encode($data);
351354
}
352355
);
353356

@@ -408,29 +411,29 @@ public function testAfterRenderResultWithWrongArray()
408411
->method('setPublicCookie')
409412
->with(
410413
MessagePlugin::MESSAGES_COOKIES_NAME,
411-
\Zend_Json::encode($messages),
414+
json_encode($messages),
412415
$cookieMetadataMock
413416
);
414417
$this->cookieManagerMock->expects($this->once())
415418
->method('getCookie')
416419
->with(
417420
MessagePlugin::MESSAGES_COOKIES_NAME,
418-
\Zend_Json::encode([])
421+
json_encode([])
419422
)
420-
->willReturn(\Zend_Json::encode('string'));
423+
->willReturn(json_encode('string'));
421424

422-
$this->dataMock->expects($this->any())
423-
->method('jsonDecode')
425+
$this->serializerMock->expects($this->once())
426+
->method('unserialize')
424427
->willReturnCallback(
425428
function ($data) {
426-
return \Zend_Json::decode($data);
429+
return json_decode($data, true);
427430
}
428431
);
429-
$this->dataMock->expects($this->any())
430-
->method('jsonEncode')
432+
$this->serializerMock->expects($this->exactly(2))
433+
->method('serialize')
431434
->willReturnCallback(
432435
function ($data) {
433-
return \Zend_Json::encode($data);
436+
return json_encode($data);
434437
}
435438
);
436439

0 commit comments

Comments
 (0)