8000 Fix expression language in the container when using the "container" variable by fabpot · Pull Request #12030 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix expression language in the container when using the "container" variable #12030

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[ExpressionLanguage] allowed to compile a var name under a different …
…name than the one used in the expression
  • Loading branch information
fabpot committed Sep 25, 2014
commit feabe96d8fbe0b77ccc88938f066adebb882deac
18 changes: 17 additions & 1 deletion src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function __construct(array $functions)
/**
* Converts a token stream to a node tree.
*
* The valid names is an array where the values
* are the names that the user can use in an expression.
*
* If the variable name in the compiled PHP code must be
* different, define it as the key.
*
* For instance, ['this' => 'container'] means that the
* variable 'container' can be used in the expression
* but the compiled code will use 'this'.
*
* @param TokenStream $stream A token stream instance
* @param array $names An array of valid names
*
< CDF4 svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-fold-down"> Expand Down Expand Up @@ -194,7 +204,13 @@ public function parsePrimaryExpression()
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor);
}

$node = new Node\NameNode($token->value);
// is the name used in the compiled code different
// from the name used in the expression?
if (is_int($name = array_search($token->value, $this->names))) {
$name = $token->value;
}

$node = new Node\NameNode($name);
}
}
break;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public function getParseData()
'foo.bar().foo().baz[3]',
array('foo'),
),

array(
new Node\NameNode('foo'),
'bar',
array('foo' => 'bar'),
),
);
}

Expand Down
0