Skip to content

Improvement PageLayout Config Builder. Added save in cache config files. #28818

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

Merged
merged 6 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
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