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

Skip to content

[TwigBridge] fixes TranslationDefaultDomainNodeVisitor #7281

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 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
125 changes: 125 additions & 0 deletions src/Symfony/Bridge/Twig/NodeVisitor/Scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Symfony\Bridge\Twig\NodeVisitor;

/**
* @author Jean-François Simon <[email protected]>
*/
class Scope
{
/**
* @var Scope|null
*/
private $parent;

/**
* @var Scope[]
*/
private $children;

/**
* @var array
*/
private $data;

/**
* @var boolean
*/
private $left;

/**
* @param Scope $parent
*/
public function __construct(Scope $parent = null)
{
$this->parent = $parent;
$this->left = false;
}

/**
* Opens a new child scope.
*
* @return Scope
*/
public function enter()
{
$child = new self($this);
$this->children[] = $child;

return $child;
}

/**
* Closes current scope and returns parent one.
*
* @return Scope|null
*/
public function leave()
{
$this->left = true;

return $this->parent;
}

/**
* Stores data into current scope.
*
* @param string $key
* @param mixed $value
*
* @throws \LogicException
*
* @return Scope Current scope
*/
public function set($key, $value)
{
if ($this->left) {
throw new \LogicException('Left scope is not mutable.');
}

$this->data[$key] = $value;

return $this;
}

/**
* Tests if a data is visible from current scope.
*
* @param string $key
*
* @return boolean
*/
public function has($key)
{
if (array_key_exists($key, $this->data)) {
return true;
}

if (null === $this->parent) {
return false;
}

return $this->parent->has($key);
}

/**
* Returns data visible from current scope.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}

if (null === $this->parent) {
return $default;
}

return $this->parent->get($key, $default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,47 @@
*/
class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
{
private $domain;
/**
* @var Scope
*/
private $scope;

/**
* Constructor.
*/
public function __construct()
{
$this->scope = new Scope();
}

/**
* {@inheritdoc}
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Block) {
$this->scope = $this->scope->enter();
}

if ($node instanceof \Twig_Node_Module) {
$this->domain = null;
$this->scope->set('domain', null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfsimon I think the issue might be here: you are setting it to null, but then, the corresponding code was previously null === $this->domain and is now replaced by !$this->scope->has('domain') (which is false as it has a null value)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof exact, thanks!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But array_key_exists() used by Scope::has($key) also returns true if the value is null? I thought that this is the reason for not using isset()?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have read it twice. Sorry. Is somebody working on this? Otherwise I would build a PR for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lennerd the problem was here: jfsimon@fe4cc24#L0R37 ;)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. With the new scope, every variable in the scope defaults to null. Thanks for the hint. ;)

}

if ($node instanceof TransDefaultDomainNode) {
if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) {
$this->domain = $node->getNode('expr');
$this->scope->set('domain', $node->getNode('expr'));

return $node;
} else {
$var = $env->getParser()->getVarName();
$name = new \Twig_Node_Expression_AssignName($var, $node->getLine());
$this->domain = new \Twig_Node_Expression_Name($var, $node->getLine());
$this->scope->set('domain', new \Twig_Node_Expression_Name($var, $node->getLine()));

return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine());
}
}

if (null === $this->domain) {
if (!$this->scope->has('domain')) {
return $node;
}

Expand All @@ -58,11 +73,11 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
}

$arguments->setNode($ind, $this->domain);
$arguments->setNode($ind, $this->scope->get('domain'));
}
} elseif ($node instanceof TransNode) {
if (null === $node->getNode('domain')) {
$node->setNode('domain', $this->domain);
$node->setNode('domain', $this->scope->get('domain'));
}
}

Expand All @@ -78,6 +93,10 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
return false;
}

if ($node instanceof \Twig_Node_Block) {
$this->scope = $this->scope->leave();
}

return $node;
}

Expand Down