diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index 5edea04..62d3d17 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,6 @@ +# 4.0.1 +* Add support for Shopware 6.5.6 with changed private methods + # 4.0.0 * Add unit and integration tests * Changed Testbutton to show error text above the image diff --git a/composer.json b/composer.json index 28efcce..0017b65 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "thumbnail" ], "description": "This plugins allows you to use variable thumbnails, without having them on storage.", - "version": "4.0.0", + "version": "4.0.1", "type": "shopware-platform-plugin", "license": "mit", "authors": [ diff --git a/phpstan.neon b/phpstan.neon index 79051d3..48d0fd3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -16,6 +16,11 @@ parameters: message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#" path: src/DependencyInjection/ThumbnailService.php + - + message: "#^Anonymous function should return array but returns mixed.#" + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + - message: '#^Call to deprecated method#' path: src/DependencyInjection/FileSaver.php @@ -29,7 +34,6 @@ parameters: message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#" reportUnmatched: false - # NEXT-29041 - Needs to be fixed with a script, rest goes to baseline - message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#' reportUnmatched: false @@ -42,3 +46,7 @@ parameters: - message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest(.*)does not accept object\\|null\\.$#" path: tests/integration/MediaUrlTest.php + + - + message: "#Use AbstractMediaUrlGenerator instead#" + reportUnmatched: false diff --git a/src/DependencyInjection/GeneratorCompilerPass.php b/src/DependencyInjection/GeneratorCompilerPass.php index 608ee5d..cd0f9e9 100644 --- a/src/DependencyInjection/GeneratorCompilerPass.php +++ b/src/DependencyInjection/GeneratorCompilerPass.php @@ -88,29 +88,17 @@ public function process(ContainerBuilder $container): void */ private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): void { - $createThumbnailsForSizesNode = $this->getClassMethod($nodeFinder, 'createThumbnailsForSizes', $ast); - - // we don't need to generate the files, so we just return the array - $createThumbnailsForSizesNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7) - ->parse('count() === 0) { - return []; - } - - $savedThumbnails = []; - - foreach ($thumbnailSizes as $size) { - $savedThumbnails[] = [ - \'mediaId\' => $media->getId(), - \'width\' => $size->getWidth(), - \'height\' => $size->getHeight(), - ]; - } - - return $savedThumbnails;'); + try { + $createThumbnailsForSizesNode = $this->getClassMethod($nodeFinder, 'createThumbnailsForSizes', $ast); + $this->handleCreateThumbnailsForSizes($createThumbnailsForSizesNode); + } catch (\RuntimeException $e) { + if ($e->getMessage() === 'Method createThumbnailsForSizes in class Shopware\Core\Content\Media\Thumbnail\ThumbnailService is missing') { + $generateAndSaveNode = $this->getClassMethod($nodeFinder, 'generateAndSave', $ast); + $this->handleGenerateAndSaveNode($generateAndSaveNode); + } else { + throw $e; + } + } // the strict option is useless with this plugin, so this should always be false $updateThumbnailsNode = $this->getClassMethod($nodeFinder, 'updateThumbnails', $ast); @@ -232,4 +220,73 @@ private function getClassName(): string return substr($lastOccur, 1); } + + private function handleCreateThumbnailsForSizes(ClassMethod $createThumbnailsForSizesNode): void + { + // we don't need to generate the files, so we just return the array + $createThumbnailsForSizesNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7) + ->parse('count() === 0) { + return []; + } + + $savedThumbnails = []; + + foreach ($thumbnailSizes as $size) { + $savedThumbnails[] = [ + \'mediaId\' => $media->getId(), + \'width\' => $size->getWidth(), + \'height\' => $size->getHeight(), + ]; + } + + return $savedThumbnails;'); + } + + private function handleGenerateAndSaveNode(ClassMethod $generateAndSaveNode): void + { + // we don't need to generate the files, so we just return the array + $generateAndSaveNode->stmts = (new ParserFactory())->create(ParserFactory::PREFER_PHP7) + ->parse('count() === 0) { + return []; + } + + $records = []; + + $type = $media->getMediaType(); + if ($type === null) { + throw MediaException::mediaTypeNotLoaded($media->getId()); + } + + $mapped = []; + foreach ($sizes as $size) { + $id = Uuid::randomHex(); + + $mapped[$size->getId()] = $id; + + $records[] = [ + \'id\' => $id, + \'mediaId\' => $media->getId(), + \'width\' => $size->getWidth(), + \'height\' => $size->getHeight(), + ]; + } + + // write thumbnail records to trigger path generation afterward + $context->scope(Context::SYSTEM_SCOPE, function ($context) use ($records): void { + $context->addState(EntityIndexerRegistry::DISABLE_INDEXING); + + $this->thumbnailRepository->create($records, $context); + }); + + $ids = \array_column($records, \'id\'); + + // triggers the path generation for the persisted thumbnails + $this->dispatcher->dispatch(new UpdateThumbnailPathEvent($ids)); + + return $records;'); + } } diff --git a/src/Service/UrlGeneratorDecorator.php b/src/Service/UrlGeneratorDecorator.php index 063cf07..5b2c0ca 100644 --- a/src/Service/UrlGeneratorDecorator.php +++ b/src/Service/UrlGeneratorDecorator.php @@ -5,10 +5,15 @@ use League\Flysystem\FilesystemOperator; use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity; use Shopware\Core\Content\Media\MediaEntity; +use Shopware\Core\Content\Media\Pathname\UrlGenerator; use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface; -use Symfony\Contracts\Service\ResetInterface; -class UrlGeneratorDecorator implements UrlGeneratorInterface, ResetInterface +/** + * We have to extend here, otherwise this would be thrown: + * 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 + * TODO: check PR https://github.com/shopware/platform/pull/3337 + */ +class UrlGeneratorDecorator extends UrlGenerator { /** * @var array|null