Skip to content

Commit

Permalink
Improvement PageLayout Config Builder. Added save in cache config files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Svyatoslav committed Jun 19, 2020
1 parent 8513dfd commit f2468b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
27 changes: 22 additions & 5 deletions app/code/Magento/Theme/Model/PageLayout/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class Builder implements \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface
{
const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED';

/**
* @var \Magento\Framework\View\PageLayout\ConfigFactory
*/
Expand All @@ -32,19 +34,27 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
*/
private $configFiles = [];

/**
* @var \Magento\Framework\App\Cache\Type\Layout
*/
protected $cacheModel;

/**
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
* @param \Magento\Framework\App\Cache\Type\Layout $cacheModel
*/
public function __construct(
\Magento\Framework\View\PageLayout\ConfigFactory $configFactory,
\Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector,
\Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
\Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection,
\Magento\Framework\App\Cache\Type\Layout $cacheModel
) {
$this->configFactory = $configFactory;
$this->fileCollector = $fileCollector;
$this->themeCollection = $themeCollection;
$this->cacheModel = $cacheModel;
$this->themeCollection->setItemObjectClass(\Magento\Theme\Model\Theme\Data::class);
}

Expand All @@ -57,18 +67,25 @@ public function getPageLayoutsConfig()
}

/**
* Retrieve configuration files.
* Retrieve configuration files. Caches merged layouts.xml XML files.
*
* @return array
*/
protected function getConfigFiles()
{
if (!$this->configFiles) {
$configFiles = [];
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
$this->configFiles = $this->cacheModel->load(self::CACHE_KEY_LAYOUTS);
if (!empty($this->configFiles)) {
$this->configFiles = @unserialize($this->configFiles);//if value in cache is corrupted.
}
if (empty($this->configFiles)) {
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
}
$this->configFiles = array_merge(...$configFiles);
$this->cacheModel->save(serialize($this->configFiles), self::CACHE_KEY_LAYOUTS);
}
$this->configFiles = array_merge(...$configFiles);
}

return $this->configFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
*/
namespace Magento\Theme\Test\Unit\Model\PageLayout\Config;

use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\PageLayout\Config;
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Theme\Model\PageLayout\Config\Builder;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
use Magento\Theme\Model\Theme\Data;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Cache\Type\Layout;

class BuilderTest extends TestCase
{
Expand All @@ -41,6 +46,11 @@ class BuilderTest extends TestCase
*/
protected $themeCollection;

/**
* @var Layout|MockObject
*/
protected $cacheModel;

/**
* SetUp method
*
Expand All @@ -58,21 +68,26 @@ protected function setUp(): void
)->disableOriginalConstructor()
->getMock();

$helper = new ObjectManager($this);
$this->themeCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();
$this->cacheModel = $this->getMockBuilder(Layout::class)
->disableOriginalConstructor()
->getMock();

$this->themeCollection->expects($this->once())
->method('setItemObjectClass')
->with(Data::class)
->willReturnSelf();

$helper = new ObjectManager($this);
$this->builder = $helper->getObject(
Builder::class,
[
'configFactory' => $this->configFactory,
'fileCollector' => $this->fileCollector,
'themeCollection' => $this->themeCollection
'themeCollection' => $this->themeCollection,
'cacheModel' => $this->cacheModel,
]
);
}
Expand All @@ -84,6 +99,7 @@ protected function setUp(): void
*/
public function testGetPageLayoutsConfig()
{
$this->cacheModel->clean();
$files1 = ['content layouts_1.xml', 'content layouts_2.xml'];
$files2 = ['content layouts_3.xml', 'content layouts_4.xml'];

Expand Down

0 comments on commit f2468b5

Please sign in to comment.