Support two words test guard

This commit is contained in:
Vincent Langlet
2025-09-05 11:15:29 +02:00
committed by Fabien Potencier
parent 8832f6327c
commit 30977bdea9
3 changed files with 26 additions and 3 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
# 3.21.2 (2025-XX-XX)
# 3.22.0 (2025-XX-XX)
* n/a
* Add support for two words test in guard tag
# 3.21.1 (2025-05-03)
+7 -1
View File
@@ -32,9 +32,15 @@ final class GuardTokenParser extends AbstractTokenParser
$method = 'get'.$typeToken->getValue();
$nameToken = $stream->expect(Token::NAME_TYPE);
$name = $nameToken->getValue();
if ('test' === $typeToken->getValue() && $stream->test(Token::NAME_TYPE)) {
// try 2-words tests
$name .= ' '.$stream->getCurrent()->getValue();
$stream->next();
}
try {
$exists = null !== $this->parser->getEnvironment()->$method($nameToken->getValue());
$exists = null !== $this->parser->getEnvironment()->$method($name);
} catch (SyntaxError) {
$exists = false;
}
+17
View File
@@ -13,9 +13,26 @@
{% else %}
NEVER
{% endguard %}
{% guard test foobar %}
NEVER
{{ 'a'|foobar }}
{% else -%}
The foobar test doesn't exist
{% endguard %}
{% guard test divisible by -%}
The divisible by function does exist
{% else %}
NEVER
{% endguard %}
--DATA--
return []
--EXPECT--
The foobar filter doesn't exist
The constant function does exist
The foobar test doesn't exist
The divisible by function does exist