Skip to content

Commit 0b0e952

Browse files
authored
ENGCOM-3634: Avoid duplicate loading of configuration files #19585
2 parents a556e3c + 3356534 commit 0b0e952

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Source/Layout.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class Layout extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
1717
*/
1818
protected $pageLayoutBuilder;
1919

20+
/**
21+
* @inheritdoc
22+
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
23+
*/
24+
protected $_options = null;
25+
2026
/**
2127
* @param \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder
2228
*/
@@ -26,14 +32,14 @@ public function __construct(\Magento\Framework\View\Model\PageLayout\Config\Buil
2632
}
2733

2834
/**
29-
* {@inheritdoc}
35+
* @inheritdoc
3036
*/
3137
public function getAllOptions()
3238
{
33-
if (!$this->_options) {
34-
$this->_options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
35-
array_unshift($this->_options, ['value' => '', 'label' => __('No layout updates')]);
36-
}
37-
return $this->_options;
39+
$options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
40+
array_unshift($options, ['value' => '', 'label' => __('No layout updates')]);
41+
$this->_options = $options;
42+
43+
return $options;
3844
}
3945
}

app/code/Magento/Catalog/Model/Product/Attribute/Source/Layout.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class Layout extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
1717
*/
1818
protected $pageLayoutBuilder;
1919

20+
/**
21+
* @inheritdoc
22+
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
23+
*/
24+
protected $_options = null;
25+
2026
/**
2127
* @param \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder
2228
*/
@@ -26,14 +32,14 @@ public function __construct(\Magento\Framework\View\Model\PageLayout\Config\Buil
2632
}
2733

2834
/**
29-
* @return array
35+
* @inheritdoc
3036
*/
3137
public function getAllOptions()
3238
{
33-
if (!$this->_options) {
34-
$this->_options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
35-
array_unshift($this->_options, ['value' => '', 'label' => __('No layout updates')]);
36-
}
37-
return $this->_options;
39+
$options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
40+
array_unshift($options, ['value' => '', 'label' => __('No layout updates')]);
41+
$this->_options = $options;
42+
43+
return $options;
3844
}
3945
}

app/code/Magento/Cms/Model/Page/Source/PageLayout.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PageLayout implements OptionSourceInterface
2020

2121
/**
2222
* @var array
23+
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
2324
*/
2425
protected $options;
2526

@@ -34,16 +35,10 @@ public function __construct(BuilderInterface $pageLayoutBuilder)
3435
}
3536

3637
/**
37-
* Get options
38-
*
39-
* @return array
38+
* @inheritdoc
4039
*/
4140
public function toOptionArray()
4241
{
43-
if ($this->options !== null) {
44-
return $this->options;
45-
}
46-
4742
$configOptions = $this->pageLayoutBuilder->getPageLayoutsConfig()->getOptions();
4843
$options = [];
4944
foreach ($configOptions as $key => $value) {
@@ -54,6 +49,6 @@ public function toOptionArray()
5449
}
5550
$this->options = $options;
5651

57-
return $this->options;
52+
return $options;
5853
}
5954
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
2727
*/
2828
protected $themeCollection;
2929

30+
/**
31+
* @var array
32+
*/
33+
private $configFiles = [];
34+
3035
/**
3136
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
3237
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
@@ -44,23 +49,28 @@ public function __construct(
4449
}
4550

4651
/**
47-
* @return \Magento\Framework\View\PageLayout\Config
52+
* @inheritdoc
4853
*/
4954
public function getPageLayoutsConfig()
5055
{
5156
return $this->configFactory->create(['configFiles' => $this->getConfigFiles()]);
5257
}
5358

5459
/**
60+
* Retrieve configuration files.
61+
*
5562
* @return array
5663
*/
5764
protected function getConfigFiles()
5865
{
59-
$configFiles = [];
60-
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
61-
$configFiles = array_merge($configFiles, $this->fileCollector->getFilesContent($theme, 'layouts.xml'));
66+
if (!$this->configFiles) {
67+
$configFiles = [];
68+
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
69+
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
70+
}
71+
$this->configFiles = array_merge(...$configFiles);
6272
}
6373

64-
return $configFiles;
74+
return $this->configFiles;
6575
}
6676
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function testGetPageLayoutsConfig()
8383
->disableOriginalConstructor()
8484
->getMock();
8585

86-
$this->themeCollection->expects($this->any())
86+
$this->themeCollection->expects($this->once())
8787
->method('loadRegisteredThemes')
8888
->willReturn([$theme1, $theme2]);
8989

0 commit comments

Comments
 (0)