-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows registration of filter/function/test with an attribute
- Loading branch information
Showing
10 changed files
with
744 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\Attribute; | ||
|
||
use Twig\DeprecatedCallableInfo; | ||
use Twig\TwigFilter; | ||
|
||
/** | ||
* Registers a method as template filter. | ||
* | ||
* If the first argument of the method has Twig\Environment type-hint, the filter will receive the current environment. | ||
* If the next argument of the method is named $context and has array type-hint, the filter will receive the current context. | ||
* Additional arguments of the method come from the filter call. | ||
* | ||
* #[AsTwigFilter('foo')] | ||
* function fooFilter(Environment $env, array $context, $string, $arg1 = null, ...) { ... } | ||
* | ||
* {{ 'string'|foo(arg1) }} | ||
* | ||
* @see TwigFilter | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigFilter | ||
{ | ||
/** | ||
* @param non-empty-string $name The name of the filter in Twig. | ||
* @param bool|null $needsCharset Whether the filter needs the charset passed as the first argument. | ||
* @param bool|null $needsEnvironment Whether the filter needs the environment passed as the first argument, or after the charset. | ||
* @param bool|null $needsContext Whether the filter needs the context array passed as the first argument, or after the charset and the environment. | ||
* @param string[]|null $isSafe List of formats in which you want the raw output to be printed unescaped. | ||
* @param string|array|null $isSafeCallback Function called at compilation time to determine if the filter is safe. | ||
* @param string|null $preEscape Some filters may need to work on input that is already escaped or safe | ||
* @param string[]|null $preservesSafety Preserves the safety of the value that the filter is applied to. | ||
* @param DeprecatedCallableInfo|null $deprecationInfo Information about the deprecation | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public ?bool $needsCharset = null, | ||
public ?bool $needsEnvironment = null, | ||
public ?bool $needsContext = null, | ||
public ?array $isSafe = null, | ||
public string|array|null $isSafeCallback = null, | ||
public ?string $preEscape = null, | ||
public ?array $preservesSafety = null, | ||
public ?DeprecatedCallableInfo $deprecationInfo = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\Attribute; | ||
|
||
use Twig\DeprecatedCallableInfo; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* Registers a method as template function. | ||
* | ||
* If the first argument of the method has Twig\Environment type-hint, the function will receive the current environment. | ||
* If the next argument of the method is named $context and has array type-hint, the function will receive the current context. | ||
* Additional arguments of the method come from the function call. | ||
* | ||
* #[AsTwigFunction('foo')] | ||
* function fooFunction(Environment $env, array $context, string $string, $arg1 = null, ...) { ... } | ||
* | ||
* {{ foo('string', arg1) }} | ||
* | ||
* @see TwigFunction | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigFunction | ||
{ | ||
/** | ||
* @param non-empty-string $name The name of the function in Twig. | ||
* @param bool|null $needsCharset Whether the function needs the charset passed as the first argument. | ||
* @param bool|null $needsEnvironment Whether the function needs the environment passed as the first argument, or after the charset. | ||
* @param bool|null $needsContext Whether the function needs the context array passed as the first argument, or after the charset and the environment. | ||
* @param string[]|null $isSafe List of formats in which you want the raw output to be printed unescaped. | ||
* @param string|array|null $isSafeCallback Function called at compilation time to determine if the function is safe. | ||
* @param DeprecatedCallableInfo|null $deprecationInfo Information about the deprecation | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public ?bool $needsCharset = null, | ||
public ?bool $needsEnvironment = null, | ||
public ?bool $needsContext = null, | ||
public ?array $isSafe = null, | ||
public string|array|null $isSafeCallback = null, | ||
public ?DeprecatedCallableInfo $deprecationInfo = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\Attribute; | ||
|
||
use Twig\DeprecatedCallableInfo; | ||
use Twig\TwigTest; | ||
|
||
/** | ||
* Registers a method as template test. | ||
* | ||
* The first argument is the value to test and the other arguments are the | ||
* arguments passed to the test in the template. | ||
* | ||
* #[AsTwigTest('foo')] | ||
* public function fooTest($value, $arg1 = null) { ... } | ||
* | ||
* {% if value is foo(arg1) %} | ||
* | ||
* @see TwigTest | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigTest | ||
{ | ||
/** | ||
* @param non-empty-string $name The name of the test in Twig. | ||
* @param bool|null $needsCharset Whether the test needs the charset passed as the first argument. | ||
* @param bool|null $needsEnvironment Whether the test needs the environment passed as the first argument, or after the charset. | ||
* @param bool|null $needsContext Whether the test needs the context array passed as the first argument, or after the charset and the environment. | ||
* @param DeprecatedCallableInfo|null $deprecationInfo Information about the deprecation | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public ?bool $needsCharset = null, | ||
public ?bool $needsEnvironment = null, | ||
public ?bool $needsContext = null, | ||
public ?DeprecatedCallableInfo $deprecationInfo = null, | ||
) { | ||
} | ||
} |
Oops, something went wrong.