Skip to content

Commit b91afa2

Browse files
committed
Merge branch 'b-7.5.x-cache-active-modules-id-OXDEV-9930' into b-8.0.x-cache-active-modules-id-OXDEV-9930
# Conflicts: # CHANGELOG-2.x.md
2 parents ce869aa + 12cf9ae commit b91afa2

4 files changed

Lines changed: 91 additions & 18 deletions

File tree

src/Resolver/TemplateChain/TemplateChainResolver.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class TemplateChainResolver implements TemplateChainResolverInterface
1515
{
1616
private array $lastChildCache = [];
17+
private array $parentCache = [];
1718

1819
public function __construct(
1920
private TemplateChainBuilderInterface $templateChainBuilder,
@@ -23,11 +24,15 @@ public function __construct(
2324

2425
public function getParent(string $templateName): string
2526
{
26-
$templateType = $this->templateTypeFactory->createFromTemplateName($templateName);
27-
return $this->templateChainBuilder
28-
->getChain($templateType)
29-
->getParent($templateType)
30-
->getFullyQualifiedName();
27+
if (!isset($this->parentCache[$templateName])) {
28+
$templateType = $this->templateTypeFactory->createFromTemplateName($templateName);
29+
$this->parentCache[$templateName] = $this->templateChainBuilder
30+
->getChain($templateType)
31+
->getParent($templateType)
32+
->getFullyQualifiedName();
33+
}
34+
35+
return $this->parentCache[$templateName];
3136
}
3237

3338
public function getLastChild(string $templateName): string

src/Resolver/TemplateChain/TemplateChainSorter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace OxidEsales\Twig\Resolver\TemplateChain;
1111

12-
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\ShopConfigurationDaoInterface;
12+
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\Chain\TemplateExtensionChainDaoInterface;
1313
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleTemplateExtensionChain;
1414
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
1515
use OxidEsales\Twig\Resolver\TemplateChain\DataObject\TemplateChain;
@@ -18,21 +18,25 @@
1818

1919
class TemplateChainSorter implements TemplateChainSorterInterface
2020
{
21+
private array $chainCache = [];
22+
2123
public function __construct(
2224
private SortingConfigurationValidatorInterface $sortingConfigurationValidator,
23-
private ShopConfigurationDaoInterface $shopConfigurationDao,
25+
private TemplateExtensionChainDaoInterface $templateExtensionChainDao,
2426
private ContextInterface $context,
2527
private LoggerInterface $logger,
2628
) {
2729
}
2830

2931
public function sort(TemplateChain $unsortedChain, TemplateTypeInterface $extendedTemplate): TemplateChain
3032
{
31-
$sortingConfiguration = $this->shopConfigurationDao
32-
->get($this->context->getCurrentShopId())
33-
->getModuleTemplateExtensionChain();
33+
$shopId = $this->context->getCurrentShopId();
34+
35+
if (!isset($this->chainCache[$shopId])) {
36+
$this->chainCache[$shopId] = $this->templateExtensionChainDao->getChain($shopId);
37+
}
3438

35-
return $this->getSortedChain($unsortedChain, $sortingConfiguration, $extendedTemplate);
39+
return $this->getSortedChain($unsortedChain, $this->chainCache[$shopId], $extendedTemplate);
3640
}
3741

3842
private function getSortedChain(

tests/Unit/Resolver/TemplateChain/TemplateChainResolverTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@
1818

1919
final class TemplateChainResolverTest extends TestCase
2020
{
21+
public function testGetParentWithMultipleCallsWillUseCachedResult(): void
22+
{
23+
$templateName = 'widget/header.html.twig';
24+
25+
$templateTypeMock = $this->createMock(TemplateTypeInterface::class);
26+
$parentMock = $this->createConfiguredMock(TemplateTypeInterface::class, [
27+
'getFullyQualifiedName' => '@my_module/widget/header.html.twig'
28+
]);
29+
$templateChainMock = $this->createConfiguredMock(TemplateChain::class, [
30+
'getParent' => $parentMock
31+
]);
32+
33+
$templateTypeFactoryMock = $this->createMock(TemplateTypeFactoryInterface::class);
34+
$templateTypeFactoryMock
35+
->expects($this->once())
36+
->method('createFromTemplateName')
37+
->with($templateName)
38+
->willReturn($templateTypeMock);
39+
40+
$templateChainBuilderMock = $this->createMock(TemplateChainBuilderInterface::class);
41+
$templateChainBuilderMock
42+
->expects($this->once())
43+
->method('getChain')
44+
->with($templateTypeMock)
45+
->willReturn($templateChainMock);
46+
47+
$templateChainResolver = new TemplateChainResolver($templateChainBuilderMock, $templateTypeFactoryMock);
48+
49+
$templateChainResolver->getParent($templateName);
50+
$templateChainResolver->getParent($templateName);
51+
$templateChainResolver->getParent($templateName);
52+
}
53+
2154
public function testGetLastChildWithMultipleCallsWillUseCachedResult(): void
2255
{
2356
$templateName = 'widget/header.html.twig';

tests/Unit/Resolver/TemplateChain/TemplateChainSorterTest.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
namespace OxidEsales\Twig\Tests\Unit\Resolver\TemplateChain;
1111

12-
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\ShopConfigurationDaoInterface;
12+
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\Chain\TemplateExtensionChainDaoInterface;
1313
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleTemplateExtensionChain;
14-
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ShopConfiguration;
1514
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
1615
use OxidEsales\Twig\Resolver\TemplateChain\DataObject\TemplateChain;
1716
use OxidEsales\Twig\Resolver\TemplateChain\InvalidSortingConfigurationException;
@@ -162,17 +161,49 @@ public function testSortWithADuplicatedModuleIdInConfigurationWillCallLogger():
162161
$this->logger->error(Argument::type('string'))->shouldHaveBeenCalled();
163162
}
164163

164+
public function testSortWithMultipleCallsWillUseCachedShopConfiguration(): void
165+
{
166+
$shopId = 1;
167+
$templateModule1 = new ModuleTemplateType('template1', 'module1');
168+
$chain = new TemplateChain();
169+
$chain->append($templateModule1);
170+
171+
$moduleTemplateExtensionsChain = new ModuleTemplateExtensionChain([]);
172+
173+
$templateExtensionChainDao = $this->prophesize(TemplateExtensionChainDaoInterface::class);
174+
$templateExtensionChainDao->getChain($shopId)->willReturn($moduleTemplateExtensionsChain)->shouldBeCalledOnce();
175+
176+
$context = $this->prophesize(ContextInterface::class);
177+
$context->getCurrentShopId()->willReturn($shopId);
178+
179+
$this->sortingConfigurationValidator = $this->prophesize(SortingConfigurationValidatorInterface::class);
180+
$this->logger = $this->prophesize(LoggerInterface::class);
181+
182+
$this->chainSorter = new TemplateChainSorter(
183+
$this->sortingConfigurationValidator->reveal(),
184+
$templateExtensionChainDao->reveal(),
185+
$context->reveal(),
186+
$this->logger->reveal(),
187+
);
188+
189+
$chain1 = new TemplateChain();
190+
$chain1->append($templateModule1);
191+
$this->chainSorter->sort($chain1, $templateModule1);
192+
193+
$chain2 = new TemplateChain();
194+
$chain2->append($templateModule1);
195+
$this->chainSorter->sort($chain2, $templateModule1);
196+
}
197+
165198
private function prepareChainSortersConfiguration(string $templateName, array $loadOrder): void
166199
{
167200
$shopId = 1;
168201
$moduleTemplateExtensionsChain = new ModuleTemplateExtensionChain([$templateName => $loadOrder]);
169202

170-
$shopConfiguration = $this->prophesize(ShopConfiguration::class);
171-
$shopConfigurationDao = $this->prophesize(ShopConfigurationDaoInterface::class);
203+
$templateExtensionChainDao = $this->prophesize(TemplateExtensionChainDaoInterface::class);
172204
$context = $this->prophesize(ContextInterface::class);
173205

174-
$shopConfiguration->getModuleTemplateExtensionChain()->willReturn($moduleTemplateExtensionsChain);
175-
$shopConfigurationDao->get($shopId)->willReturn($shopConfiguration);
206+
$templateExtensionChainDao->getChain($shopId)->willReturn($moduleTemplateExtensionsChain);
176207
$context->getCurrentShopId()->willReturn($shopId);
177208

178209
$this->logger = $this->prophesize(LoggerInterface::class);
@@ -181,7 +212,7 @@ private function prepareChainSortersConfiguration(string $templateName, array $l
181212

182213
$this->chainSorter = new TemplateChainSorter(
183214
$this->sortingConfigurationValidator->reveal(),
184-
$shopConfigurationDao->reveal(),
215+
$templateExtensionChainDao->reveal(),
185216
$context->reveal(),
186217
$this->logger->reveal(),
187218
);

0 commit comments

Comments
 (0)