8000 Define Twig runtime extension with attributes · symfony/symfony@edbf868 · GitHub
[go: up one dir, main page]

Skip to content

Commit edbf868

Browse files
committed
Define Twig runtime extension with attributes
1 parent a3686fb commit edbf868

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Enable `#[AsTwigFilter]`, `#[AsTwigFunction]` and `#[AsTwigTest]` attributes to configure runtime extensions
8+
49
7.0
510
---
611

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/RuntimeLoaderPass.php

+5
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ public function process(ContainerBuilder $container): void
3535
}
3636

3737
$definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $mapping));
38+
39+
// Scan all Twig runtimes for attributes
40+
if ($container->hasDefinition('twig.extension.runtime')) {
41+
$container->getDefinition('twig.extension.runtime')->replaceArgument(0, array_keys($mapping));
42+
}
3843
}
3944
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Translation\LocaleSwitcher;
2626
use Symfony\Component\Translation\Translator;
2727
use Symfony\Contracts\Service\ResetInterface;
28+
use Twig\Extension\AttributeExtension;
2829
use Twig\Extension\ExtensionInterface;
2930
use Twig\Extension\RuntimeExtensionInterface;
3031
use Twig\Loader\LoaderInterface;
@@ -177,6 +178,11 @@ public function load(array $configs, ContainerBuilder $container): void
177178
if (false === $config['cache']) {
178179
$container->removeDefinition('twig.template_cache_warmer');
179180
}
181+
182+
// Attributes declaration requires Twig 3.9+
183+
if (!class_exists(AttributeExtension::class)) {
184+
$container->removeDefinition('twig.extension.runtime');
185+
}
180186
}
181187

182188
private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array

src/Symfony/Bundle/TwigBundle/Resources/config/twig.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Bundle\TwigBundle\TemplateIterator;
3838
use Twig\Cache\FilesystemCache;
3939
use Twig\Environment;
40+
use Twig\Extension\AttributeExtension;
4041
use Twig\Extension\CoreExtension;
4142
use Twig\Extension\DebugExtension;
4243
use Twig\Extension\EscaperExtension;
@@ -151,6 +152,10 @@
151152
->set('twig.runtime_loader', ContainerRuntimeLoader::class)
152153
->args([abstract_arg('runtime locator')])
153154

155+
->set('twig.extension.runtime', AttributeExtension::class)
156+
->args([abstract_arg('runtime classes')])
157+
->tag('twig.extension')
158+
154159
->set('twig.error_renderer.html', TwigErrorRenderer::class)
155160
->decorate('error_renderer.html')
156161
->args([

0 commit comments

Comments
 (0)
0