Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Avoid duplicate loading of configuration files #20646

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Layout extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
*/
protected $pageLayoutBuilder;

/**
* @inheritdoc
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
*/
protected $_options = null;

/**
* @param \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder
*/
Expand All @@ -26,14 +32,14 @@ public function __construct(\Magento\Framework\View\Model\PageLayout\Config\Buil
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
array_unshift($this->_options, ['value' => '', 'label' => __('No layout updates')]);
}
return $this->_options;
$options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
array_unshift($options, ['value' => '', 'label' => __('No layout updates')]);
$this->_options = $options;

return $options;
}
}
18 changes: 12 additions & 6 deletions app/code/Magento/Catalog/Model/Product/Attribute/Source/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Layout extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
*/
protected $pageLayoutBuilder;

/**
* @inheritdoc
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
*/
protected $_options = null;

/**
* @param \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder
*/
Expand All @@ -26,14 +32,14 @@ public function __construct(\Magento\Framework\View\Model\PageLayout\Config\Buil
}

/**
* @return array
* @inheritdoc
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
array_unshift($this->_options, ['value' => '', 'label' => __('No layout updates')]);
}
return $this->_options;
$options = $this->pageLayoutBuilder->getPageLayoutsConfig()->toOptionArray();
array_unshift($options, ['value' => '', 'label' => __('No layout updates')]);
$this->_options = $options;

return $options;
}
}
11 changes: 3 additions & 8 deletions app/code/Magento/Cms/Model/Page/Source/PageLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PageLayout implements OptionSourceInterface

/**
* @var array
* @deprecated since the cache is now handled by \Magento\Theme\Model\PageLayout\Config\Builder::$configFiles
*/
protected $options;

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

/**
* Get options
*
* @return array
* @inheritdoc
*/
public function toOptionArray()
{
if ($this->options !== null) {
return $this->options;
}

$configOptions = $this->pageLayoutBuilder->getPageLayoutsConfig()->getOptions();
$options = [];
foreach ($configOptions as $key => $value) {
Expand All @@ -54,6 +49,6 @@ public function toOptionArray()
}
$this->options = $options;

return $this->options;
return $options;
}
}
20 changes: 15 additions & 5 deletions app/code/Magento/Theme/Model/PageLayout/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
*/
protected $themeCollection;

/**
* @var array
*/
private $configFiles = [];

/**
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
Expand All @@ -44,23 +49,28 @@ public function __construct(
}

/**
* @return \Magento\Framework\View\PageLayout\Config
* @inheritdoc
*/
public function getPageLayoutsConfig()
{
return $this->configFactory->create(['configFiles' => $this->getConfigFiles()]);
}

/**
* Retrieve configuration files.
*
* @return array
*/
protected function getConfigFiles()
{
$configFiles = [];
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
$configFiles = array_merge($configFiles, $this->fileCollector->getFilesContent($theme, 'layouts.xml'));
if (!$this->configFiles) {
$configFiles = [];
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
}
$this->configFiles = array_merge(...$configFiles);
}

return $configFiles;
return $this->configFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testGetPageLayoutsConfig()
->disableOriginalConstructor()
->getMock();

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

Expand Down