Skip to content

Commit 26112a2

Browse files
authored
feat: add support for shopware 6.5.6
1 parent bc61a67 commit 26112a2

File tree

5 files changed

+100
-27
lines changed

5 files changed

+100
-27
lines changed

CHANGELOG_en-GB.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 4.0.1
2+
* Add support for Shopware 6.5.6 with changed private methods
3+
14
# 4.0.0
25
* Add unit and integration tests
36
* Changed Testbutton to show error text above the image

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"thumbnail"
1212
],
1313
"description": "This plugins allows you to use variable thumbnails, without having them on storage.",
14-
"version": "4.0.0",
14+
"version": "4.0.1",
1515
"type": "shopware-platform-plugin",
1616
"license": "mit",
1717
"authors": [

phpstan.neon

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ parameters:
1616
message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#"
1717
path: src/DependencyInjection/ThumbnailService.php
1818

19+
-
20+
message: "#^Anonymous function should return array but returns mixed.#"
21+
path: src/DependencyInjection/ThumbnailService.php
22+
reportUnmatched: false
23+
1924
-
2025
message: '#^Call to deprecated method#'
2126
path: src/DependencyInjection/FileSaver.php
@@ -29,7 +34,6 @@ parameters:
2934
message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#"
3035
reportUnmatched: false
3136

32-
# NEXT-29041 - Needs to be fixed with a script, rest goes to baseline
3337
-
3438
message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#'
3539
reportUnmatched: false
@@ -42,3 +46,7 @@ parameters:
4246
-
4347
message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest(.*)does not accept object\\|null\\.$#"
4448
path: tests/integration/MediaUrlTest.php
49+
50+
-
51+
message: "#Use AbstractMediaUrlGenerator instead#"
52+
reportUnmatched: false

src/DependencyInjection/GeneratorCompilerPass.php

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,17 @@ public function process(ContainerBuilder $container): void
8888
*/
8989
private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): void
9090
{
91-
$createThumbnailsForSizesNode = $this->getClassMethod($nodeFinder, 'createThumbnailsForSizes', $ast);
92-
93-
// we don't need to generate the files, so we just return the array
94-
$createThumbnailsForSizesNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7)
95-
->parse('<?php if ($thumbnailSizes === null) {
96-
return [];
97-
}
98-
99-
if ($thumbnailSizes->count() === 0) {
100-
return [];
101-
}
102-
103-
$savedThumbnails = [];
104-
105-
foreach ($thumbnailSizes as $size) {
106-
$savedThumbnails[] = [
107-
\'mediaId\' => $media->getId(),
108-
\'width\' => $size->getWidth(),
109-
\'height\' => $size->getHeight(),
110-
];
111-
}
112-
113-
return $savedThumbnails;');
91+
try {
92+
$createThumbnailsForSizesNode = $this->getClassMethod($nodeFinder, 'createThumbnailsForSizes', $ast);
93+
$this->handleCreateThumbnailsForSizes($createThumbnailsForSizesNode);
94+
} catch (\RuntimeException $e) {
95+
if ($e->getMessage() === 'Method createThumbnailsForSizes in class Shopware\Core\Content\Media\Thumbnail\ThumbnailService is missing') {
96+
$generateAndSaveNode = $this->getClassMethod($nodeFinder, 'generateAndSave', $ast);
97+
$this->handleGenerateAndSaveNode($generateAndSaveNode);
98+
} else {
99+
throw $e;
100+
}
101+
}
114102

115103
// the strict option is useless with this plugin, so this should always be false
116104
$updateThumbnailsNode = $this->getClassMethod($nodeFinder, 'updateThumbnails', $ast);
@@ -232,4 +220,73 @@ private function getClassName(): string
232220

233221
return substr($lastOccur, 1);
234222
}
223+
224+
private function handleCreateThumbnailsForSizes(ClassMethod $createThumbnailsForSizesNode): void
225+
{
226+
// we don't need to generate the files, so we just return the array
227+
$createThumbnailsForSizesNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7)
228+
->parse('<?php if ($thumbnailSizes === null) {
229+
return [];
230+
}
231+
232+
if ($thumbnailSizes->count() === 0) {
233+
return [];
234+
}
235+
236+
$savedThumbnails = [];
237+
238+
foreach ($thumbnailSizes as $size) {
239+
$savedThumbnails[] = [
240+
\'mediaId\' => $media->getId(),
241+
\'width\' => $size->getWidth(),
242+
\'height\' => $size->getHeight(),
243+
];
244+
}
245+
246+
return $savedThumbnails;');
247+
}
248+
249+
private function handleGenerateAndSaveNode(ClassMethod $generateAndSaveNode): void
250+
{
251+
// we don't need to generate the files, so we just return the array
252+
$generateAndSaveNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7)
253+
->parse('<?php if ($sizes === null || $sizes->count() === 0) {
254+
return [];
255+
}
256+
257+
$records = [];
258+
259+
$type = $media->getMediaType();
260+
if ($type === null) {
261+
throw MediaException::mediaTypeNotLoaded($media->getId());
262+
}
263+
264+
$mapped = [];
265+
foreach ($sizes as $size) {
266+
$id = Uuid::randomHex();
267+
268+
$mapped[$size->getId()] = $id;
269+
270+
$records[] = [
271+
\'id\' => $id,
272+
\'mediaId\' => $media->getId(),
273+
\'width\' => $size->getWidth(),
274+
\'height\' => $size->getHeight(),
275+
];
276+
}
277+
278+
// write thumbnail records to trigger path generation afterward
279+
$context->scope(Context::SYSTEM_SCOPE, function ($context) use ($records): void {
280+
$context->addState(EntityIndexerRegistry::DISABLE_INDEXING);
281+
282+
$this->thumbnailRepository->create($records, $context);
283+
});
284+
285+
$ids = \array_column($records, \'id\');
286+
287+
// triggers the path generation for the persisted thumbnails
288+
$this->dispatcher->dispatch(new UpdateThumbnailPathEvent($ids));
289+
290+
return $records;');
291+
}
235292
}

src/Service/UrlGeneratorDecorator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
use League\Flysystem\FilesystemOperator;
66
use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
77
use Shopware\Core\Content\Media\MediaEntity;
8+
use Shopware\Core\Content\Media\Pathname\UrlGenerator;
89
use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
9-
use Symfony\Contracts\Service\ResetInterface;
1010

11-
class UrlGeneratorDecorator implements UrlGeneratorInterface, ResetInterface
11+
/**
12+
* We have to extend here, otherwise this would be thrown:
13+
* Shopware\Core\Content\Media\Core\Strategy\BCStrategy::__construct(): Argument #3 ($generator) must be of type Shopware\Core\Content\Media\Pathname\UrlGenerator, Frosh\ThumbnailProcessor\Service\UrlGeneratorDecorator given
14+
* TODO: check PR https://github.com/shopware/platform/pull/3337
15+
*/
16+
class UrlGeneratorDecorator extends UrlGenerator
1217
{
1318
/**
1419
* @var array<string>|null

0 commit comments

Comments
 (0)