Skip to content

Commit 8d4139c

Browse files
committed
[LazyImage] Re-add forgotten twig.runtime
1 parent 80b36bc commit 8d4139c

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

src/LazyImage/src/DependencyInjection/LazyImageExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\UX\LazyImage\BlurHash\BlurHash;
2424
use Symfony\UX\LazyImage\BlurHash\BlurHashInterface;
2525
use Symfony\UX\LazyImage\Twig\BlurHashExtension;
26+
use Symfony\UX\LazyImage\Twig\BlurHashRuntime;
2627

2728
/**
2829
* @author Titouan Galopin <galopintitouan@gmail.com>
@@ -60,10 +61,16 @@ public function load(array $configs, ContainerBuilder $container)
6061

6162
$container
6263
->setDefinition('twig.extension.blur_hash', new Definition(BlurHashExtension::class))
63-
->addArgument(new Reference('lazy_image.blur_hash'))
6464
->addTag('twig.extension')
6565
->setPublic(false)
6666
;
67+
68+
$container
69+
->setDefinition('twig.runtime.blur_hash', new Definition(BlurHashRuntime::class))
70+
->addArgument(new Reference('lazy_image.blur_hash'))
71+
->addTag('twig.runtime')
72+
->setPublic(false)
73+
;
6774
}
6875

6976
public function prepend(ContainerBuilder $container)

src/LazyImage/src/Twig/BlurHashExtension.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\UX\LazyImage\Twig;
1313

14-
use Symfony\UX\LazyImage\BlurHash\BlurHashInterface;
1514
use Twig\Extension\AbstractExtension;
1615
use Twig\TwigFunction;
1716

@@ -22,28 +21,9 @@
2221
*/
2322
class BlurHashExtension extends AbstractExtension
2423
{
25-
private $blurHash;
26-
27-
public function __construct(BlurHashInterface $blurHash)
28-
{
29-
$this->blurHash = $blurHash;
30-
}
31-
32-
public function getFunctions(): array
33-
{
34-
return [
35-
new TwigFunction('data_uri_thumbnail', [$this, 'createDataUriThumbnail']),
36-
new TwigFunction('blur_hash', [$this, 'blurHash']),
37-
];
38-
}
39-
40-
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
41-
{
42-
return $this->blurHash->createDataUriThumbnail($filename, $width, $height, $encodingWidth, $encodingHeight);
43-
}
44-
45-
public function blurHash(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
24+
public function getFunctions(): iterable
4625
{
47-
return $this->blurHash->encode($filename, $encodingWidth, $encodingHeight);
26+
yield new TwigFunction('data_uri_thumbnail', [BlurHashRuntime::class, 'createDataUriThumbnail']);
27+
yield new TwigFunction('blur_hash', [BlurHashRuntime::class, 'blurHash']);
4828
}
4929
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LazyImage\Twig;
13+
14+
use Symfony\UX\LazyImage\BlurHash\BlurHashInterface;
15+
use Twig\Extension\RuntimeExtensionInterface;
16+
17+
/**
18+
* @author Hugo Alliaume <hugo@alliau.me>
19+
*/
20+
final class BlurHashRuntime implements RuntimeExtensionInterface
21+
{
22+
public function __construct(
23+
private BlurHashInterface $blurHash,
24+
) {
25+
}
26+
27+
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
28+
{
29+
return $this->blurHash->createDataUriThumbnail($filename, $width, $height, $encodingWidth, $encodingHeight);
30+
}
31+
32+
public function blurHash(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
33+
{
34+
return $this->blurHash->encode($filename, $encodingWidth, $encodingHeight);
35+
}
36+
}

0 commit comments

Comments
 (0)