Skip to content
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

Load constants from AsTwigConstant attribute #39

Draft
wants to merge 1 commit into
base: rewrite
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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);
}
}
Loading