]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Node/TransNode.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / Node / TransNode.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\Node;
13
14 /**
15 * @author Fabien Potencier <fabien@symfony.com>
16 */
17 class TransNode extends \Twig_Node
18 {
19 public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
20 {
21 parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
22 }
23
24 /**
25 * Compiles the node to PHP.
26 *
27 * @param \Twig_Compiler $compiler A Twig_Compiler instance
28 */
29 public function compile(\Twig_Compiler $compiler)
30 {
31 $compiler->addDebugInfo($this);
32
33 $vars = $this->getNode('vars');
34 $defaults = new \Twig_Node_Expression_Array(array(), -1);
35 if ($vars instanceof \Twig_Node_Expression_Array) {
36 $defaults = $this->getNode('vars');
37 $vars = null;
38 }
39 list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
40
41 $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
42
43 $compiler
44 ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
45 ->subcompile($msg)
46 ;
47
48 $compiler->raw(', ');
49
50 if (null !== $this->getNode('count')) {
51 $compiler
52 ->subcompile($this->getNode('count'))
53 ->raw(', ')
54 ;
55 }
56
57 if (null !== $vars) {
58 $compiler
59 ->raw('array_merge(')
60 ->subcompile($defaults)
61 ->raw(', ')
62 ->subcompile($this->getNode('vars'))
63 ->raw(')')
64 ;
65 } else {
66 $compiler->subcompile($defaults);
67 }
68
69 $compiler->raw(', ');
70
71 if (null === $this->getNode('domain')) {
72 $compiler->repr('messages');
73 } else {
74 $compiler->subcompile($this->getNode('domain'));
75 }
76
77 if (null !== $this->getNode('locale')) {
78 $compiler
79 ->raw(', ')
80 ->subcompile($this->getNode('locale'))
81 ;
82 }
83 $compiler->raw(");\n");
84 }
85
86 protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
87 {
88 if ($body instanceof \Twig_Node_Expression_Constant) {
89 $msg = $body->getAttribute('value');
90 } elseif ($body instanceof \Twig_Node_Text) {
91 $msg = $body->getAttribute('data');
92 } else {
93 return array($body, $vars);
94 }
95
96 preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
97
98 if (version_compare(\Twig_Environment::VERSION, '1.5', '>=')) {
99 foreach ($matches[1] as $var) {
100 $key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
101 if (!$vars->hasElement($key)) {
102 $vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key);
103 }
104 }
105 } else {
106 $current = array();
107 foreach ($vars as $name => $var) {
108 $current[$name] = true;
109 }
110 foreach ($matches[1] as $var) {
111 if (!isset($current['%'.$var.'%'])) {
112 $vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
113 }
114 }
115 }
116
117 return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
118 }
119 }