Skip to content

Commit

Permalink
Load constants from AsTwigConstant attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool committed Jul 26, 2024
1 parent 1860ce3 commit 5dbc93d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Accessor/ConstantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class ConstantResolver
{
/**
* @param list<ConstantConfiguration> $classes
* @param iterable<ConstantConfiguration> $classes
*
* @return array<string, mixed>
*/
public function fromClassList(array $classes): array
public function fromClassList(iterable $classes): array
{
$constants = [];

Expand Down
13 changes: 13 additions & 0 deletions src/Attribute/AsTwigConstant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace JDecool\Bundle\TwigConstantAccessorBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsTwigConstant
{
public function __construct(
public readonly ?string $alias = null,
public readonly ?string $matches = null,
) {
}
}
8 changes: 7 additions & 1 deletion src/DependencyInjection/TwigConstantAccessorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace JDecool\Bundle\TwigConstantAccessorBundle\DependencyInjection;

use JDecool\Bundle\TwigConstantAccessorBundle\Accessor\ConstantResolver;
use JDecool\Bundle\TwigConstantAccessorBundle\Attribute\AsTwigConstant;
use JDecool\Bundle\TwigConstantAccessorBundle\Twig\ConstantExtension;
use Kcs\ClassFinder\Finder\ComposerFinder;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -23,8 +25,12 @@ public function load(array $configs, ContainerBuilder $container): void

private function resolveConstants(array $config): array
{
$finder = new ComposerFinder();
$resolver = new ConstantResolver();

return $resolver->fromClassList($config['classes']);
return array_merge(
$resolver->fromClassList($config['classes']),
$resolver->fromClassList($finder->withAttribute(AsTwigConstant::class)),

Check failure on line 33 in src/DependencyInjection/TwigConstantAccessorExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $classes of method JDecool\Bundle\TwigConstantAccessorBundle\Accessor\ConstantResolver::fromClassList() expects iterable<array{class: class-string, alias?: string|null, matches?: string|null}>, Kcs\ClassFinder\Finder\ComposerFinder given.
);
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/AttributeConstant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Fixtures;

use JDecool\Bundle\TwigConstantAccessorBundle\Attribute\AsTwigConstant;

#[AsTwigConstant]
class AttributeConstant
{
public const FROM_ATTRIBUTE_1 = 'from_attribute_1';
public const FROM_ATTRIBUTE_2 = 'from_attribute_2';
}
1 change: 1 addition & 0 deletions tests/Fixtures/ConstantAccessFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testConstantsAreAccessibleInView(): void
$content = $this->render('index.html.twig');

$this->assertStringContainsString('my_constant.my_constant_value', $content);
$this->assertStringContainsString('from_attribute_1', $content);
}

private function render(string $template): string
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/Resources/views/index.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{# From `class` configuration #}
{{ MyConstant.MY_CONSTANT_VALUE }}

{# From Fixtures\AttributeConstant class registered with AsTwigConstant attribute #}
{{ AttributeConstant.FROM_ATTRIBUTE_1 }}
2 changes: 1 addition & 1 deletion tests/Fixtures/TemplateKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5dbc93d

Please sign in to comment.