Skip to content

Commit

Permalink
Add AsTwigExtension attribute and make function name required
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 30, 2023
1 parent e64a54b commit c7f20ab
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
13 changes: 13 additions & 0 deletions src/Extension/Attribute/AsTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Twig\Extension\Attribute;

use Twig\TwigFilter;

/**
* Identifies a class that uses PHP attributes to define filters, functions, or tests.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class AsTwigExtension
{
}
6 changes: 3 additions & 3 deletions src/Extension/Attribute/AsTwigFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class AsTwigFilter
{
public function __construct(
/**
* The name of the filter in Twig (defaults to the method name).
* The name of the filter in Twig
*
* @var non-empty-string|null $name
* @var non-empty-string $name
*/
public ?string $name = null,
public string $name,
public ?array $isSafe = null,
public ?string $isSafeCallback = null,
public ?string $preEscape = null,
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/Attribute/AsTwigFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class AsTwigFunction
{
public function __construct(
/**
* The name of the function in Twig (defaults to the method name).
* The name of the function in Twig
*
* @var non-empty-string|null $name
* @var non-empty-string $name
*/
public ?string $name = null,
public string $name,
public ?array $isSafe = null,
public ?string $isSafeCallback = null,
public bool|string $deprecated = false,
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/Attribute/AsTwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class AsTwigTest
{
public function __construct(
/**
* The name of the filter in Twig (defaults to the method name).
* The name of the filter in Twig.
*
* @var non-empty-string|null $name
* @var non-empty-string $name
*/
public ?string $name = null,
public string $name,
public bool|string $deprecated = false,
public ?string $alternative = null,
) {
Expand Down
31 changes: 14 additions & 17 deletions tests/Extension/AttributeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ public function testFilter(string $name, string $method, array $options)

public static function provideFilters()
{
yield 'basic' => ['fooFilter', 'fooFilter', []];
yield 'with name' => ['foo', 'fooFilter', []];
yield 'with env' => ['withEnvFilter', 'withEnvFilter', ['needs_environment' => true]];
yield 'with context' => ['withContextFilter', 'withContextFilter', ['needs_context' => true]];
yield 'with env and context' => ['withEnvAndContextFilter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]];
yield 'no argument' => ['noArgFilter', 'noArgFilter', []];
yield 'variadic' => ['variadicFilter', 'variadicFilter', ['is_variadic' => true]];
yield 'deprecated' => ['deprecatedFilter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']];
yield 'with env' => ['with_env_filter', 'withEnvFilter', ['needs_environment' => true]];
yield 'with context' => ['with_context_filter', 'withContextFilter', ['needs_context' => true]];
yield 'with env and context' => ['with_env_and_context_filter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]];
yield 'no argument' => ['no_arg_filter', 'noArgFilter', []];
yield 'variadic' => ['variadic_filter', 'variadicFilter', ['is_variadic' => true]];
yield 'deprecated' => ['deprecated_filter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']];
}

/**
Expand All @@ -64,14 +63,13 @@ public function testFunction(string $name, string $method, array $options)

public static function provideFunctions()
{
yield 'basic' => ['fooFunction', 'fooFunction', []];
yield 'with name' => ['foo', 'fooFunction', []];
yield 'with env' => ['withEnvFunction', 'withEnvFunction', ['needs_environment' => true]];
yield 'with context' => ['withContextFunction', 'withContextFunction', ['needs_context' => true]];
yield 'with env and context' => ['withEnvAndContextFunction', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]];
yield 'no argument' => ['noArgFunction', 'noArgFunction', []];
yield 'variadic' => ['variadicFunction', 'variadicFunction', ['is_variadic' => true]];
yield 'deprecated' => ['deprecatedFunction', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']];
yield 'with env' => ['with_env_function', 'withEnvFunction', ['needs_environment' => true]];
yield 'with context' => ['with_context_function', 'withContextFunction', ['needs_context' => true]];
yield 'with env and context' => ['with_env_and_context_function', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]];
yield 'no argument' => ['no_arg_function', 'noArgFunction', []];
yield 'variadic' => ['variadic_function', 'variadicFunction', ['is_variadic' => true]];
yield 'deprecated' => ['deprecated_function', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']];
}

/**
Expand All @@ -94,10 +92,9 @@ public function testTest(string $name, string $method, array $options)

public static function provideTests()
{
yield 'basic' => ['fooTest', 'fooTest', []];
yield 'with name' => ['foo', 'fooTest', []];
yield 'variadic' => ['variadicTest', 'variadicTest', ['is_variadic' => true]];
yield 'deprecated' => ['deprecatedTest', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']];
yield 'variadic' => ['variadic_test', 'variadicTest', ['is_variadic' => true]];
yield 'deprecated' => ['deprecated_test', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']];
}

public function testRuntimeExtension()
Expand Down
36 changes: 17 additions & 19 deletions tests/Extension/Fixtures/ExtensionWithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,95 @@
namespace Twig\Tests\Extension\Fixtures;

use Twig\Environment;
use Twig\Extension\Attribute\AsTwigExtension;
use Twig\Extension\Attribute\AsTwigFilter;
use Twig\Extension\Attribute\AsTwigFunction;
use Twig\Extension\Attribute\AsTwigTest;
use Twig\Extension\RuntimeExtensionInterface;

class ExtensionWithAttributes implements RuntimeExtensionInterface
#[AsTwigExtension]
class ExtensionWithAttributes
{
#[AsTwigFilter]
#[AsTwigFilter(name: 'foo')]
public function fooFilter(string $string)
{
}

#[AsTwigFilter]
#[AsTwigFilter('with_context_filter')]
public function withContextFilter(array $context, string $string)
{
}

#[AsTwigFilter]
#[AsTwigFilter('with_env_filter')]
public function withEnvFilter(Environment $env, string $string)
{
}

#[AsTwigFilter]
#[AsTwigFilter('with_env_and_context_filter')]
public function withEnvAndContextFilter(Environment $env, array $context, string $string)
{
}

#[AsTwigFilter]
#[AsTwigFilter('no_arg_filter')]
public function noArgFilter()
{
}

#[AsTwigFilter]
#[AsTwigFilter('variadic_filter')]
public function variadicFilter(string ...$strings)
{
}

#[AsTwigFilter(deprecated: true, alternative: 'bar')]
#[AsTwigFilter('deprecated_filter', deprecated: true, alternative: 'bar')]
public function deprecatedFilter(string $string)
{
}

#[AsTwigFunction]
#[AsTwigFunction(name: 'foo')]
public function fooFunction(string $string)
{
}

#[AsTwigFunction]
#[AsTwigFunction('with_context_function')]
public function withContextFunction(array $context, string $string)
{
}

#[AsTwigFunction]
#[AsTwigFunction('with_env_function')]
public function withEnvFunction(Environment $env, string $string)
{
}

#[AsTwigFunction]
#[AsTwigFunction('with_env_and_context_function')]
public function withEnvAndContextFunction(Environment $env, array $context, string $string)
{
}

#[AsTwigFunction]
#[AsTwigFunction('no_arg_function')]
public function noArgFunction()
{
}

#[AsTwigFunction]
#[AsTwigFunction('variadic_function')]
public function variadicFunction(string ...$strings)
{
}

#[AsTwigFunction(deprecated: true, alternative: 'bar')]
#[AsTwigFunction('deprecated_function', deprecated: true, alternative: 'bar')]
public function deprecatedFunction(string $string)
{
}

#[AsTwigTest]
#[AsTwigTest(name: 'foo')]
public function fooTest(string $string)
{
}

#[AsTwigTest]
#[AsTwigTest('variadic_test')]
public function variadicTest(string ...$strings)
{
}

#[AsTwigTest(deprecated: true, alternative: 'bar')]
#[AsTwigTest('deprecated_test', deprecated: true, alternative: 'bar')]
public function deprecatedTest(string $string)
{
}
Expand Down

0 comments on commit c7f20ab

Please sign in to comment.