Skip to content

Commit c852c5a

Browse files
committed
minor #1771 [LazyImage] Re-add forgotten twig.runtime (Kocal)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LazyImage] Re-add forgotten twig.runtime | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Hi! It looks like the Twig Runtime has been removed in #1766 while processing my review, so let's add it back :) Commits ------- 6f92d33 [LazyImage] Re-add forgotten twig.runtime
2 parents 80b36bc + 6f92d33 commit c852c5a

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

src/LazyImage/src/DependencyInjection/LazyImageExtension.php

Lines changed: 7 additions & 3 deletions
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 <[email protected]>
@@ -40,7 +41,6 @@ public function load(array $configs, ContainerBuilder $container)
4041
$container
4142
->setDefinition('lazy_image.image_manager', new Definition(ImageManager::class))
4243
->addArgument(BlurHash::intervention3() ? Driver::class : [])
43-
->setPublic(false)
4444
;
4545
}
4646

@@ -60,9 +60,13 @@ public function load(array $configs, ContainerBuilder $container)
6060

6161
$container
6262
->setDefinition('twig.extension.blur_hash', new Definition(BlurHashExtension::class))
63-
->addArgument(new Reference('lazy_image.blur_hash'))
6463
->addTag('twig.extension')
65-
->setPublic(false)
64+
;
65+
66+
$container
67+
->setDefinition('twig.runtime.blur_hash', new Definition(BlurHashRuntime::class))
68+
->addArgument(new Reference('lazy_image.blur_hash'))
69+
->addTag('twig.runtime')
6670
;
6771
}
6872

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 <[email protected]>
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 <[email protected]>
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)