From 5dbc93d5e2a6eec55a6c334a8a5c79db29adcd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Fri, 26 Jul 2024 21:16:04 +0200 Subject: [PATCH] Load constants from AsTwigConstant attribute --- composer.json | 1 + src/Accessor/ConstantResolver.php | 4 ++-- src/Attribute/AsTwigConstant.php | 13 +++++++++++++ .../TwigConstantAccessorExtension.php | 8 +++++++- tests/Fixtures/AttributeConstant.php | 12 ++++++++++++ tests/Fixtures/ConstantAccessFunctionalTest.php | 1 + tests/Fixtures/Resources/views/index.html.twig | 4 ++++ tests/Fixtures/TemplateKernel.php | 2 +- 8 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/Attribute/AsTwigConstant.php create mode 100644 tests/Fixtures/AttributeConstant.php 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..786a9f5 --- /dev/null +++ b/src/Attribute/AsTwigConstant.php @@ -0,0 +1,13 @@ +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); } }