Skip to content

Commit 21b392f

Browse files
committed
refactor: update processing url encoding for supporting imgproxy
1 parent 336e3fd commit 21b392f

File tree

1 file changed

+10
-51
lines changed

1 file changed

+10
-51
lines changed

src/Storefront/Framework/Twig/Extension/UrlEncodingTwigFilter.php

+10-51
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,37 @@
44

55
namespace Frosh\LazySizes\Storefront\Framework\Twig\Extension;
66

7+
use Shopware\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter as ShopwareUrlEncodingTwigFilter;
78
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
89
use Twig\Extension\AbstractExtension;
910
use Twig\TwigFilter;
1011

1112
#[AutoconfigureTag('twig.extension')]
1213
class UrlEncodingTwigFilter extends AbstractExtension
1314
{
15+
public function __construct(
16+
private readonly ShopwareUrlEncodingTwigFilter $urlEncodingTwigFilter
17+
) {}
18+
1419
/**
1520
* @return TwigFilter[]
1621
*/
1722
public function getFilters(): array
1823
{
1924
return [
20-
new TwigFilter('frosh_encode_url', [$this, 'encodeUrl']),
25+
new TwigFilter('frosh_encode_url', $this->encodeUrl(...)),
2126
];
2227
}
2328

2429
public function encodeUrl(?string $mediaUrl): ?string
2530
{
31+
$mediaUrl = $this->urlEncodingTwigFilter->encodeUrl($mediaUrl);
32+
2633
if ($mediaUrl === null) {
2734
return null;
2835
}
2936

30-
$urlInfo = parse_url($mediaUrl);
31-
32-
if (!isset($urlInfo['path'])) {
33-
return $mediaUrl;
34-
}
35-
36-
$mediaParts = ['media', 'thumbnail'];
37-
38-
foreach ($mediaParts as $mediaPart) {
39-
// we encode just parts after "/media/" and "/thumbnail/" to add support for imgproxy and paths which always need to be encoded
40-
$paths = \explode(\sprintf('/%s/', $mediaPart), $urlInfo['path']);
41-
42-
if (count($paths) < 2) {
43-
continue;
44-
}
45-
46-
$paths[0] .= '/' . $mediaPart;
47-
48-
$relativeImagePath = $paths[1];
49-
50-
$relativeImagePathSegments = explode('/', $relativeImagePath);
51-
foreach ($relativeImagePathSegments as $index => $segment) {
52-
$relativeImagePathSegments[$index] = \rawurlencode($segment);
53-
}
54-
55-
$paths[1] = implode('/', $relativeImagePathSegments);
56-
57-
$path = implode('/', $paths);
58-
if (isset($urlInfo['query'])) {
59-
$path .= "?{$urlInfo['query']}";
60-
}
61-
62-
$encodedPath = '';
63-
64-
if (isset($urlInfo['scheme'])) {
65-
$encodedPath = "{$urlInfo['scheme']}://";
66-
}
67-
68-
if (isset($urlInfo['host'])) {
69-
$encodedPath .= "{$urlInfo['host']}";
70-
}
71-
72-
if (isset($urlInfo['port'])) {
73-
$encodedPath .= ":{$urlInfo['port']}";
74-
}
75-
76-
return $encodedPath . $path;
77-
}
78-
79-
return $mediaUrl;
37+
// this adds support for imgproxy with the procession options coming with version 3.0
38+
return \str_replace('%3A', ':', $mediaUrl);
8039
}
8140
}

0 commit comments

Comments
 (0)