5
5
*/
6
6
namespace Magento \Framework \App \PageCache ;
7
7
8
- use Magento \Framework \App \ObjectManager ;
9
-
10
8
/**
11
9
* Builtin cache processor
12
10
*/
@@ -34,19 +32,76 @@ class Kernel
34
32
*/
35
33
private $ fullPageCache ;
36
34
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
+
37
55
/**
38
56
* @param Cache $cache
39
57
* @param Identifier $identifier
40
58
* @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
41
63
*/
42
64
public function __construct (
43
65
\Magento \Framework \App \PageCache \Cache $ cache ,
44
66
\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
46
72
) {
47
73
$ this ->cache = $ cache ;
48
74
$ this ->identifier = $ identifier ;
49
75
$ 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
+ }
50
105
}
51
106
52
107
/**
@@ -57,7 +112,12 @@ public function __construct(
57
112
public function load ()
58
113
{
59
114
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 );
61
121
}
62
122
return false ;
63
123
}
@@ -84,11 +144,63 @@ public function process(\Magento\Framework\App\Response\Http $response)
84
144
if (!headers_sent ()) {
85
145
header_remove ('Set-Cookie ' );
86
146
}
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
+ );
88
154
}
89
155
}
90
156
}
91
157
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
+
92
204
/**
93
205
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
94
206
*
@@ -97,7 +209,9 @@ public function process(\Magento\Framework\App\Response\Http $response)
97
209
private function getCache ()
98
210
{
99
211
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
+ );
101
215
}
102
216
return $ this ->fullPageCache ;
103
217
}
0 commit comments