diff --git a/README.md b/README.md index b0545db..83cd661 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,18 @@ services: - { name: twig.constant_accessor, matches: '/^RULE_/' } # matches accept an regexp compatible with the preg_match function ``` +Or you can use the `#[AsTwigConstantAccessor]` attribute on your class : + +```php +use JDecool\Bundle\TwigConstantAccessorBundle\Annotation\AsTwigConstantAccessor; + +#[AsTwigConstantAccessor] +class MyClass +{ + // ... +} +``` + After you can access your class constant in your templates : ```twig diff --git a/composer.json b/composer.json index b1ef3fa..fddfcab 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "symfony/dependency-injection": "^5.4|^6.0|^7.0", "symfony/framework-bundle": "^5.4|^6.0|^7.0", "symfony/twig-bundle": "^5.4|^6.0|^7.0", diff --git a/src/Attribute/AsTwigConstantAccessor.php b/src/Attribute/AsTwigConstantAccessor.php new file mode 100644 index 0000000..13c9bb2 --- /dev/null +++ b/src/Attribute/AsTwigConstantAccessor.php @@ -0,0 +1,13 @@ +registerAttributeForAutoconfiguration( + AsTwigConstantAccessor::class, + static function(ChildDefinition $definition, AsTwigConstantAccessor $attribute, \Reflector $reflector): void { + if (!$reflector instanceof \ReflectionClass) { + throw new \LogicException('AsTwigConstantAccessor attribute can only be used on classes.'); + } + + $definition->addTag( + 'twig.constant_accessor', + [ + 'class' => $definition->getClass() ?? $reflector->getName(), + 'alias' => $attribute->alias, + 'matches' => $attribute->matches, + ], + ); + }, + ); + $container->addCompilerPass(new TwigConstantExtensionPass()); $container->addCompilerPass(new TwigConstantOptimizerPass()); } diff --git a/tests/Fixtures/Foo/ClassConstantByAttribute.php b/tests/Fixtures/Foo/ClassConstantByAttribute.php new file mode 100644 index 0000000..3d633bf --- /dev/null +++ b/tests/Fixtures/Foo/ClassConstantByAttribute.php @@ -0,0 +1,12 @@ +assertStringNotContainsString('doe', $content); } + /** + * @requires PHP >= 8.1 + */ public function testEnumAccess(): void { - if (!class_exists(\UnitEnum::class)) { - $this->markTestSkipped('Enum class is not available'); - } - $content = $this->render('enum.html.twig'); $this->assertStringNotContainsString('Foo', $content); @@ -104,6 +103,22 @@ public function testEnumAccess(): void $this->assertStringContainsString('acme', $content); } + public function testAccessByAttribute(): void + { + $content = $this->render('class_constants_by_attribute.html.twig'); + + $this->assertStringContainsString('foo', $content); + $this->assertStringContainsString('bar', $content); + } + + public function testAccessByAttributeWithArgs(): void + { + $content = $this->render('class_constants_by_attribute_with_args.html.twig'); + + $this->assertStringContainsString('selected_foo', $content); + $this->assertStringContainsString('selected_bar', $content); + } + private function render(string $template): string { /** @var Environment $twig */