Skip to content

Commit e877544

Browse files
ENGCOM-8195: Improvement PageLayout Config Builder. Added save in cache config files. #28818
- Merge Pull Request #28818 from Knase/magento2:cache-config-builder - Merged commits: 1. f2468b5 2. cc2c5b9 3. 26b41af 4. eeecdf7 5. 2ac6e0c 6. 276cb1c
2 parents 390505a + 276cb1c commit e877544

File tree

2 files changed

+89
-22
lines changed

2 files changed

+89
-22
lines changed

app/code/Magento/Theme/Model/PageLayout/Config/Builder.php

+53-15
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,38 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
declare(strict_types=1);
9+
810
namespace 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;

app/code/Magento/Theme/Test/Unit/Model/PageLayout/Config/BuilderTest.php

+36-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
*/
1111
namespace Magento\Theme\Test\Unit\Model\PageLayout\Config;
1212

13+
use Magento\Framework\App\Cache\Type\Layout;
14+
use Magento\Framework\Serialize\SerializerInterface;
1315
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1416
use Magento\Framework\View\PageLayout\Config;
17+
use Magento\Framework\View\PageLayout\ConfigFactory;
1518
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
1619
use Magento\Theme\Model\PageLayout\Config\Builder;
1720
use Magento\Theme\Model\ResourceModel\Theme\Collection;
@@ -27,7 +30,7 @@ class BuilderTest extends TestCase
2730
protected $builder;
2831

2932
/**
30-
* @var \Magento\Framework\View\PageLayout\ConfigFactory|MockObject
33+
* @var ConfigFactory|MockObject
3134
*/
3235
protected $configFactory;
3336

@@ -41,26 +44,40 @@ class BuilderTest extends TestCase
4144
*/
4245
protected $themeCollection;
4346

47+
/**
48+
* @var Layout|MockObject
49+
*/
50+
protected $cacheModel;
51+
/**
52+
* @var SerializerInterface|MockObject
53+
*/
54+
protected $serializer;
55+
4456
/**
4557
* SetUp method
4658
*
4759
* @return void
4860
*/
4961
protected function setUp(): void
5062
{
51-
$this->configFactory = $this->getMockBuilder(\Magento\Framework\View\PageLayout\ConfigFactory::class)
63+
$this->configFactory = $this->getMockBuilder(ConfigFactory::class)
5264
->disableOriginalConstructor()
5365
->setMethods(['create'])
5466
->getMock();
5567

56-
$this->fileCollector = $this->getMockBuilder(
57-
Aggregated::class
58-
)->disableOriginalConstructor()
68+
$this->fileCollector = $this->getMockBuilder(Aggregated::class)
69+
->disableOriginalConstructor()
5970
->getMock();
6071

6172
$this->themeCollection = $this->getMockBuilder(Collection::class)
6273
->disableOriginalConstructor()
6374
->getMock();
75+
$this->cacheModel = $this->getMockBuilder(Layout::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
80+
6481
$this->themeCollection->expects($this->once())
6582
->method('setItemObjectClass')
6683
->with(Data::class)
@@ -72,7 +89,9 @@ protected function setUp(): void
7289
[
7390
'configFactory' => $this->configFactory,
7491
'fileCollector' => $this->fileCollector,
75-
'themeCollection' => $this->themeCollection
92+
'themeCollection' => $this->themeCollection,
93+
'cacheModel' => $this->cacheModel,
94+
'serializer' => $this->serializer,
7695
]
7796
);
7897
}
@@ -84,8 +103,10 @@ protected function setUp(): void
84103
*/
85104
public function testGetPageLayoutsConfig()
86105
{
106+
$this->cacheModel->clean();
87107
$files1 = ['content layouts_1.xml', 'content layouts_2.xml'];
88108
$files2 = ['content layouts_3.xml', 'content layouts_4.xml'];
109+
$configFiles = array_merge($files1, $files2);
89110

90111
$theme1 = $this->getMockBuilder(Data::class)
91112
->disableOriginalConstructor()
@@ -113,9 +134,17 @@ public function testGetPageLayoutsConfig()
113134

114135
$this->configFactory->expects($this->once())
115136
->method('create')
116-
->with(['configFiles' => array_merge($files1, $files2)])
137+
->with(['configFiles' => $configFiles])
117138
->willReturn($config);
118139

140+
$this->serializer->expects($this->once())
141+
->method('serialize')
142+
->with($configFiles);
143+
144+
$this->cacheModel->expects($this->once())
145+
->method('save')
146+
->willReturnSelf();
147+
119148
$this->assertSame($config, $this->builder->getPageLayoutsConfig());
120149
}
121150
}

0 commit comments

Comments
 (0)