Skip to content

Commit

Permalink
feat: add support for shopware 6.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect authored Oct 2, 2023
1 parent bc61a67 commit 26112a2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 27 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 @@
# 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
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": "4.0.0",
"version": "4.0.1",
"type": "shopware-platform-plugin",
"license": "mit",
"authors": [
Expand Down
10 changes: 9 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
103 changes: 80 additions & 23 deletions src/DependencyInjection/GeneratorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php if ($thumbnailSizes === null) {
return [];
}
if ($thumbnailSizes->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);
Expand Down Expand Up @@ -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('<?php if ($thumbnailSizes === null) {
return [];
}
if ($thumbnailSizes->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('<?php if ($sizes === null || $sizes->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;');
}
}
9 changes: 7 additions & 2 deletions src/Service/UrlGeneratorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>|null
Expand Down

0 comments on commit 26112a2

Please sign in to comment.