Skip to content

Commit

Permalink
feat: update thumbnails after thumbnailSizes have been changed
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect authored Jun 17, 2023
1 parent 2af3a60 commit ee5b9fe
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
16 changes: 14 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\>\\|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
6 changes: 5 additions & 1 deletion src/DependencyInjection/GeneratorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php if ($thumbnailSizes->count() === 0) {
->parse('<?php if ($thumbnailSizes === null) {
return [];
}
if ($thumbnailSizes->count() === 0) {
return [];
}
Expand Down
65 changes: 65 additions & 0 deletions src/EventListener/ThumbnailSizesChangedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace Frosh\ThumbnailProcessor\EventListener;

use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
use Shopware\Core\Content\Media\Aggregate\MediaFolderConfigurationMediaThumbnailSize\MediaFolderConfigurationMediaThumbnailSizeDefinition;
use Shopware\Core\Content\Media\Commands\GenerateThumbnailsCommand;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ThumbnailSizesChangedListener implements EventSubscriberInterface
{
public function __construct(
private readonly GenerateThumbnailsCommand $generateThumbnailsCommand,
private readonly EntityRepository $mediaFolderRepository,
) {
}

public static function getSubscribedEvents(): array
{
return [
MediaFolderConfigurationMediaThumbnailSizeDefinition::ENTITY_NAME . '.written' => '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());
}
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
<argument type="service" id="request_stack"/>
<argument type="service" id="product_export.repository"/>
</service>

<service id="Frosh\ThumbnailProcessor\EventListener\ThumbnailSizesChangedListener">
<argument type="service" id="Shopware\Core\Content\Media\Commands\GenerateThumbnailsCommand"/>
<argument type="service" id="media_folder.repository"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>

0 comments on commit ee5b9fe

Please sign in to comment.