Skip to content

Commit 373c18c

Browse files
NEXT-14693 - Consider app version id for template cache. Consider plugin version id for cache hash which is used for the cache directory
1 parent 78dee6f commit 373c18c

14 files changed

+81
-109
lines changed

phpstan-baseline.neon

+1-21
Original file line numberDiff line numberDiff line change
@@ -3917,31 +3917,11 @@ parameters:
39173917

39183918
-
39193919
message: "#^Cannot call method getPluginInstances\\(\\) on Shopware\\\\Core\\\\Framework\\\\Plugin\\\\KernelPluginLoader\\\\KernelPluginLoader\\|null\\.$#"
3920-
count: 2
3921-
path: src/Core/Kernel.php
3922-
3923-
-
3924-
message: "#^Cannot call method getPluginDir\\(\\) on Shopware\\\\Core\\\\Framework\\\\Plugin\\\\KernelPluginLoader\\\\KernelPluginLoader\\|null\\.$#"
3925-
count: 1
3926-
path: src/Core/Kernel.php
3927-
3928-
-
3929-
message: "#^Cannot call method getPluginInfos\\(\\) on Shopware\\\\Core\\\\Framework\\\\Plugin\\\\KernelPluginLoader\\\\KernelPluginLoader\\|null\\.$#"
3930-
count: 1
3931-
path: src/Core/Kernel.php
3932-
3933-
-
3934-
message: "#^Method Shopware\\\\Core\\\\Kernel\\:\\:getCacheHash\\(\\) has no return typehint specified\\.$#"
3935-
count: 1
3936-
path: src/Core/Kernel.php
3937-
3938-
-
3939-
message: "#^Parameter \\#1 \\$str of function md5 expects string, string\\|false given\\.$#"
39403920
count: 1
39413921
path: src/Core/Kernel.php
39423922

39433923
-
3944-
message: "#^Parameter \\#1 \\$str of function mb_substr expects string, string\\|null given\\.$#"
3924+
message: "#^Cannot call method getPluginDir\\(\\) on Shopware\\\\Core\\\\Framework\\\\Plugin\\\\KernelPluginLoader\\\\KernelPluginLoader\\|null\\.$#"
39453925
count: 1
39463926
path: src/Core/Kernel.php
39473927

phpstan.neon.dist

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ parameters:
116116
-
117117
message: '#Method Shopware\\Core\\Content\\Product\\DataAbstractionLayer\\ProductStreamUpdater::iterate\(\) has parameter \$offset with no typehint specified\.#'
118118
path: src/Core/Content/Product/DataAbstractionLayer/ProductStreamUpdater.php
119+
-
120+
message: '#Cannot call method getPluginInfos\(\) on Shopware\\Core\\Framework\\Plugin\\KernelPluginLoader\\KernelPluginLoader\|null\.#'
121+
path: src/Core/Kernel.php
122+
119123
- '#Method .*FieldSerializer::decode\(\) has no return typehint specified\.#'
120124
- '#Method .*FieldSerializer::decode\(\) has parameter \$value with no typehint specified\.#'
121125
- '#Method .*CustomFieldsSerializer::decode\(\) has no return typehint specified\.#'

src/Core/Framework/Adapter/Twig/NamespaceHierarchy/BundleHierarchyBuilder.php

+21-33
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,26 @@
22

33
namespace Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy;
44

5+
use Doctrine\DBAL\Connection;
56
use Shopware\Core\Framework\Bundle;
6-
use Shopware\Core\Framework\Context;
7-
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
8-
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
9-
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
10-
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
11-
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
127
use Symfony\Component\HttpKernel\KernelInterface;
138

149
class BundleHierarchyBuilder implements TemplateNamespaceHierarchyBuilderInterface
1510
{
16-
/**
17-
* @var KernelInterface
18-
*/
19-
private $kernel;
11+
private KernelInterface $kernel;
2012

21-
/**
22-
* @var EntityRepositoryInterface
23-
*/
24-
private $appRepository;
13+
private Connection $connection;
2514

26-
public function __construct(KernelInterface $kernel, EntityRepositoryInterface $appRepository)
15+
public function __construct(KernelInterface $kernel, Connection $connection)
2716
{
2817
$this->kernel = $kernel;
29-
$this->appRepository = $appRepository;
18+
$this->connection = $connection;
3019
}
3120

3221
public function buildNamespaceHierarchy(array $namespaceHierarchy): array
3322
{
23+
$bundles = [];
24+
3425
foreach ($this->kernel->getBundles() as $bundle) {
3526
if (!$bundle instanceof Bundle) {
3627
continue;
@@ -44,29 +35,26 @@ public function buildNamespaceHierarchy(array $namespaceHierarchy): array
4435
continue;
4536
}
4637

47-
array_unshift($namespaceHierarchy, $bundle->getName());
48-
49-
$namespaceHierarchy = array_values(array_unique($namespaceHierarchy));
38+
// bundle or plugin version unknown at this point
39+
$bundles[$bundle->getName()] = 1;
5040
}
5141

52-
return array_unique(array_merge($this->getAppTemplateNamespaces(), $namespaceHierarchy));
42+
$bundles = array_reverse($bundles);
43+
44+
return array_merge(
45+
$this->getAppTemplateNamespaces(),
46+
$bundles,
47+
$namespaceHierarchy
48+
);
5349
}
5450

55-
/**
56-
* @return string[]
57-
*/
5851
private function getAppTemplateNamespaces(): array
5952
{
60-
$criteria = new Criteria();
61-
$criteria->addFilter(
62-
new EqualsFilter('app.active', true),
63-
new EqualsFilter('app.templates.active', true)
53+
return $this->connection->fetchAllKeyValue(
54+
'SELECT `app`.`name`, `app`.`version`
55+
FROM `app`
56+
INNER JOIN `app_template` ON `app_template`.`app_id` = `app`.`id`
57+
WHERE `app`.`active` = 1 AND `app_template`.`active` = 1'
6458
);
65-
$criteria->addAggregation(new TermsAggregation('appNames', 'app.name'));
66-
67-
/** @var TermsResult $appNames */
68-
$appNames = $this->appRepository->aggregate($criteria, Context::createDefaultContext())->get('appNames');
69-
70-
return $appNames->getKeys();
7159
}
7260
}

src/Core/Framework/Adapter/Twig/TemplateFinder.php

+8-23
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,15 @@
1010

1111
class TemplateFinder implements TemplateFinderInterface
1212
{
13-
/**
14-
* @var Environment
15-
*/
16-
protected $twig;
13+
private Environment $twig;
1714

18-
/**
19-
* @var LoaderInterface
20-
*/
21-
protected $loader;
15+
private LoaderInterface $loader;
2216

23-
/**
24-
* @var array
25-
*/
26-
protected $namespaceHierarchy;
17+
private array $namespaceHierarchy = [];
2718

28-
/**
29-
* @var string
30-
*/
31-
protected $cacheDir;
19+
private string $cacheDir;
3220

33-
/**
34-
* @var NamespaceHierarchyBuilder
35-
*/
36-
private $namespaceHierarchyBuilder;
21+
private NamespaceHierarchyBuilder $namespaceHierarchyBuilder;
3722

3823
public function __construct(
3924
Environment $twig,
@@ -129,16 +114,16 @@ private function getNamespaceHierarchy(): array
129114
return $this->namespaceHierarchy;
130115
}
131116

132-
$namespaceHierarchy = array_unique($this->namespaceHierarchyBuilder->buildHierarchy());
117+
$namespaceHierarchy = $this->namespaceHierarchyBuilder->buildHierarchy();
133118
$this->defineCache($namespaceHierarchy);
134119

135-
return $this->namespaceHierarchy = $namespaceHierarchy;
120+
return $this->namespaceHierarchy = array_keys($namespaceHierarchy);
136121
}
137122

138123
private function defineCache(array $queue): void
139124
{
140125
if ($this->twig->getCache(false) instanceof FilesystemCache) {
141-
$configHash = implode(':', $queue);
126+
$configHash = md5((string) json_encode($queue));
142127

143128
$fileSystemCache = new ConfigurableFilesystemCache($this->cacheDir);
144129
$fileSystemCache->setConfigHash($configHash);

src/Core/Framework/DependencyInjection/services.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ base-uri 'self';
323323

324324
<service id="Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder">
325325
<argument type="service" id="kernel"/>
326-
<argument type="service" id="app.repository"/>
326+
<argument type="service" id="Doctrine\DBAL\Connection"/>
327327

328328
<tag name="shopware.twig.hierarchy_builder" priority="1000"/>
329329
</service>

src/Core/Framework/Test/Adapter/Twig/NamespaceHierarchy/BundleHierarchyBuilderTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testItAddsAppNamespace(): void
5252
'Storefront',
5353
'Administration',
5454
'Framework',
55-
], $bundleHierarchyBuilder->buildNamespaceHierarchy([]));
55+
], array_keys($bundleHierarchyBuilder->buildNamespaceHierarchy([])));
5656
}
5757

5858
public function testItExcludesInactiveApps(): void
@@ -93,7 +93,7 @@ public function testItExcludesInactiveApps(): void
9393
'Storefront',
9494
'Administration',
9595
'Framework',
96-
], $bundleHierarchyBuilder->buildNamespaceHierarchy([]));
96+
], array_keys($bundleHierarchyBuilder->buildNamespaceHierarchy([])));
9797
}
9898

9999
public function testItExcludesInactiveAppTemplates(): void
@@ -135,7 +135,7 @@ public function testItExcludesInactiveAppTemplates(): void
135135
'Storefront',
136136
'Administration',
137137
'Framework',
138-
], $bundleHierarchyBuilder->buildNamespaceHierarchy([]));
138+
], array_keys($bundleHierarchyBuilder->buildNamespaceHierarchy([])));
139139
}
140140

141141
public function testItExcludesAppNamespacesWithNoTemplates(): void
@@ -170,6 +170,6 @@ public function testItExcludesAppNamespacesWithNoTemplates(): void
170170
'Storefront',
171171
'Administration',
172172
'Framework',
173-
], $bundleHierarchyBuilder->buildNamespaceHierarchy([]));
173+
], array_keys($bundleHierarchyBuilder->buildNamespaceHierarchy([])));
174174
}
175175
}

src/Core/Framework/Test/Adapter/Twig/NamespaceHierarchy/NamespaceHierarchyBuilderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ public function testItAddsAppTemplateNamespaces(): void
6969
'Storefront',
7070
'Administration',
7171
'Framework',
72-
], $hierarchyBuilder->buildHierarchy());
72+
], array_keys($hierarchyBuilder->buildHierarchy()));
7373
}
7474
}

src/Core/Framework/Test/Adapter/Twig/TwigCacheTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Shopware\Core\Framework\Test\Adapter\Twig;
44

5+
use Doctrine\DBAL\Connection;
56
use PHPUnit\Framework\TestCase;
67
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder;
78
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\NamespaceHierarchyBuilder;
@@ -63,7 +64,7 @@ private function createFinder(array $bundles): array
6364
new NamespaceHierarchyBuilder([
6465
new BundleHierarchyBuilder(
6566
$kernel,
66-
$this->getContainer()->get('app.repository')
67+
$this->getContainer()->get(Connection::class)
6768
),
6869
])
6970
);

src/Core/Framework/Test/Adapter/Twig/TwigSwExtendsTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Shopware\Core\Framework\Test\Adapter\Twig;
44

5+
use Doctrine\DBAL\Connection;
56
use PHPUnit\Framework\TestCase;
67
use Shopware\Core\Framework\Adapter\Twig\InheritanceExtension;
78
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder;
@@ -170,7 +171,7 @@ private function createFinder(array $bundles): array
170171
new NamespaceHierarchyBuilder([
171172
new BundleHierarchyBuilder(
172173
$kernel,
173-
$this->getContainer()->get('app.repository')
174+
$this->getContainer()->get(Connection::class)
174175
),
175176
])
176177
);

src/Core/Framework/Test/Adapter/Twig/TwigSwIncludeTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Shopware\Core\Framework\Test\Adapter\Twig;
44

5+
use Doctrine\DBAL\Connection;
56
use PHPUnit\Framework\TestCase;
67
use Shopware\Core\Framework\Adapter\Twig\InheritanceExtension;
78
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder;
@@ -124,7 +125,7 @@ private function initTwig(array $bundles): Environment
124125
new NamespaceHierarchyBuilder([
125126
new BundleHierarchyBuilder(
126127
$kernel,
127-
$this->getContainer()->get('app.repository')
128+
$this->getContainer()->get(Connection::class)
128129
),
129130
])
130131
);

src/Core/Framework/Test/App/StorefrontPluginRegistryTestBehaviour.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function clearTemplateFinderNamespaceHierarchyCache(): void
5151
$prop = $reflection->getProperty('namespaceHierarchy');
5252

5353
$prop->setAccessible(true);
54-
$prop->setValue($templateFinder, null);
54+
$prop->setValue($templateFinder, []);
5555
}
5656

5757
abstract protected function getContainer(): ContainerInterface;

src/Core/Kernel.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,21 @@ protected function getKernelParameters(): array
285285
);
286286
}
287287

288-
protected function getCacheHash()
288+
protected function getCacheHash(): string
289289
{
290-
$pluginHash = md5(implode('', array_keys($this->pluginLoader->getPluginInstances()->getActives())));
290+
$plugins = [];
291+
foreach ($this->pluginLoader->getPluginInfos() as $plugin) {
292+
if ($plugin['active'] === false) {
293+
continue;
294+
}
295+
$plugins[$plugin['name']] = $plugin['version'];
296+
}
297+
298+
$pluginHash = md5((string) json_encode($plugins));
291299

292-
return md5(json_encode([
300+
return md5((string) json_encode([
293301
$this->cacheId,
294-
mb_substr($this->shopwareVersionRevision, 0, 8),
302+
mb_substr((string) $this->shopwareVersionRevision, 0, 8),
295303
mb_substr($pluginHash, 0, 8),
296304
]));
297305
}

0 commit comments

Comments
 (0)