diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php b/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php new file mode 100644 index 0000000000..4993aed90a --- /dev/null +++ b/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php @@ -0,0 +1,125 @@ + + */ +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); + } +} diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php index f6869dee77..f6a764bc6c 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -21,32 +21,47 @@ use Symfony\Bridge\Twig\Node\TransDefaultDomainNode; */ 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); } 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; } @@ -58,11 +73,11 @@ class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface $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')); } } @@ -78,6 +93,10 @@ class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface return false; } + if ($node instanceof \Twig_Node_Block) { + $this->scope = $this->scope->leave(); + } + return $node; }