11
11
use Magento \Framework \App \ObjectManager ;
12
12
use Magento \Framework \UrlInterface ;
13
13
use Magento \Framework \AuthorizationInterface ;
14
+ use Magento \Framework \Cache \FrontendInterface ;
15
+ use Magento \Framework \Serialize \Serializer \Json ;
14
16
15
17
/**
16
18
* Provide configuration to the admin JavaScript app
@@ -32,6 +34,10 @@ class Config
32
34
const TEMPLATE_SAVE_RESOURCE = 'Magento_PageBuilder::template_save ' ;
33
35
const TEMPLATE_APPLY_RESOURCE = 'Magento_PageBuilder::template_apply ' ;
34
36
37
+ private const CONTENT_TYPE_CACHE_ID = 'CONTENT_TYPE ' ;
38
+ private const TINY_MCE_CONFIG_CACHE_ID = 'TYNY_MCE_CONFIG ' ;
39
+ private const WIDGET_BREAKPOINS_CACHE_ID = 'WIDGET_BREAKPOINS ' ;
40
+
35
41
/**
36
42
* @var \Magento\PageBuilder\Model\ConfigInterface
37
43
*/
@@ -102,6 +108,16 @@ class Config
102
108
*/
103
109
private $ authorization ;
104
110
111
+ /**
112
+ * @var FrontendInterface
113
+ */
114
+ private $ cache ;
115
+
116
+ /**
117
+ * @var Json
118
+ */
119
+ private $ serializer ;
120
+
105
121
/**
106
122
* @param \Magento\PageBuilder\Model\ConfigInterface $config
107
123
* @param Config\UiComponentConfig $uiComponentConfig
@@ -117,6 +133,8 @@ class Config
117
133
* @param \Magento\Widget\Model\Widget\Config|null $widgetConfig
118
134
* @param \Magento\Variable\Model\Variable\Config|null $variableConfig
119
135
* @param AuthorizationInterface|null $authorization
136
+ * @param FrontendInterface|null $cache
137
+ * @param Json|null $serializer
120
138
*
121
139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
122
140
*/
@@ -134,7 +152,9 @@ public function __construct(
134
152
array $ data = [],
135
153
\Magento \Widget \Model \Widget \Config $ widgetConfig = null ,
136
154
\Magento \Variable \Model \Variable \Config $ variableConfig = null ,
137
- AuthorizationInterface $ authorization = null
155
+ AuthorizationInterface $ authorization = null ,
156
+ FrontendInterface $ cache = null ,
157
+ Json $ serializer = null
138
158
) {
139
159
$ this ->config = $ config ;
140
160
$ this ->uiComponentConfig = $ uiComponentConfig ;
@@ -152,6 +172,8 @@ public function __construct(
152
172
$ this ->variableConfig = $ variableConfig ?? \Magento \Framework \App \ObjectManager::getInstance ()
153
173
->get (\Magento \Variable \Model \Variable \Config::class);
154
174
$ this ->authorization = $ authorization ?: ObjectManager::getInstance ()->get (AuthorizationInterface::class);
175
+ $ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Json::class);
176
+ $ this ->cache = $ cache ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (FrontendInterface::class);
155
177
}
156
178
157
179
/**
@@ -173,8 +195,8 @@ public function getConfig()
173
195
'column_grid_max ' => $ this ->scopeConfig ->getValue (self ::XML_PATH_COLUMN_GRID_MAX ),
174
196
'can_use_inline_editing_on_stage ' => $ this ->isWysiwygProvisionedForEditingOnStage (),
175
197
'widgets ' => $ this ->widgetInitializerConfig ->getConfig (),
176
- 'breakpoints ' => $ this ->widgetInitializerConfig -> getBreakpoints (),
177
- 'tinymce ' => $ this ->getTinyMceConfig (),
198
+ 'breakpoints ' => $ this ->getCachedWidgetBreakpoints (),
199
+ 'tinymce ' => $ this ->getCachedTinyMceConfig (),
178
200
'acl ' => $ this ->getAcl ()
179
201
];
180
202
}
@@ -243,21 +265,45 @@ private function getContentTypes()
243
265
244
266
$ contentTypeData = [];
245
267
foreach ($ contentTypes as $ name => $ contentType ) {
246
- $ contentTypeData [$ name ] = $ this ->flattenContentTypeData (
268
+ $ contentTypeData [$ name ] = $ this ->getCachedFlattenContentTypeData (
247
269
$ name ,
248
270
$ contentType
249
271
);
250
272
}
251
273
252
274
// The stage requires a root container to house it's children
253
- $ contentTypeData [self ::ROOT_CONTAINER_NAME ] = $ this ->flattenContentTypeData (
275
+ $ contentTypeData [self ::ROOT_CONTAINER_NAME ] = $ this ->getCachedFlattenContentTypeData (
254
276
self ::ROOT_CONTAINER_NAME ,
255
277
$ this ->rootContainerConfig
256
278
);
257
279
258
280
return $ contentTypeData ;
259
281
}
260
282
283
+ /**
284
+ * Get flatten content type for content name from cache and add it to cache if wasn't cached
285
+ *
286
+ * @param string $name
287
+ * @param array $contentType
288
+ *
289
+ * @return array
290
+ */
291
+ private function getCachedFlattenContentTypeData (string $ name , array $ contentType )
292
+ {
293
+ $ identifier = self ::CONTENT_TYPE_CACHE_ID . '_ ' . $ name ;
294
+
295
+ $ flattenContentTypeData = $ this ->getCache ($ identifier );
296
+ if (empty ($ flattenContentTypeData )) {
297
+ $ flattenContentTypeData = $ this ->flattenContentTypeData (
298
+ $ name ,
299
+ $ contentType
300
+ );
301
+ $ this ->saveCache ($ flattenContentTypeData , $ identifier );
302
+ }
303
+
304
+ return $ flattenContentTypeData ;
305
+ }
306
+
261
307
/**
262
308
* Flatten the content type
263
309
*
@@ -308,4 +354,57 @@ private function isWysiwygProvisionedForEditingOnStage()
308
354
309
355
return $ this ->inlineEditingChecker ->isSupported ($ activeEditorPath );
310
356
}
357
+
358
+ /**
359
+ * Get the TINY MCE config from cache and add it to cache if it wasn't cached
360
+ *
361
+ * @return array
362
+ */
363
+ private function getCachedTinyMceConfig (): array
364
+ {
365
+ $ configData = $ this ->getCache (self ::TINY_MCE_CONFIG_CACHE_ID );
366
+ if (empty ($ configData )) {
367
+ $ configData = $ this ->getTinyMceConfig ();
368
+ $ this ->saveCache ($ configData , self ::TINY_MCE_CONFIG_CACHE_ID );
369
+ }
370
+ return $ configData ;
371
+ }
372
+
373
+ /**
374
+ * Get widget breakpoints from cache and add it to cache if it wasn't cached
375
+ *
376
+ * @return array
377
+ */
378
+ private function getCachedWidgetBreakpoints (): array
379
+ {
380
+ $ cache = $ this ->getCache (self ::WIDGET_BREAKPOINS_CACHE_ID );
381
+ if (empty ($ cache )) {
382
+ $ cache = $ this ->widgetInitializerConfig ->getBreakpoints ();
383
+ $ this ->saveCache ($ cache , self ::WIDGET_BREAKPOINS_CACHE_ID );
384
+ }
385
+ return $ cache ;
386
+ }
387
+
388
+ /**
389
+ * @param $cacheIdentifier
390
+ * @return array
391
+ */
392
+ private function getCache ($ cacheIdentifier ): array
393
+ {
394
+ $ serializedData = $ this ->cache ->load ($ cacheIdentifier );
395
+ $ cache = $ serializedData
396
+ ? $ this ->serializer ->unserialize ($ serializedData )
397
+ : [];
398
+
399
+ return $ cache ;
400
+ }
401
+
402
+ /**
403
+ * @param array $data
404
+ * @param $cacheIdentifier
405
+ */
406
+ private function saveCache (array $ data , $ cacheIdentifier ): void
407
+ {
408
+ $ this ->cache ->save ($ this ->serializer ->serialize ($ data ), $ cacheIdentifier );
409
+ }
311
410
}
0 commit comments