Skip to content
Open
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
37 changes: 28 additions & 9 deletions app/code/Magento/Theme/Model/Theme/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
*/
class Resolver implements \Magento\Framework\View\Design\Theme\ResolverInterface
{
/**
* @var array
*/
private $resolvedThemes = [];

/**
* @var bool[]
*/
private $isThemeResolved = [];

/**
* @var \Magento\Framework\View\DesignInterface
*/
Expand Down Expand Up @@ -48,19 +58,28 @@ public function __construct(
public function get()
{
$area = $this->appState->getAreaCode();
if ($this->design->getDesignTheme()->getArea() == $area || $this->design->getArea() == $area) {
return $this->design->getDesignTheme();
if ($this->isThemeResolved[$area] ?? false) {
return $this->resolvedThemes[$area];
}

/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection */
$themeCollection = $this->themeFactory->create();
$themeIdentifier = $this->design->getConfigurationDesignTheme($area);
if (is_numeric($themeIdentifier)) {
$result = $themeCollection->getItemById($themeIdentifier);
$designTheme = $this->design->getDesignTheme();
if (($designTheme && $designTheme->getArea() == $area) || $this->design->getArea() == $area) {
$result = $designTheme;
} else {
$themeFullPath = $area . \Magento\Framework\View\Design\ThemeInterface::PATH_SEPARATOR . $themeIdentifier;
$result = $themeCollection->getThemeByFullPath($themeFullPath);
/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection */
$themeCollection = $this->themeFactory->create();
$themeIdentifier = $this->design->getConfigurationDesignTheme($area);
if (is_numeric($themeIdentifier)) {
$result = $themeCollection->getItemById($themeIdentifier);
} else {
$themeFullPath = $area . \Magento\Framework\View\Design\ThemeInterface::PATH_SEPARATOR . $themeIdentifier;
$result = $themeCollection->getThemeByFullPath($themeFullPath);
}
}

$this->resolvedThemes[$area] = $result;
$this->isThemeResolved[$area] = true;

return $result;
}
}
120 changes: 120 additions & 0 deletions app/code/Magento/Theme/Test/Unit/Model/Theme/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,124 @@ public function testGetByAreaWithOtherAreaAndNumericThemeId()

$this->assertEquals($this->themeMock, $this->model->get());
}

public function testGetCachesResolvedThemeByArea()
{
$this->designMock->expects(
$this->once()
)->method(
'getDesignTheme'
)->willReturn(
$this->themeMock
);
$this->designMock->expects(
$this->once()
)->method(
'getArea'
)->willReturn(
'design_area'
);
$this->designMock->expects(
$this->once()
)->method(
'getConfigurationDesignTheme'
)->with(
'other_area'
)->willReturn(
'other_theme'
);

$this->themeMock->expects(
$this->once()
)->method(
'getArea'
)->willReturn(
'theme_area'
);

$this->themeCollectionFactoryMock->expects(
$this->once()
)->method(
'create'
)->willReturn(
$this->themeCollectionMock
);

$this->themeCollectionMock->expects(
$this->once()
)->method(
'getThemeByFullPath'
)->with(
'other_area' . ThemeInterface::PATH_SEPARATOR . 'other_theme'
)->willReturn(
$this->themeMock
);

$this->appStateMock->expects(
$this->exactly(2)
)->method(
'getAreaCode'
)->willReturn(
'other_area'
);

$this->assertSame($this->themeMock, $this->model->get());
$this->assertSame($this->themeMock, $this->model->get());
}

public function testGetCachesNullResolvedThemeByArea()
{
$this->designMock->expects(
$this->once()
)->method(
'getDesignTheme'
)->willReturn(
null
);
$this->designMock->expects(
$this->once()
)->method(
'getArea'
)->willReturn(
'design_area'
);
$this->designMock->expects(
$this->once()
)->method(
'getConfigurationDesignTheme'
)->with(
'other_area'
)->willReturn(
12
);

$this->themeCollectionFactoryMock->expects(
$this->once()
)->method(
'create'
)->willReturn(
$this->themeCollectionMock
);

$this->themeCollectionMock->expects(
$this->once()
)->method(
'getItemById'
)->with(
12
)->willReturn(
null
);

$this->appStateMock->expects(
$this->exactly(2)
)->method(
'getAreaCode'
)->willReturn(
'other_area'
);

$this->assertNull($this->model->get());
$this->assertNull($this->model->get());
}
}