]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / NodeVisitor / TranslationDefaultDomainNodeVisitor.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Bridge\Twig\NodeVisitor;
13
14 use Symfony\Bridge\Twig\Node\TransNode;
15 use Symfony\Bridge\Twig\Node\TransDefaultDomainNode;
16
17 /**
18 * TranslationDefaultDomainNodeVisitor.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 */
22 class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
23 {
24 /**
25 * @var Scope
26 */
27 private $scope;
28
29 /**
30 * Constructor.
31 */
32 public function __construct()
33 {
34 $this->scope = new Scope();
35 }
36
37 /**
38 * {@inheritdoc}
39 */
40 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
41 {
42 if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
43 $this->scope = $this->scope->enter();
44 }
45
46 if ($node instanceof TransDefaultDomainNode) {
47 if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) {
48 $this->scope->set('domain', $node->getNode('expr'));
49
50 return $node;
51 } else {
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()));
55
56 return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine());
57 }
58 }
59
60 if (!$this->scope->has('domain')) {
61 return $node;
62 }
63
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()));
70 }
71
72 $arguments->setNode($ind, $this->scope->get('domain'));
73 }
74 } elseif ($node instanceof TransNode) {
75 if (null === $node->getNode('domain')) {
76 $node->setNode('domain', $this->scope->get('domain'));
77 }
78 }
79
80 return $node;
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
87 {
88 if ($node instanceof TransDefaultDomainNode) {
89 return false;
90 }
91
92 if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
93 $this->scope = $this->scope->leave();
94 }
95
96 return $node;
97 }
98
99 /**
100 * {@inheritdoc}
101 */
102 public function getPriority()
103 {
104 return -10;
105 }
106 }