-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: 3.x
Are you sure you want to change the base?
Changes from all commits
c8c2f57
4d9ae3f
22aeb4f
3f84792
d441d0a
153e028
7b88138
76b2830
200c0ba
29f3b3e
5dc5e40
365d102
2fd1210
4f9e0f8
43bec2f
36476c3
57cb0f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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\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, ...) { ... } | ||
* | ||
* @see TwigFilter | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigFilter | ||
{ | ||
public function __construct( | ||
/** | ||
* The name of the filter in Twig. | ||
* | ||
* @var non-empty-string $name | ||
*/ | ||
public string $name, | ||
|
||
/** | ||
* List of formats in which you want the raw output to be printed unescaped. | ||
* | ||
* @var list<string>|null $isSafe | ||
*/ | ||
public ?array $isSafe = null, | ||
|
||
/** | ||
* Function called at compilation time to determine if the filter is safe. | ||
* | ||
* @var callable(Node):bool $isSafeCallback | ||
*/ | ||
public ?string $isSafeCallback = null, | ||
|
||
/** | ||
* 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. | ||
*/ | ||
public ?string $preEscape = null, | ||
|
||
/** | ||
* Preserves the safety of the value that the filter is applied to. | ||
*/ | ||
public ?array $preservesSafety = null, | ||
|
||
/** | ||
* Set to true if the filter is deprecated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the string type about then ? |
||
*/ | ||
public bool|string $deprecated = false, | ||
|
||
/** | ||
* The alternative filter name to suggest when the deprecated filter is called. | ||
*/ | ||
public ?string $alternative = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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\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, $arg1 = null, ...) { ... } | ||
* | ||
* @see TwigFunction | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigFunction | ||
{ | ||
public function __construct( | ||
/** | ||
* The name of the function in Twig. | ||
* | ||
* @var non-empty-string $name | ||
*/ | ||
public string $name, | ||
|
||
/** | ||
* List of formats in which you want the raw output to be printed unescaped. | ||
* | ||
* @var list<string>|null $isSafe | ||
*/ | ||
public ?array $isSafe = null, | ||
|
||
/** | ||
* Function called at compilation time to determine if the function is safe. | ||
* | ||
* @var callable(Node):bool $isSafeCallback | ||
*/ | ||
public ?string $isSafeCallback = null, | ||
|
||
/** | ||
* Set to true if the function is deprecated. | ||
*/ | ||
public bool|string $deprecated = false, | ||
|
||
/** | ||
* The alternative function name to suggest when the deprecated function is called. | ||
*/ | ||
public ?string $alternative = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\TwigTest; | ||
|
||
/** | ||
* Registers a method as template test. | ||
* | ||
* If the first argument of the method has Twig\Environment type-hint, the test will receive the current environment. | ||
* If the next argument of the method is named $context and has array type-hint, the test will receive the current context. | ||
* The last argument of the method is the value to be tested, if any. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is wrong. It is not the last argument. It is the following argument. A test can have as many arguments as you want after the value being tested, which become arguments in the Twig signature. |
||
* | ||
* #[AsTwigTest('foo')] | ||
* public function fooTest(Environment $env, array $context, $value, $arg1 = null) { ... } | ||
* | ||
* @see TwigTest | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class AsTwigTest | ||
{ | ||
public function __construct( | ||
/** | ||
* The name of the filter in Twig. | ||
* | ||
* @var non-empty-string $name | ||
*/ | ||
public string $name, | ||
|
||
/** | ||
* Set to true if the function is deprecated. | ||
*/ | ||
public bool|string $deprecated = false, | ||
|
||
/** | ||
* The alternative function name to suggest when the deprecated function is called. | ||
*/ | ||
public ?string $alternative = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,14 +616,17 @@ public function getRuntime(string $class) | |
throw new RuntimeError(sprintf('Unable to load the "%s" runtime.', $class)); | ||
} | ||
|
||
public function addExtension(ExtensionInterface $extension) | ||
/** | ||
* @param ExtensionInterface|object|class-string $extension | ||
*/ | ||
public function addExtension(object|string $extension) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. |
||
{ | ||
$this->extensionSet->addExtension($extension); | ||
$this->updateOptionsHash(); | ||
} | ||
|
||
/** | ||
* @param ExtensionInterface[] $extensions An array of extensions | ||
* @param list<ExtensionInterface|object|class-string> $extensions An array of extensions | ||
*/ | ||
public function setExtensions(array $extensions) | ||
{ | ||
|
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 misses the doc for the type of array values