5
5
* Copyright © Magento, Inc. All rights reserved.
6
6
* See COPYING.txt for license details.
7
7
*/
8
+ declare (strict_types=1 );
9
+
8
10
namespace Magento \Theme \Model \PageLayout \Config ;
9
11
12
+ use Magento \Framework \App \Cache \Type \Layout ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \View \Model \PageLayout \Config \BuilderInterface ;
15
+ use Magento \Framework \View \PageLayout \ConfigFactory ;
16
+ use Magento \Framework \View \PageLayout \File \Collector \Aggregated ;
17
+ use Magento \Theme \Model \ResourceModel \Theme \Collection ;
18
+ use Magento \Theme \Model \Theme \Data ;
19
+ use Magento \Framework \Serialize \SerializerInterface ;
20
+
10
21
/**
11
22
* Page layout config builder
12
23
*/
13
- class Builder implements \ Magento \ Framework \ View \ Model \ PageLayout \ Config \ BuilderInterface
24
+ class Builder implements BuilderInterface
14
25
{
26
+ const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED ' ;
27
+
15
28
/**
16
- * @var \Magento\Framework\View\PageLayout\ ConfigFactory
29
+ * @var ConfigFactory
17
30
*/
18
31
protected $ configFactory ;
19
32
20
33
/**
21
- * @var \Magento\Framework\View\PageLayout\File\Collector\ Aggregated
34
+ * @var Aggregated
22
35
*/
23
36
protected $ fileCollector ;
24
37
25
38
/**
26
- * @var \Magento\Theme\Model\ResourceModel\Theme\ Collection
39
+ * @var Collection
27
40
*/
28
41
protected $ themeCollection ;
29
42
@@ -33,19 +46,36 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
33
46
private $ configFiles = [];
34
47
35
48
/**
36
- * @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
37
- * @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
38
- * @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
49
+ * @var Layout|null
50
+ */
51
+ private $ cacheModel ;
52
+ /**
53
+ * @var SerializerInterface|null
54
+ */
55
+ private $ serializer ;
56
+
57
+ /**
58
+ * @param ConfigFactory $configFactory
59
+ * @param Aggregated $fileCollector
60
+ * @param Collection $themeCollection
61
+ * @param Layout|null $cacheModel
62
+ * @param SerializerInterface|null $serializer
39
63
*/
40
64
public function __construct (
41
- \Magento \Framework \View \PageLayout \ConfigFactory $ configFactory ,
42
- \Magento \Framework \View \PageLayout \File \Collector \Aggregated $ fileCollector ,
43
- \Magento \Theme \Model \ResourceModel \Theme \Collection $ themeCollection
65
+ ConfigFactory $ configFactory ,
66
+ Aggregated $ fileCollector ,
67
+ Collection $ themeCollection ,
68
+ ?Layout $ cacheModel = null ,
69
+ ?SerializerInterface $ serializer = null
44
70
) {
45
71
$ this ->configFactory = $ configFactory ;
46
72
$ this ->fileCollector = $ fileCollector ;
47
73
$ this ->themeCollection = $ themeCollection ;
48
- $ this ->themeCollection ->setItemObjectClass (\Magento \Theme \Model \Theme \Data::class);
74
+ $ this ->themeCollection ->setItemObjectClass (Data::class);
75
+ $ this ->cacheModel = $ cacheModel
76
+ ?? ObjectManager::getInstance ()->get (Layout::class);
77
+ $ this ->serializer = $ serializer
78
+ ?? ObjectManager::getInstance ()->get (SerializerInterface::class);
49
79
}
50
80
51
81
/**
@@ -57,18 +87,26 @@ public function getPageLayoutsConfig()
57
87
}
58
88
59
89
/**
60
- * Retrieve configuration files.
90
+ * Retrieve configuration files. Caches merged layouts.xml XML files.
61
91
*
62
92
* @return array
63
93
*/
64
94
protected function getConfigFiles ()
65
95
{
66
96
if (!$ this ->configFiles ) {
67
97
$ configFiles = [];
68
- foreach ($ this ->themeCollection ->loadRegisteredThemes () as $ theme ) {
69
- $ configFiles [] = $ this ->fileCollector ->getFilesContent ($ theme , 'layouts.xml ' );
98
+ $ this ->configFiles = $ this ->cacheModel ->load (self ::CACHE_KEY_LAYOUTS );
99
+ if (!empty ($ this ->configFiles )) {
100
+ //if value in cache is corrupted.
101
+ $ this ->configFiles = $ this ->serializer ->unserialize ($ this ->configFiles );
102
+ }
103
+ if (empty ($ this ->configFiles )) {
104
+ foreach ($ this ->themeCollection ->loadRegisteredThemes () as $ theme ) {
105
+ $ configFiles [] = $ this ->fileCollector ->getFilesContent ($ theme , 'layouts.xml ' );
106
+ }
107
+ $ this ->configFiles = array_merge (...$ configFiles );
108
+ $ this ->cacheModel ->save ($ this ->serializer ->serialize ($ this ->configFiles ), self ::CACHE_KEY_LAYOUTS );
70
109
}
71
- $ this ->configFiles = array_merge (...$ configFiles );
72
110
}
73
111
74
112
return $ this ->configFiles ;
0 commit comments