Skip to content

Commit 943804e

Browse files
committed
Merge branch 'MAGETWO-60418' of github.com:magento-troll/magento2ce into Sprint55
2 parents 5f76007 + 69a0ee4 commit 943804e

File tree

4 files changed

+285
-33
lines changed

4 files changed

+285
-33
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)