diff --git a/composer.json b/composer.json index 5c5fad3..7235150 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "homepage": "http://github.com/jdecool/TwigConstantAccessorBundle", "require": { "php": "^8.1", + "kcs/class-finder": "^0.5.0", "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0", "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0", "symfony/twig-bundle": "^5.4 || ^6.4 || ^7.0", diff --git a/src/Accessor/ConstantResolver.php b/src/Accessor/ConstantResolver.php index f3894dd..205f328 100644 --- a/src/Accessor/ConstantResolver.php +++ b/src/Accessor/ConstantResolver.php @@ -8,11 +8,11 @@ class ConstantResolver { /** - * @param list $classes + * @param iterable $classes * * @return array */ - public function fromClassList(array $classes): array + public function fromClassList(iterable $classes): array { $constants = []; diff --git a/src/Attribute/AsTwigConstant.php b/src/Attribute/AsTwigConstant.php new file mode 100644 index 0000000..26ac086 --- /dev/null +++ b/src/Attribute/AsTwigConstant.php @@ -0,0 +1,15 @@ +fromClassList($config['classes']); + return array_merge( + $resolver->fromClassList($config['classes']), + $resolver->fromClassList($finder->withAttribute(AsTwigConstant::class)), + ); } } diff --git a/tests/Fixtures/AttributeConstant.php b/tests/Fixtures/AttributeConstant.php new file mode 100644 index 0000000..2021598 --- /dev/null +++ b/tests/Fixtures/AttributeConstant.php @@ -0,0 +1,12 @@ +render('index.html.twig'); $this->assertStringContainsString('my_constant.my_constant_value', $content); + $this->assertStringContainsString('from_attribute_1', $content); } private function render(string $template): string diff --git a/tests/Fixtures/Resources/views/index.html.twig b/tests/Fixtures/Resources/views/index.html.twig index fd9cf31..6a98963 100644 --- a/tests/Fixtures/Resources/views/index.html.twig +++ b/tests/Fixtures/Resources/views/index.html.twig @@ -1 +1,5 @@ +{# From `class` configuration #} {{ MyConstant.MY_CONSTANT_VALUE }} + +{# From Fixtures\AttributeConstant class registered with AsTwigConstant attribute #} +{{ AttributeConstant.FROM_ATTRIBUTE_1 }} diff --git a/tests/Fixtures/TemplateKernel.php b/tests/Fixtures/TemplateKernel.php index 3a9a1ec..c1e8c5a 100644 --- a/tests/Fixtures/TemplateKernel.php +++ b/tests/Fixtures/TemplateKernel.php @@ -61,6 +61,6 @@ public function getLogDir(): string public function getBasePath(): string { - return sprintf('%s/%s/TemplateKernel', sys_get_temp_dir(), Kernel::VERSION); + return sprintf('%s/TwigConstantAccessor/%s/TemplateKernel', sys_get_temp_dir(), Kernel::VERSION); } }