|
4 | 4 |
|
5 | 5 | namespace Frosh\LazySizes\Storefront\Framework\Twig\Extension;
|
6 | 6 |
|
| 7 | +use Shopware\Storefront\Framework\Twig\Extension\UrlEncodingTwigFilter as ShopwareUrlEncodingTwigFilter; |
7 | 8 | use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
|
8 | 9 | use Twig\Extension\AbstractExtension;
|
9 | 10 | use Twig\TwigFilter;
|
10 | 11 |
|
11 | 12 | #[AutoconfigureTag('twig.extension')]
|
12 | 13 | class UrlEncodingTwigFilter extends AbstractExtension
|
13 | 14 | {
|
| 15 | + public function __construct( |
| 16 | + private readonly ShopwareUrlEncodingTwigFilter $urlEncodingTwigFilter |
| 17 | + ) {} |
| 18 | + |
14 | 19 | /**
|
15 | 20 | * @return TwigFilter[]
|
16 | 21 | */
|
17 | 22 | public function getFilters(): array
|
18 | 23 | {
|
19 | 24 | return [
|
20 |
| - new TwigFilter('frosh_encode_url', [$this, 'encodeUrl']), |
| 25 | + new TwigFilter('frosh_encode_url', $this->encodeUrl(...)), |
21 | 26 | ];
|
22 | 27 | }
|
23 | 28 |
|
24 | 29 | public function encodeUrl(?string $mediaUrl): ?string
|
25 | 30 | {
|
| 31 | + $mediaUrl = $this->urlEncodingTwigFilter->encodeUrl($mediaUrl); |
| 32 | + |
26 | 33 | if ($mediaUrl === null) {
|
27 | 34 | return null;
|
28 | 35 | }
|
29 | 36 |
|
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); |
80 | 39 | }
|
81 | 40 | }
|
0 commit comments