-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Create attributes AsTwigFilter
, AsTwigFunction
and AsTwigTest
to ease extension development
#3916
Open
GromNaN
wants to merge
20
commits into
twigphp:3.x
Choose a base branch
from
GromNaN:attribute
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
53aeae5
Allows registration of filter/function/test with an attribute
GromNaN 4e7ac90
Undocument detected options
GromNaN 9ed0d44
Test repeated attributes
GromNaN 071ee08
Use attributes in standalone classes and add flatten promote options …
GromNaN a91c3d3
Accept a class name to leverage runtime lazy-loading
GromNaN 81cbbff
Fix CS
GromNaN 9685949
Track changes in runtime extensions
GromNaN 270787e
Fix types
GromNaN 7b2cf4e
Add AsTwigExtension attribute and make function name required
GromNaN 064760a
Update phpdoc
GromNaN 43a8950
Move attributes to Twig\Attribute namespace
GromNaN 8f095e0
Add pattern filter test case
GromNaN 11b570d
Remove name unicity constraint, add phpdoc, refactor time computation
GromNaN 49c0f80
Add docs
GromNaN 1808c2c
Mark attributes as final
GromNaN 2bf1fa1
Rename ModificationAwareInterface
GromNaN aa4f4cb
Make AsTwigExtension extension first party instead of relying on the …
GromNaN f634240
Use array_values for list of filters/functions/tests
GromNaN 1906c8f
Switch to the new DeprecatedCallableInfo object
GromNaN d16812e
Improve doc and tests
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ | ||
<?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; | ||
|
||
/** | ||
* Identifies a class that uses PHP attributes to define filters, functions, or tests. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsTwigExtension | ||
{ | ||
} |
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,55 @@ | ||
<?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\Node\Node; | ||
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 string[]|null $isSafe List of formats in which you want the raw output to be printed unescaped. | ||
* @param null|callable(Node):bool $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, for | ||
* example when adding (safe) HTML tags to originally unsafe output. In such a | ||
* case, set preEscape to an escape format to escape the input data before it | ||
* is run through the filter. | ||
* @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 ?array $isSafe = null, | ||
public mixed $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,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\Node\Node; | ||
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 string[]|null $isSafe List of formats in which you want the raw output to be printed unescaped. | ||
* @param null|callable(Node):bool $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 ?array $isSafe = null, | ||
public mixed $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,42 @@ | ||
<?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 DeprecatedCallableInfo|null $deprecationInfo Information about the deprecation | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change.