diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index 35a49af..94994d3 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,6 @@ +# 3.0.2 +* Add auto update of thumbnails after changing thumbnailSizes: no more need to run "media:generate-thumbnails" + # 3.0.1 * Add configuration to set maximum size for original image * Add configuration to specify file extension which can be processed diff --git a/README.md b/README.md index f39e533..bd0f714 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ You may want to delete folder `thumbnails` within folder `public` ## Adding more thumbnail sizes: - Save new size in the folder of the media management -- then run the command `bin/console media:generate-thumbnails` on the console to update the thumbnails for all images in the database +- (up to and including plugin version 3.0.1) then run the command `bin/console media:generate-thumbnails` on the console to update the thumbnails for all images in the database - Clear shop cache ## Find Patterns diff --git a/composer.json b/composer.json index fd59635..aeff444 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": "3.0.1", + "version": "3.0.2", "type": "shopware-platform-plugin", "license": "mit", "authors": [ diff --git a/phpstan.neon b/phpstan.neon index 90367d2..a9c7019 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,5 +2,17 @@ parameters: level: 9 paths: - src - excludePaths: - - src/DependencyInjection/ThumbnailService.php + ignoreErrors: + - + message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + + - + message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php + + - + message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#" + path: src/DependencyInjection/ThumbnailService.php diff --git a/src/DependencyInjection/GeneratorCompilerPass.php b/src/DependencyInjection/GeneratorCompilerPass.php index cb904a6..608ee5d 100644 --- a/src/DependencyInjection/GeneratorCompilerPass.php +++ b/src/DependencyInjection/GeneratorCompilerPass.php @@ -92,7 +92,11 @@ private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): voi // 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) { + ->parse('count() === 0) { return []; } diff --git a/src/EventListener/ThumbnailSizesChangedListener.php b/src/EventListener/ThumbnailSizesChangedListener.php new file mode 100644 index 0000000..f2171cc --- /dev/null +++ b/src/EventListener/ThumbnailSizesChangedListener.php @@ -0,0 +1,65 @@ + 'onThumbnailSizeChanged', + MediaFolderConfigurationMediaThumbnailSizeDefinition::ENTITY_NAME . '.deleted' => 'onThumbnailSizeChanged', + ]; + } + + public function onThumbnailSizeChanged(EntityWrittenEvent $event): void + { + $updatedMediaFolderConfigurationIds = []; + + foreach ($event->getWriteResults() as $writeResult) { + $mediaFolderConfigurationId = $writeResult->getPayload()['mediaFolderConfigurationId'] ?? null; + + if (!empty($mediaFolderConfigurationId) && \is_string($mediaFolderConfigurationId)) { + $updatedMediaFolderConfigurationIds[] = $mediaFolderConfigurationId; + } + } + + $updatedMediaFolderConfigurationIds = \array_unique($updatedMediaFolderConfigurationIds); + + if (empty($updatedMediaFolderConfigurationIds)) { + return; + } + + $criteria = new Criteria(); + $criteria->addFilter(new EqualsAnyFilter('configurationId', $updatedMediaFolderConfigurationIds)); + + $result = $this->mediaFolderRepository->search($criteria, $event->getContext()); + + /** @var MediaFolderEntity $entity */ + foreach ($result->getEntities() as $entity) { + $parameters = []; + + $parameters['--async'] = null; + $parameters['--folder-name'] = $entity->getName(); + + $this->generateThumbnailsCommand->run(new ArrayInput($parameters, $this->generateThumbnailsCommand->getDefinition()), new NullOutput()); + } + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index c3bbd3b..836324b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -30,5 +30,11 @@ + + + + + +