55 * Copyright © Magento, Inc. All rights reserved.
66 * See COPYING.txt for license details.
77 */
8+ declare (strict_types=1 );
9+
810namespace Magento \Theme \Model \PageLayout \Config ;
911
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+
1021/**
1122 * Page layout config builder
1223 */
13- class Builder implements \ Magento \ Framework \ View \ Model \ PageLayout \ Config \ BuilderInterface
24+ class Builder implements BuilderInterface
1425{
26+ const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED ' ;
27+
1528 /**
16- * @var \Magento\Framework\View\PageLayout\ ConfigFactory
29+ * @var ConfigFactory
1730 */
1831 protected $ configFactory ;
1932
2033 /**
21- * @var \Magento\Framework\View\PageLayout\File\Collector\ Aggregated
34+ * @var Aggregated
2235 */
2336 protected $ fileCollector ;
2437
2538 /**
26- * @var \Magento\Theme\Model\ResourceModel\Theme\ Collection
39+ * @var Collection
2740 */
2841 protected $ themeCollection ;
2942
@@ -33,19 +46,36 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
3346 private $ configFiles = [];
3447
3548 /**
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
3963 */
4064 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
4470 ) {
4571 $ this ->configFactory = $ configFactory ;
4672 $ this ->fileCollector = $ fileCollector ;
4773 $ 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);
4979 }
5080
5181 /**
@@ -57,18 +87,26 @@ public function getPageLayoutsConfig()
5787 }
5888
5989 /**
60- * Retrieve configuration files.
90+ * Retrieve configuration files. Caches merged layouts.xml XML files.
6191 *
6292 * @return array
6393 */
6494 protected function getConfigFiles ()
6595 {
6696 if (!$ this ->configFiles ) {
6797 $ 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 );
70109 }
71- $ this ->configFiles = array_merge (...$ configFiles );
72110 }
73111
74112 return $ this ->configFiles ;
0 commit comments