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