From 6f92d3336c4a9f6e72b20f50717416b9bbac3655 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Tue, 23 Apr 2024 22:55:18 +0200 Subject: [PATCH] [LazyImage] Re-add forgotten twig.runtime --- .../LazyImageExtension.php | 10 ++++-- src/LazyImage/src/Twig/BlurHashExtension.php | 26 ++------------ src/LazyImage/src/Twig/BlurHashRuntime.php | 36 +++++++++++++++++++ 3 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 src/LazyImage/src/Twig/BlurHashRuntime.php diff --git a/src/LazyImage/src/DependencyInjection/LazyImageExtension.php b/src/LazyImage/src/DependencyInjection/LazyImageExtension.php index c24104587f6..490d415478f 100644 --- a/src/LazyImage/src/DependencyInjection/LazyImageExtension.php +++ b/src/LazyImage/src/DependencyInjection/LazyImageExtension.php @@ -23,6 +23,7 @@ use Symfony\UX\LazyImage\BlurHash\BlurHash; use Symfony\UX\LazyImage\BlurHash\BlurHashInterface; use Symfony\UX\LazyImage\Twig\BlurHashExtension; +use Symfony\UX\LazyImage\Twig\BlurHashRuntime; /** * @author Titouan Galopin @@ -40,7 +41,6 @@ public function load(array $configs, ContainerBuilder $container) $container ->setDefinition('lazy_image.image_manager', new Definition(ImageManager::class)) ->addArgument(BlurHash::intervention3() ? Driver::class : []) - ->setPublic(false) ; } @@ -60,9 +60,13 @@ public function load(array $configs, ContainerBuilder $container) $container ->setDefinition('twig.extension.blur_hash', new Definition(BlurHashExtension::class)) - ->addArgument(new Reference('lazy_image.blur_hash')) ->addTag('twig.extension') - ->setPublic(false) + ; + + $container + ->setDefinition('twig.runtime.blur_hash', new Definition(BlurHashRuntime::class)) + ->addArgument(new Reference('lazy_image.blur_hash')) + ->addTag('twig.runtime') ; } diff --git a/src/LazyImage/src/Twig/BlurHashExtension.php b/src/LazyImage/src/Twig/BlurHashExtension.php index caeb35e5fca..cd872ecbc66 100644 --- a/src/LazyImage/src/Twig/BlurHashExtension.php +++ b/src/LazyImage/src/Twig/BlurHashExtension.php @@ -11,7 +11,6 @@ namespace Symfony\UX\LazyImage\Twig; -use Symfony\UX\LazyImage\BlurHash\BlurHashInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -22,28 +21,9 @@ */ class BlurHashExtension extends AbstractExtension { - private $blurHash; - - public function __construct(BlurHashInterface $blurHash) - { - $this->blurHash = $blurHash; - } - - public function getFunctions(): array - { - return [ - new TwigFunction('data_uri_thumbnail', [$this, 'createDataUriThumbnail']), - new TwigFunction('blur_hash', [$this, 'blurHash']), - ]; - } - - public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string - { - return $this->blurHash->createDataUriThumbnail($filename, $width, $height, $encodingWidth, $encodingHeight); - } - - public function blurHash(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string + public function getFunctions(): iterable { - return $this->blurHash->encode($filename, $encodingWidth, $encodingHeight); + yield new TwigFunction('data_uri_thumbnail', [BlurHashRuntime::class, 'createDataUriThumbnail']); + yield new TwigFunction('blur_hash', [BlurHashRuntime::class, 'blurHash']); } } diff --git a/src/LazyImage/src/Twig/BlurHashRuntime.php b/src/LazyImage/src/Twig/BlurHashRuntime.php new file mode 100644 index 00000000000..49d931cab8b --- /dev/null +++ b/src/LazyImage/src/Twig/BlurHashRuntime.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LazyImage\Twig; + +use Symfony\UX\LazyImage\BlurHash\BlurHashInterface; +use Twig\Extension\RuntimeExtensionInterface; + +/** + * @author Hugo Alliaume + */ +final class BlurHashRuntime implements RuntimeExtensionInterface +{ + public function __construct( + private BlurHashInterface $blurHash, + ) { + } + + public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string + { + return $this->blurHash->createDataUriThumbnail($filename, $width, $height, $encodingWidth, $encodingHeight); + } + + public function blurHash(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string + { + return $this->blurHash->encode($filename, $encodingWidth, $encodingHeight); + } +}