diff options
Diffstat (limited to 'vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php')
-rw-r--r-- | vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php new file mode 100644 index 00000000..592c2506 --- /dev/null +++ b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php | |||
@@ -0,0 +1,137 @@ | |||
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 | |||
16 | /** | ||
17 | * TranslationNodeVisitor extracts translation messages. | ||
18 | * | ||
19 | * @author Fabien Potencier <fabien@symfony.com> | ||
20 | */ | ||
21 | class TranslationNodeVisitor implements \Twig_NodeVisitorInterface | ||
22 | { | ||
23 | const UNDEFINED_DOMAIN = '_undefined'; | ||
24 | |||
25 | private $enabled = false; | ||
26 | private $messages = array(); | ||
27 | |||
28 | public function enable() | ||
29 | { | ||
30 | $this->enabled = true; | ||
31 | $this->messages = array(); | ||
32 | } | ||
33 | |||
34 | public function disable() | ||
35 | { | ||
36 | $this->enabled = false; | ||
37 | $this->messages = array(); | ||
38 | } | ||
39 | |||
40 | public function getMessages() | ||
41 | { | ||
42 | return $this->messages; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * {@inheritdoc} | ||
47 | */ | ||
48 | public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) | ||
49 | { | ||
50 | if (!$this->enabled) { | ||
51 | return $node; | ||
52 | } | ||
53 | |||
54 | if ( | ||
55 | $node instanceof \Twig_Node_Expression_Filter && | ||
56 | 'trans' === $node->getNode('filter')->getAttribute('value') && | ||
57 | $node->getNode('node') instanceof \Twig_Node_Expression_Constant | ||
58 | ) { | ||
59 | // extract constant nodes with a trans filter | ||
60 | $this->messages[] = array( | ||
61 | $node->getNode('node')->getAttribute('value'), | ||
62 | $this->getReadDomainFromArguments($node->getNode('arguments'), 1), | ||
63 | ); | ||
64 | } elseif ( | ||
65 | $node instanceof \Twig_Node_Expression_Filter && | ||
66 | 'transchoice' === $node->getNode('filter')->getAttribute('value') && | ||
67 | $node->getNode('node') instanceof \Twig_Node_Expression_Constant | ||
68 | ) { | ||
69 | // extract constant nodes with a trans filter | ||
70 | $this->messages[] = array( | ||
71 | $node->getNode('node')->getAttribute('value'), | ||
72 | $this->getReadDomainFromArguments($node->getNode('arguments'), 2), | ||
73 | ); | ||
74 | } elseif ($node instanceof TransNode) { | ||
75 | // extract trans nodes | ||
76 | $this->messages[] = array( | ||
77 | $node->getNode('body')->getAttribute('data'), | ||
78 | $this->getReadDomainFromNode($node->getNode('domain')), | ||
79 | ); | ||
80 | } | ||
81 | |||
82 | return $node; | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * {@inheritdoc} | ||
87 | */ | ||
88 | public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) | ||
89 | { | ||
90 | return $node; | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * {@inheritdoc} | ||
95 | */ | ||
96 | public function getPriority() | ||
97 | { | ||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * @param \Twig_Node $arguments | ||
103 | * @param int $index | ||
104 | * | ||
105 | * @return string|null | ||
106 | */ | ||
107 | private function getReadDomainFromArguments(\Twig_Node $arguments, $index) | ||
108 | { | ||
109 | if ($arguments->hasNode('domain')) { | ||
110 | $argument = $arguments->getNode('domain'); | ||
111 | } elseif ($arguments->hasNode($index)) { | ||
112 | $argument = $arguments->getNode($index); | ||
113 | } else { | ||
114 | return null; | ||
115 | } | ||
116 | |||
117 | return $this->getReadDomainFromNode($argument); | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * @param \Twig_Node $node | ||
122 | * | ||
123 | * @return string|null | ||
124 | */ | ||
125 | private function getReadDomainFromNode(\Twig_Node $node = null) | ||
126 | { | ||
127 | if (null === $node) { | ||
128 | return null; | ||
129 | } | ||
130 | |||
131 | if ($node instanceof \Twig_Node_Expression_Constant) { | ||
132 | return $node->getAttribute('value'); | ||
133 | } | ||
134 | |||
135 | return self::UNDEFINED_DOMAIN; | ||
136 | } | ||
137 | } | ||