Skip to content

Commit

Permalink
feat: decorate sw_encode_url to support imgproxy, update codestyle (#142
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tinect authored Aug 18, 2024
1 parent f139da9 commit 7ec4c8c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Core/Media/MediaUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MediaUrlGenerator extends AbstractMediaUrlGenerator
public function __construct(
private readonly AbstractMediaUrlGenerator $mediaUrlGenerator,
private readonly ThumbnailUrlTemplateInterface $thumbnailUrlTemplate,
private readonly FilesystemOperator $filesystem,
private readonly FilesystemOperator $publicFilesystem,
private readonly ConfigReader $configReader
) {
}
Expand Down Expand Up @@ -80,7 +80,7 @@ private function canProcessFileExtension(string $fileExtension): bool

private function getBaseUrl(): string
{
return \rtrim($this->filesystem->publicUrl(''), '/');
return \rtrim($this->publicFilesystem->publicUrl(''), '/');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Media/MediaUrlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Shopware\Core\Framework\DataAbstractionLayer\Entity;

class MediaUrlLoader
readonly class MediaUrlLoader
{
public function __construct(
private readonly MediaUrlGenerator $generator
private MediaUrlGenerator $generator
) {
}

Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
<argument type="service" id="Shopware\Core\Content\Media\File\FileSaver"/>
<argument type="service" id="Shopware\Core\Content\Media\File\FileFetcher"/>
</service>

<service id="Frosh\ThumbnailProcessor\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter"
decorates="Shopware\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter">
</service>
</services>
</container>
22 changes: 22 additions & 0 deletions src/Storefront/Framework/Twig/Extension/UrlEncodingTwigFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Frosh\ThumbnailProcessor\Storefront\Framework\Twig\Extension;

use Shopware\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter as ShopwareUrlEncodingTwigFilter;

class UrlEncodingTwigFilter extends ShopwareUrlEncodingTwigFilter
{
public function encodeUrl(?string $mediaUrl): ?string
{
$mediaUrl = parent::encodeUrl($mediaUrl);

if ($mediaUrl === null) {
return null;
}

// this adds support for imgproxy with the procession options coming with version 3.0
return \str_replace('%3A', ':', $mediaUrl);
}
}
1 change: 1 addition & 0 deletions tests/unit/Controller/Api/TestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\PlatformRequest;
use Symfony\Component\HttpFoundation\Request;

use function PHPUnit\Framework\assertSame;

class TestControllerTest extends TestCase
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Core/Media/MediaUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testGenerateWithUrlParam(): void
static::assertCount(1, $generatedPaths);
static::assertEquals('https://localhost/media/123.jpg?width=3000', current($generatedPaths));
}

public function testGenerateWithExtendedUrlParam(): void
{
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
Expand Down Expand Up @@ -293,4 +294,4 @@ public static function provideAllowedExtensions(): iterable
yield ['gif,png'];
yield [''];
}
}
}
2 changes: 1 addition & 1 deletion tests/unit/Core/Media/MediaUrlLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,4 @@ public static function provideInvalidThumbnailData(): iterable
yield [['']];
yield [new PartialEntity()];
}
}
}
4 changes: 2 additions & 2 deletions tests/unit/Service/ConfigReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testGetConfigActiveWithActiveTest(): void
}

/**
* @return iterable<array{null|string}>
* @return iterable<array{string|null}>
*/
public static function getSalesChannelIds(): iterable
{
Expand All @@ -129,7 +129,7 @@ public static function getWidths(): iterable
}

/**
* @return iterable<array{null|bool}>
* @return iterable<array{bool|null}>
*/
public static function getActiveValues(): iterable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Frosh\ThumbnailProcessor\Tests\Unit\Storefront\Framework\Twig\Extension;

use Frosh\ThumbnailProcessor\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter;
use PHPUnit\Framework\TestCase;

class UrlEncodingTwigFilterTest extends TestCase
{
public function testEncodeUrl(): void
{
$urlEncodingTwigFilter = new UrlEncodingTwigFilter();
$result = $urlEncodingTwigFilter->encodeUrl('https://example.com/w:1/image.jpg');
static::assertSame('https://example.com/w:1/image.jpg', $result);
}

public function testEncodeUrlNull(): void
{
$urlEncodingTwigFilter = new UrlEncodingTwigFilter();
$result = $urlEncodingTwigFilter->encodeUrl(null);
static::assertNull($result);
}
}

0 comments on commit 7ec4c8c

Please sign in to comment.