4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\NodeVisitor
;
14 use Symfony\Bridge\Twig\Node\TransNode
;
15 use Symfony\Bridge\Twig\Node\TransDefaultDomainNode
;
18 * TranslationDefaultDomainNodeVisitor.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class TranslationDefaultDomainNodeVisitor
implements \Twig_NodeVisitorInterface
32 public function __construct()
34 $this->scope
= new Scope();
40 public function enterNode(\Twig_NodeInterface
$node, \Twig_Environment
$env)
42 if ($node instanceof \Twig_Node_Block
|| $node instanceof \Twig_Node_Module
) {
43 $this->scope
= $this->scope
->enter();
46 if ($node instanceof TransDefaultDomainNode
) {
47 if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant
) {
48 $this->scope
->set('domain', $node->getNode('expr'));
52 $var = $env->getParser()->getVarName();
53 $name = new \
Twig_Node_Expression_AssignName($var, $node->getLine());
54 $this->scope
->set('domain', new \
Twig_Node_Expression_Name($var, $node->getLine()));
56 return new \
Twig_Node_Set(false, new \
Twig_Node(array($name)), new \
Twig_Node(array($node->getNode('expr'))), $node->getLine());
60 if (!$this->scope
->has('domain')) {
64 if ($node instanceof \Twig_Node_Expression_Filter
&& in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
65 $ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
66 $arguments = $node->getNode('arguments');
67 if (!$arguments->hasNode($ind)) {
68 if (!$arguments->hasNode($ind - 1)) {
69 $arguments->setNode($ind - 1, new \
Twig_Node_Expression_Array(array(), $node->getLine()));
72 $arguments->setNode($ind, $this->scope
->get('domain'));
74 } elseif ($node instanceof TransNode
) {
75 if (null === $node->getNode('domain')) {
76 $node->setNode('domain', $this->scope
->get('domain'));
86 public function leaveNode(\Twig_NodeInterface
$node, \Twig_Environment
$env)
88 if ($node instanceof TransDefaultDomainNode
) {
92 if ($node instanceof \Twig_Node_Block
|| $node instanceof \Twig_Node_Module
) {
93 $this->scope
= $this->scope
->leave();
102 public function getPriority()