Skip to content

Commit

Permalink
ENGCOM-3634: Avoid duplicate loading of configuration files #19585
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaschenko authored Jan 25, 2019
2 parents a556e3c + 3356534 commit 0b0e952
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
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

0 comments on commit 0b0e952

Please sign in to comment.