Skip to content

Commit

Permalink
ENGCOM-8195: Improvement PageLayout Config Builder. Added save in cac…
Browse files Browse the repository at this point in the history
…he config files. #28818
  • Loading branch information
gabrieldagama authored Sep 22, 2020
2 parents 1feefb1 + e877544 commit c9a4107
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
68 changes: 53 additions & 15 deletions app/code/Magento/Theme/Model/PageLayout/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Theme\Model\PageLayout\Config;

use Magento\Framework\App\Cache\Type\Layout;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface;
use Magento\Framework\View\PageLayout\ConfigFactory;
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
use Magento\Theme\Model\Theme\Data;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Page layout config builder
*/
class Builder implements \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface
class Builder implements BuilderInterface
{
const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED';

/**
* @var \Magento\Framework\View\PageLayout\ConfigFactory
* @var ConfigFactory
*/
protected $configFactory;

/**
* @var \Magento\Framework\View\PageLayout\File\Collector\Aggregated
* @var Aggregated
*/
protected $fileCollector;

/**
* @var \Magento\Theme\Model\ResourceModel\Theme\Collection
* @var Collection
*/
protected $themeCollection;

Expand All @@ -33,19 +46,36 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
private $configFiles = [];

/**
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
* @var Layout|null
*/
private $cacheModel;
/**
* @var SerializerInterface|null
*/
private $serializer;

/**
* @param ConfigFactory $configFactory
* @param Aggregated $fileCollector
* @param Collection $themeCollection
* @param Layout|null $cacheModel
* @param SerializerInterface|null $serializer
*/
public function __construct(
\Magento\Framework\View\PageLayout\ConfigFactory $configFactory,
\Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector,
\Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
ConfigFactory $configFactory,
Aggregated $fileCollector,
Collection $themeCollection,
?Layout $cacheModel = null,
?SerializerInterface $serializer = null
) {
$this->configFactory = $configFactory;
$this->fileCollector = $fileCollector;
$this->themeCollection = $themeCollection;
$this->themeCollection->setItemObjectClass(\Magento\Theme\Model\Theme\Data::class);
$this->themeCollection->setItemObjectClass(Data::class);
$this->cacheModel = $cacheModel
?? ObjectManager::getInstance()->get(Layout::class);
$this->serializer = $serializer
?? ObjectManager::getInstance()->get(SerializerInterface::class);
}

/**
Expand All @@ -57,18 +87,26 @@ 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)) {
//if value in cache is corrupted.
$this->configFiles = $this->serializer->unserialize($this->configFiles);
}
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($this->serializer->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,8 +10,11 @@
*/
namespace Magento\Theme\Test\Unit\Model\PageLayout\Config;

use Magento\Framework\App\Cache\Type\Layout;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\PageLayout\Config;
use Magento\Framework\View\PageLayout\ConfigFactory;
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
use Magento\Theme\Model\PageLayout\Config\Builder;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
Expand All @@ -27,7 +30,7 @@ class BuilderTest extends TestCase
protected $builder;

/**
* @var \Magento\Framework\View\PageLayout\ConfigFactory|MockObject
* @var ConfigFactory|MockObject
*/
protected $configFactory;

Expand All @@ -41,26 +44,40 @@ class BuilderTest extends TestCase
*/
protected $themeCollection;

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

/**
* SetUp method
*
* @return void
*/
protected function setUp(): void
{
$this->configFactory = $this->getMockBuilder(\Magento\Framework\View\PageLayout\ConfigFactory::class)
$this->configFactory = $this->getMockBuilder(ConfigFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->fileCollector = $this->getMockBuilder(
Aggregated::class
)->disableOriginalConstructor()
$this->fileCollector = $this->getMockBuilder(Aggregated::class)
->disableOriginalConstructor()
->getMock();

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

$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);

$this->themeCollection->expects($this->once())
->method('setItemObjectClass')
->with(Data::class)
Expand All @@ -72,7 +89,9 @@ protected function setUp(): void
[
'configFactory' => $this->configFactory,
'fileCollector' => $this->fileCollector,
'themeCollection' => $this->themeCollection
'themeCollection' => $this->themeCollection,
'cacheModel' => $this->cacheModel,
'serializer' => $this->serializer,
]
);
}
Expand All @@ -84,8 +103,10 @@ 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'];
$configFiles = array_merge($files1, $files2);

$theme1 = $this->getMockBuilder(Data::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -113,9 +134,17 @@ public function testGetPageLayoutsConfig()

$this->configFactory->expects($this->once())
->method('create')
->with(['configFiles' => array_merge($files1, $files2)])
->with(['configFiles' => $configFiles])
->willReturn($config);

$this->serializer->expects($this->once())
->method('serialize')
->with($configFiles);

$this->cacheModel->expects($this->once())
->method('save')
->willReturnSelf();

$this->assertSame($config, $this->builder->getPageLayoutsConfig());
}
}

0 comments on commit c9a4107

Please sign in to comment.