Skip to content

Commit 3c0c8c7

Browse files
authored
Merge pull request #718 from magento-troll/Sprint55
Fixed issues: - MAGETWO-60418 Remove uses of serialize/unserialize in \Magento\Framework\App\PageCache\Kernel - MAGETWO-60423 Remove uses of serialize/unserialize in \Magento\Framework\View\Layout - MAGETWO-60419 Remove uses of serialize/unserialize in \Magento\Ui\Model\Manager - MAGETWO-62919 Update the year in the copyright
2 parents dd124b7 + 9d56fc6 commit 3c0c8c7

File tree

10 files changed

+563
-67
lines changed

10 files changed

+563
-67
lines changed

app/code/Magento/Ui/Model/Manager.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Magento. All rights reserved.
3+
* Copyright © 2017 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Ui\Model;
@@ -17,6 +17,8 @@
1717
use Magento\Framework\View\Element\UiComponent\Config\Provider\Component\Definition as ComponentDefinition;
1818
use Magento\Framework\View\Element\UiComponent\Config\ReaderFactory;
1919
use Magento\Framework\View\Element\UiComponent\Config\UiReaderInterface;
20+
use Magento\Framework\Serialize\SerializerInterface;
21+
use Magento\Framework\App\ObjectManager;
2022

2123
/**
2224
* Class Manager
@@ -94,6 +96,11 @@ class Manager implements ManagerInterface
9496
*/
9597
protected $uiReader;
9698

99+
/**
100+
* @var SerializerInterface
101+
*/
102+
private $serializer;
103+
97104
/**
98105
* @param ComponentDefinition $componentConfigProvider
99106
* @param DomMergerInterface $domMerger
@@ -102,6 +109,7 @@ class Manager implements ManagerInterface
102109
* @param AggregatedFileCollectorFactory $aggregatedFileCollectorFactory
103110
* @param CacheInterface $cache
104111
* @param InterpreterInterface $argumentInterpreter
112+
* @param SerializerInterface|null $serializer
105113
*/
106114
public function __construct(
107115
ComponentDefinition $componentConfigProvider,
@@ -110,7 +118,8 @@ public function __construct(
110118
ArrayObjectFactory $arrayObjectFactory,
111119
AggregatedFileCollectorFactory $aggregatedFileCollectorFactory,
112120
CacheInterface $cache,
113-
InterpreterInterface $argumentInterpreter
121+
InterpreterInterface $argumentInterpreter,
122+
SerializerInterface $serializer = null
114123
) {
115124
$this->componentConfigProvider = $componentConfigProvider;
116125
$this->domMerger = $domMerger;
@@ -120,6 +129,7 @@ public function __construct(
120129
$this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory;
121130
$this->cache = $cache;
122131
$this->argumentInterpreter = $argumentInterpreter;
132+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
123133
}
124134

125135
/**
@@ -164,9 +174,14 @@ public function prepareData($name)
164174
$cachedPool = $this->cache->load($cacheID);
165175
if ($cachedPool === false) {
166176
$this->prepare($name);
167-
$this->cache->save($this->componentsPool->serialize(), $cacheID);
177+
$this->cache->save(
178+
$this->serializer->serialize($this->componentsPool->getArrayCopy()),
179+
$cacheID
180+
);
168181
} else {
169-
$this->componentsPool->unserialize($cachedPool);
182+
$this->componentsPool->exchangeArray(
183+
$this->serializer->unserialize($cachedPool)
184+
);
170185
}
171186
$this->componentsData->offsetSet($name, $this->componentsPool);
172187
$this->componentsData->offsetSet($name, $this->evaluateComponentArguments($this->getData($name)));

app/code/Magento/Ui/Test/Unit/Model/ManagerTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Magento. All rights reserved.
3+
* Copyright © 2017 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66

@@ -75,6 +75,9 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
7575
*/
7676
protected $aggregatedFileCollectorFactory;
7777

78+
/** @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
79+
private $serializer;
80+
7881
protected function setUp()
7982
{
8083
$this->componentConfigProvider = $this->getMockBuilder(
@@ -105,14 +108,33 @@ protected function setUp()
105108
->getMockForAbstractClass();
106109
$this->argumentInterpreter = $this->getMockBuilder(\Magento\Framework\Data\Argument\InterpreterInterface::class)
107110
->getMockForAbstractClass();
111+
$this->serializer = $this->getMockBuilder(
112+
\Magento\Framework\Serialize\SerializerInterface::class
113+
)->getMockForAbstractClass();
114+
$this->serializer->expects($this->any())
115+
->method('serialize')
116+
->willReturnCallback(
117+
function ($value) {
118+
return json_encode($value);
119+
}
120+
);
121+
$this->serializer->expects($this->any())
122+
->method('unserialize')
123+
->willReturnCallback(
124+
function ($value) {
125+
return json_decode($value, true);
126+
}
127+
);
128+
108129
$this->manager = new Manager(
109130
$this->componentConfigProvider,
110131
$this->domMerger,
111132
$this->readerFactory,
112133
$this->arrayObjectFactory,
113134
$this->aggregatedFileCollectorFactory,
114135
$this->cacheConfig,
115-
$this->argumentInterpreter
136+
$this->argumentInterpreter,
137+
$this->serializer
116138
);
117139
}
118140

@@ -192,7 +214,7 @@ public function getComponentData()
192214
[
193215
'test_component1',
194216
new \ArrayObject(),
195-
$cachedData->serialize(),
217+
json_encode($cachedData->getArrayCopy()),
196218
[],
197219
[
198220
'test_component1' => [

lib/internal/Magento/Framework/App/Http/Context.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Magento. All rights reserved.
3+
* Copyright © 2017 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Framework\App\Http;
@@ -27,6 +27,16 @@ class Context
2727
*/
2828
protected $default = [];
2929

30+
/**
31+
* @param array $data
32+
* @param array $default
33+
*/
34+
public function __construct(array $data = [], array $default = [])
35+
{
36+
$this->data = $data;
37+
$this->default = $default;
38+
}
39+
3040
/**
3141
* Data setter
3242
*
@@ -99,4 +109,17 @@ public function getVaryString()
99109
}
100110
return null;
101111
}
112+
113+
/**
114+
* Get data and default data in "key-value" format
115+
*
116+
* @return array
117+
*/
118+
public function toArray()
119+
{
120+
return [
121+
'data' => $this->data,
122+
'default' => $this->default
123+
];
124+
}
102125
}

lib/internal/Magento/Framework/App/PageCache/Kernel.php

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22
/**
3-
* Copyright © 2016 Magento. All rights reserved.
3+
* Copyright © 2017 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Framework\App\PageCache;
77

8-
use Magento\Framework\App\ObjectManager;
9-
108
/**
119
* Builtin cache processor
1210
*/
@@ -34,19 +32,76 @@ class Kernel
3432
*/
3533
private $fullPageCache;
3634

35+
/**
36+
* @var \Magento\Framework\Serialize\SerializerInterface
37+
*/
38+
private $serializer;
39+
40+
/**
41+
* @var \Magento\Framework\App\Http\Context
42+
*/
43+
private $context;
44+
45+
/**
46+
* @var \Magento\Framework\App\Http\ContextFactory
47+
*/
48+
private $contextFactory;
49+
50+
/**
51+
* @var \Magento\Framework\App\Response\HttpFactory
52+
*/
53+
private $httpFactory;
54+
3755
/**
3856
* @param Cache $cache
3957
* @param Identifier $identifier
4058
* @param \Magento\Framework\App\Request\Http $request
59+
* @param \Magento\Framework\App\Http\Context|null $context
60+
* @param \Magento\Framework\App\Http\ContextFactory|null $contextFactory
61+
* @param \Magento\Framework\App\Response\HttpFactory|null $httpFactory
62+
* @param \Magento\Framework\Serialize\SerializerInterface|null $serializer
4163
*/
4264
public function __construct(
4365
\Magento\Framework\App\PageCache\Cache $cache,
4466
\Magento\Framework\App\PageCache\Identifier $identifier,
45-
\Magento\Framework\App\Request\Http $request
67+
\Magento\Framework\App\Request\Http $request,
68+
\Magento\Framework\App\Http\Context $context = null,
69+
\Magento\Framework\App\Http\ContextFactory $contextFactory = null,
70+
\Magento\Framework\App\Response\HttpFactory $httpFactory = null,
71+
\Magento\Framework\Serialize\SerializerInterface $serializer = null
4672
) {
4773
$this->cache = $cache;
4874
$this->identifier = $identifier;
4975
$this->request = $request;
76+
77+
if ($context) {
78+
$this->context = $context;
79+
} else {
80+
$this->context = \Magento\Framework\App\ObjectManager::getInstance()->get(
81+
\Magento\Framework\App\Http\Context::class
82+
);
83+
}
84+
if ($contextFactory) {
85+
$this->contextFactory = $contextFactory;
86+
} else {
87+
$this->contextFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
88+
\Magento\Framework\App\Http\ContextFactory::class
89+
);
90+
}
91+
if ($httpFactory) {
92+
$this->httpFactory = $httpFactory;
93+
} else {
94+
$this->httpFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
95+
\Magento\Framework\App\Response\HttpFactory::class
96+
);
97+
}
98+
if ($serializer) {
99+
$this->serializer = $serializer;
100+
} else {
101+
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(
102+
\Magento\Framework\Serialize\SerializerInterface::class
103+
);
104+
}
50105
}
51106

52107
/**
@@ -57,7 +112,12 @@ public function __construct(
57112
public function load()
58113
{
59114
if ($this->request->isGet() || $this->request->isHead()) {
60-
return unserialize($this->getCache()->load($this->identifier->getValue()));
115+
$responseData = $this->serializer->unserialize($this->getCache()->load($this->identifier->getValue()));
116+
if (!$responseData) {
117+
return false;
118+
}
119+
120+
return $this->buildResponse($responseData);
61121
}
62122
return false;
63123
}
@@ -84,11 +144,63 @@ public function process(\Magento\Framework\App\Response\Http $response)
84144
if (!headers_sent()) {
85145
header_remove('Set-Cookie');
86146
}
87-
$this->getCache()->save(serialize($response), $this->identifier->getValue(), $tags, $maxAge);
147+
148+
$this->getCache()->save(
149+
$this->serializer->serialize($this->getPreparedData($response)),
150+
$this->identifier->getValue(),
151+
$tags,
152+
$maxAge
153+
);
88154
}
89155
}
90156
}
91157

158+
/**
159+
* Get prepared data for storage in the cache.
160+
*
161+
* @param \Magento\Framework\App\Response\Http $response
162+
* @return array
163+
*/
164+
private function getPreparedData(\Magento\Framework\App\Response\Http $response)
165+
{
166+
return [
167+
'content' => $response->getContent(),
168+
'status_code' => $response->getStatusCode(),
169+
'headers' => $response->getHeaders()->toArray(),
170+
'context' => $this->context->toArray()
171+
];
172+
173+
}
174+
175+
/**
176+
* Build response using response data.
177+
*
178+
* @param array $responseData
179+
* @return \Magento\Framework\App\Response\Http
180+
*/
181+
private function buildResponse($responseData)
182+
{
183+
$context = $this->contextFactory->create(
184+
[
185+
'data' => $responseData['context']['data'],
186+
'default' => $responseData['context']['default']
187+
]
188+
);
189+
190+
$response = $this->httpFactory->create(
191+
[
192+
'context' => $context
193+
]
194+
);
195+
$response->setStatusCode($responseData['status_code']);
196+
$response->setContent($responseData['content']);
197+
foreach ($responseData['headers'] as $headerKey => $headerValue) {
198+
$response->setHeader($headerKey, $headerValue, true);
199+
}
200+
201+
return $response;
202+
}
203+
92204
/**
93205
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
94206
*
@@ -97,7 +209,9 @@ public function process(\Magento\Framework\App\Response\Http $response)
97209
private function getCache()
98210
{
99211
if (!$this->fullPageCache) {
100-
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class);
212+
$this->fullPageCache = \Magento\Framework\App\ObjectManager::getInstance()->get(
213+
\Magento\PageCache\Model\Cache\Type::class
214+
);
101215
}
102216
return $this->fullPageCache;
103217
}

lib/internal/Magento/Framework/App/Test/Unit/Http/ContextTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Magento. All rights reserved.
3+
* Copyright © 2017 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66

@@ -64,4 +64,19 @@ public function testGetVaryString()
6464
ksort($data);
6565
$this->assertEquals(sha1(serialize($data)), $this->object->getVaryString());
6666
}
67+
68+
public function testToArray()
69+
{
70+
$newObject = new \Magento\Framework\App\Http\Context(['key' => 'value']);
71+
72+
$newObject->setValue('key1', 'value1', 'default1');
73+
$newObject->setValue('key2', 'value2', 'default2');
74+
$this->assertEquals(
75+
[
76+
'data' => ['key' => 'value', 'key1' => 'value1', 'key2' => 'value2'],
77+
'default' => ['key1' => 'default1', 'key2' => 'default2']
78+
],
79+
$newObject->toArray()
80+
);
81+
}
6782
}

0 commit comments

Comments
 (0)