Skip to content

[LazyImage] Re-add forgotten twig.runtime #1771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/LazyImage/src/DependencyInjection/LazyImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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)
;
}

Expand All @@ -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')
;
}

Expand Down
26 changes: 3 additions & 23 deletions src/LazyImage/src/Twig/BlurHashExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\UX\LazyImage\Twig;

use Symfony\UX\LazyImage\BlurHash\BlurHashInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -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']);
}
}
36 changes: 36 additions & 0 deletions src/LazyImage/src/Twig/BlurHashRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
}