Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15ce692

Browse files
committed
[ExpressionLanguage] Add Parser->parseWithoutContext
1 parent cd53a82 commit 15ce692

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Symfony/Component/ExpressionLanguage/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `enum` expression function
88
* Deprecate loose comparisons when using the "in" operator; normalize the array parameter
99
so it only has the expected types or implement loose matching in your own expression function
10+
* Add `parseWithoutContext` to `Parser`, use this function to get the expression's AST without validating names
1011

1112
6.2
1213
---

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Parser
3232
private array $functions;
3333
private ?array $names;
3434
private bool $lint = false;
35+
private bool $validateIdentifiers = true;
3536

3637
public function __construct(array $functions)
3738
{
@@ -94,10 +95,25 @@ public function __construct(array $functions)
9495
public function parse(TokenStream $stream, array $names = []): Node\Node
9596
{
9697
$this->lint = false;
98+
$this->validateIdentifiers = true;
9799

98100
return $this->doParse($stream, $names);
99101
}
100102

103+
/**
104+
* Converts a token stream to a node tree.
105+
*
106+
* This function does not validate identifiers.
107+
* Prefer using `parse` if you can.
108+
*/
109+
public function parseWithoutContext(TokenStream $stream): Node\Node
110+
{
111+
$this->lint = false;
112+
$this->validateIdentifiers = false;
113+
114+
return $this->doParse($stream, null);
115+
}
116+
101117
/**
102118
* Validates the syntax of an expression.
103119
*
@@ -109,6 +125,7 @@ public function parse(TokenStream $stream, array $names = []): Node\Node
109125
public function lint(TokenStream $stream, ?array $names = []): void
110126
{
111127
$this->lint = true;
128+
$this->validateIdentifiers = true;
112129
$this->doParse($stream, $names);
113130
}
114131

@@ -238,13 +255,13 @@ public function parsePrimaryExpression()
238255

239256
default:
240257
if ('(' === $this->stream->current->value) {
241-
if (false === isset($this->functions[$token->value])) {
258+
if ($this->validateIdentifiers && false === isset($this->functions[$token->value])) {
242259
throw new SyntaxError(sprintf('The function "%s" does not exist.', $token->value), $token->cursor, $this->stream->getExpression(), $token->value, array_keys($this->functions));
243260
}
244261

245262
$node = new Node\FunctionNode($token->value, $this->parseArguments());
246263
} else {
247-
if (!$this->lint || \is_array($this->names)) {
264+
if ($this->validateIdentifiers && (!$this->lint || \is_array($this->names))) {
248265
if (!\in_array($token->value, $this->names, true)) {
249266
throw new SyntaxError(sprintf('Variable "%s" is not valid.', $token->value), $token->cursor, $this->stream->getExpression(), $token->value, $this->names);
250267
}

src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\ExpressionLanguage\Lexer;
1616
use Symfony\Component\ExpressionLanguage\Node;
17+
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
18+
use Symfony\Component\ExpressionLanguage\Node\FunctionNode;
19+
use Symfony\Component\ExpressionLanguage\Node\NameNode;
20+
use Symfony\Component\ExpressionLanguage\Node\Node as NodeNode;
1721
use Symfony\Component\ExpressionLanguage\Parser;
1822
use Symfony\Component\ExpressionLanguage\SyntaxError;
1923

@@ -378,4 +382,14 @@ public static function getLintData(): array
378382
],
379383
];
380384
}
385+
386+
public function testParseWithoutContext()
387+
{
388+
$expression = 'is_granted(\'ROLE_EDIT\', subject)';
389+
$expected = new FunctionNode('is_granted', new NodeNode([new ConstantNode('ROLE_EDIT'), new NameNode('subject')]));
390+
391+
$lexer = new Lexer();
392+
$parser = new Parser([]);
393+
$this->assertEquals($expected, $parser->parseWithoutContext($lexer->tokenize($expression)));
394+
}
381395
}

0 commit comments

Comments
 (0)