-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[TwigBundle] Enable #[AsTwigFilter]
, #[AsTwigFunction]
and #[AsTwigTest]
attributes to configure runtime extensions
#52748
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[TwigBundle] Enable
#[AsTwigFilter]
, #[AsTwigFunction]
and `#[AsT…
…wigTest]` attributes to configure runtime extensions
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Twig\Attribute\AsTwigFilter; | ||
use Twig\Attribute\AsTwigFunction; | ||
use Twig\Attribute\AsTwigTest; | ||
use Twig\Extension\AttributeExtension; | ||
|
||
/** | ||
* Register an instance of AttributeExtension for each service using the | ||
* PHP attributes to declare Twig callables. | ||
* | ||
* @author Jérôme Tamarelle <jerome@tamarelle.net> | ||
* | ||
* @internal | ||
*/ | ||
final class AttributeExtensionPass implements CompilerPassInterface | ||
{ | ||
private const TAG = 'twig.attribute_extension'; | ||
|
||
public static function autoconfigureFromAttribute(ChildDefinition $definition, AsTwigFilter|AsTwigFunction|AsTwigTest $attribute, \ReflectionMethod $reflector): void | ||
{ | ||
$definition->addTag(self::TAG); | ||
|
||
// The service must be tagged as a runtime to call non-static methods | ||
if (!$reflector->isStatic()) { | ||
$definition->addTag('twig.runtime'); | ||
} | ||
} | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach ($container->findTaggedServiceIds(self::TAG, true) as $id => $tags) { | ||
$container->register('.twig.extension.'.$id, AttributeExtension::class) | ||
->setArguments([$container->getDefinition($id)->getClass()]) | ||
->addTag('twig.extension'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||||||||||
|
||||||||||||
namespace Symfony\Bundle\TwigBundle\DependencyInjection; | ||||||||||||
|
||||||||||||
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\AttributeExtensionPass; | ||||||||||||
use Symfony\Component\AssetMapper\AssetMapper; | ||||||||||||
use Symfony\Component\Config\FileLocator; | ||||||||||||
use Symfony\Component\Config\Resource\FileExistenceResource; | ||||||||||||
|
@@ -25,6 +26,9 @@ | |||||||||||
use Symfony\Component\Translation\LocaleSwitcher; | ||||||||||||
use Symfony\Component\Translation\Translator; | ||||||||||||
use Symfony\Contracts\Service\ResetInterface; | ||||||||||||
use Twig\Attribute\AsTwigFilter; | ||||||||||||
use Twig\Attribute\AsTwigFunction; | ||||||||||||
use Twig\Attribute\AsTwigTest; | ||||||||||||
use Twig\Environment; | ||||||||||||
use Twig\Extension\ExtensionInterface; | ||||||||||||
use Twig\Extension\RuntimeExtensionInterface; | ||||||||||||
|
@@ -179,6 +183,10 @@ public function load(array $configs, ContainerBuilder $container): void | |||||||||||
$container->registerForAutoconfiguration(LoaderInterface::class)->addTag('twig.loader'); | ||||||||||||
$container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime'); | ||||||||||||
|
||||||||||||
$container->registerAttributeForAutoconfiguration(AsTwigFilter::class, AttributeExtensionPass::autoconfigureFromAttribute(...)); | ||||||||||||
$container->registerAttributeForAutoconfiguration(AsTwigFunction::class, AttributeExtensionPass::autoconfigureFromAttribute(...)); | ||||||||||||
$container->registerAttributeForAutoconfiguration(AsTwigTest::class, AttributeExtensionPass::autoconfigureFromAttribute(...)); | ||||||||||||
|
||||||||||||
Comment on lines
+186
to
+189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the attribute class is not declared (Older Twig version), this will be skipped. symfony/src/Symfony/Component/DependencyInjection/Compiler/AttributeAutoconfigurationPass.php Lines 58 to 62 in 6a029ec
|
||||||||||||
if (false === $config['cache']) { | ||||||||||||
$container->removeDefinition('twig.template_cache_warmer'); | ||||||||||||
} | ||||||||||||
|
120 changes: 120 additions & 0 deletions
120
src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Tests\Functional; | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\TwigBundle\Tests\TestCase; | ||
use Symfony\Bundle\TwigBundle\TwigBundle; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Twig\Attribute\AsTwigFilter; | ||
use Twig\Attribute\AsTwigFunction; | ||
use Twig\Attribute\AsTwigTest; | ||
use Twig\Environment; | ||
use Twig\Error\RuntimeError; | ||
use Twig\Extension\AttributeExtension; | ||
|
||
class AttributeExtensionTest extends TestCase | ||
{ | ||
public function testExtensionWithAttributes() | ||
{ | ||
if (!class_exists(AttributeExtension::class)) { | ||
self::markTestSkipped('Twig 3.21 is required.'); | ||
} | ||
|
||
$kernel = new class('test', true) extends Kernel | ||
{ | ||
public function registerBundles(): iterable | ||
{ | ||
return [new FrameworkBundle(), new TwigBundle()]; | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
$loader->load(static function (ContainerBuilder $container) { | ||
$container->register(StaticExtensionWithAttributes::class, StaticExtensionWithAttributes::class) | ||
->setAutoconfigured(true); | ||
$container->register(RuntimeExtensionWithAttributes::class, RuntimeExtensionWithAttributes::class) | ||
->setArguments(['prefix_']) | ||
->setAutoconfigured(true); | ||
|
||
$container->setAlias('twig_test', 'twig')->setPublic(true); | ||
}); | ||
} | ||
|
||
public function getProjectDir(): string | ||
{ | ||
return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension'; | ||
} | ||
}; | ||
|
||
$kernel->boot(); | ||
|
||
/** @var Environment $twig */ | ||
$twig = $kernel->getContainer()->get('twig_test'); | ||
|
||
self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(StaticExtensionWithAttributes::class)); | ||
self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(RuntimeExtensionWithAttributes::class)); | ||
self::assertInstanceOf(RuntimeExtensionWithAttributes::class, $twig->getRuntime(RuntimeExtensionWithAttributes::class)); | ||
|
||
self::expectException(RuntimeError::class); | ||
$twig->getRuntime(StaticExtensionWithAttributes::class); | ||
} | ||
|
||
/** | ||
* @before | ||
* @after | ||
*/ | ||
protected function deleteTempDir() | ||
{ | ||
if (file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension')) { | ||
(new Filesystem())->remove($dir); | ||
} | ||
} | ||
} | ||
|
||
class StaticExtensionWithAttributes | ||
{ | ||
#[AsTwigFilter('foo')] | ||
public static function fooFilter(string $value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
#[AsTwigFunction('foo')] | ||
public static function fooFunction(string $value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
#[AsTwigTest('foo')] | ||
public static function fooTest(bool $value): bool | ||
{ | ||
return $value; | ||
} | ||
} | ||
|
||
class RuntimeExtensionWithAttributes | ||
{ | ||
public function __construct(private bool $prefix) | ||
{ | ||
} | ||
|
||
#[AsTwigFilter('foo')] | ||
#[AsTwigFunction('foo')] | ||
public function prefix(string $value): string | ||
{ | ||
return $this->prefix.$value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.