123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace Symfony\Bundle\TwigBundle\Node;
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- *
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class TransNode extends \Twig_Node
- {
- public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $isSimple, $lineno, $tag = null)
- {
- parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array('is_simple' => $isSimple), $lineno, $tag);
- }
- /**
- * Compiles the node to PHP.
- *
- * @param \Twig_Compiler A Twig_Compiler instance
- */
- public function compile($compiler)
- {
- $compiler->addDebugInfo($this);
- if ($this['is_simple']) {
- list($msg, $vars) = $this->compileString($this->body);
- } else {
- $msg = $this->body;
- $vars = $this->vars;
- }
- $method = null === $this->count ? 'trans' : 'transChoice';
- $compiler
- ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
- ->subcompile($msg)
- ;
- $compiler->raw(', ');
- if (null !== $this->count) {
- $compiler
- ->subcompile($this->count)
- ->raw(', ')
- ;
- }
- $compiler->raw('array(');
- if (is_array($vars)) {
- foreach ($vars as $var) {
- $compiler
- ->string('{{ '.$var['name'].' }}')
- ->raw(' => ')
- ->subcompile($var)
- ->raw(', ')
- ;
- }
- } elseif (null !== $vars) {
- $compiler->subcompile($vars);
- } else {
- $compiler->raw('array()');
- }
- $compiler
- ->raw("), ")
- ->subcompile($this->domain)
- ->raw(");\n")
- ;
- }
- protected function compileString(\Twig_NodeInterface $body)
- {
- if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant) {
- return array($body, array());
- }
- $msg = '';
- $vars = array();
- foreach ($body as $node) {
- if ($node instanceof \Twig_Node_Print) {
- $n = $node->expr;
- while ($n instanceof \Twig_Node_Expression_Filter) {
- $n = $n->node;
- }
- $msg .= sprintf('{{ %s }}', $n['name']);
- $vars[] = new \Twig_Node_Expression_Name($n['name'], $n->getLine());
- } else {
- $msg .= $node['data'];
- }
- }
- return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars);
- }
- }
|