8000 [TwigBridge] removed Twig null nodes (deprecated as of Twig 1.25) by fabpot · Pull Request #19983 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] removed Twig null nodes (deprecated as of Twig 1.25) #19983

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

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions src/Symfony/Bridge/Twig/Node/DumpNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class DumpNode extends \Twig_Node

public function __construct($varPrefix, \Twig_Node $values = null, $lineno, $tag = null)
{
parent::__construct(array('values' => $values), array(), $lineno, $tag);
$nodes = array();
if (null !== $values) {
$nodes['values'] = $values;
}

parent::__construct($nodes, array(), $lineno, $tag);
$this->varPrefix = $varPrefix;
}

Expand All @@ -33,9 +38,7 @@ public function compile(\Twig_Compiler $compiler)
->write("if (\$this->env->isDebug()) {\n")
->inden 10000 t();

$values = $this->getNode('values');

if (null === $values) {
if (!$this->hasNode('values')) {
// remove embedded templates (macros) from the context
$compiler
->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
Expand All @@ -50,7 +53,7 @@ public function compile(\Twig_Compiler $compiler)
->write("}\n")
->addDebugInfo($this)
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
} elseif (1 === $values->count()) {
} elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\VarDumper\VarDumper::dump(')
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Node/StopwatchNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class StopwatchNode extends \Twig_Node
{
public function __construct(\Twig_Node $name, $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
public function __construct(\Twig_Node $name, \Twig_Node $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
{
parent::__construct(array('body' => $body, 'name' => $name, 'var' => $var), array(), $lineno, $tag);
}
Expand Down
29 changes: 21 additions & 8 deletions src/Symfony/Bridge/Twig/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ class TransNode extends \Twig_Node
{
public function __construct(\Twig_Node $body, \Twig_Node $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
{
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
$nodes = array('body' => $body);
if (null !== $domain) {
$nodes['domain'] = $domain;
}
if (null !== $count) {
$nodes['count'] = $count;
}
if (null !== $vars) {
$nodes['vars'] = $vars;
}
if (null !== $locale) {
$nodes['locale'] = $locale;
}

parent::__construct($nodes, array(), $lineno, $tag);
}

/**
8000 Expand All @@ -30,15 +44,14 @@ public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);

$vars = $this->getNode('vars');
$defaults = new \Twig_Node_Expression_Array(array(), -1);
if ($vars instanceof \Twig_Node_Expression_Array) {
if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof \Twig_Node_Expression_Array) {
$defaults = $this->getNode('vars');
$vars = null;
}
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);

$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
$method = !$this->hasNode('count') ? 'trans' : 'transChoice';

$compiler
->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
Expand All @@ -47,7 +60,7 @@ public function compile(\Twig_Compiler $compiler)

$compiler->raw(', ');

if (null !== $this->getNode('count')) {
if ($this->hasNode('count')) {
$compiler
->subcompile($this->getNode('count'))
->raw(', ')
Expand All @@ -68,13 +81,13 @@ public function compile(\Twig_Compiler $compiler)

$compiler->raw(', ');

if (null === $this->getNode('domain')) {
if (!$this->hasNode('domain')) {
$compiler->repr('messages');
} else {
$compiler->subcompile($this->getNode('domain'));
}

if (null !== $this->getNode('locale')) {
if ($this->hasNode('locale')) {
$compiler
->raw(', ')
->subcompile($this->getNode('locale'))
Expand All @@ -98,7 +111,7 @@ protected function compileString(\Twig_Node $body, \Twig_Node_Expression_Array $
foreach ($matches[1] as $var) {
$key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
if (!$vars->hasElement($key)) {
if ('count' === $var && null !== $this->getNode('count')) {
if ('count' === $var && $this->hasNode('count')) {
$vars->addElement($this->getNode('count'), $key);
} else {
$varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
}
}
} elseif ($node instanceof TransNode) {
if (null === $node->getNode('domain')) {
if (!$node->hasNode('domain')) {
$node->setNode('domain', $this->scope->get('domain'));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
// extract trans nodes
$this->messages[] = array(
$node->getNode('body')->getAttribute('data'),
$this->getReadDomainFromNode($node->getNode('domain')),
$node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
);
}

Expand Down Expand Up @@ -122,12 +122,8 @@ private function getReadDomainFromArguments(\Twig_Node $arguments, $index)
*
* @return string|null
*/
private function getReadDomainFromNode(\Twig_Node $node = null)
private function getReadDomainFromNode(\Twig_Node $node)
{
if (null === $node) {
return;
}

if ($node instanceof \Twig_Node_Expression_Constant) {
return $node->getAttribute('value');
}
Expand Down
0