2010-09-27 08:45:58 +01:00
|
|
|
<?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
|
|
|
|
{
|
2010-10-01 21:04:11 +01:00
|
|
|
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $isSimple, $lineno, $tag = null)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
2010-10-01 21:04:11 +01:00
|
|
|
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array('is_simple' => $isSimple), $lineno, $tag);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles the node to PHP.
|
|
|
|
*
|
|
|
|
* @param \Twig_Compiler A Twig_Compiler instance
|
|
|
|
*/
|
2011-01-02 14:49:58 +00:00
|
|
|
public function compile(\Twig_Compiler $compiler)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
|
|
|
$compiler->addDebugInfo($this);
|
|
|
|
|
2010-10-02 08:39:48 +01:00
|
|
|
$defaults = null;
|
2010-10-02 08:39:29 +01:00
|
|
|
if ($this->getAttribute('is_simple')) {
|
2010-10-02 08:39:48 +01:00
|
|
|
list($msg, $defaults) = $this->compileString($this->getNode('body'));
|
2010-09-30 18:13:01 +01:00
|
|
|
} else {
|
2010-10-02 08:39:29 +01:00
|
|
|
$msg = $this->getNode('body');
|
2010-09-30 18:13:01 +01:00
|
|
|
}
|
2010-09-27 08:45:58 +01:00
|
|
|
|
2010-10-02 08:39:29 +01:00
|
|
|
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
|
2010-09-27 08:45:58 +01:00
|
|
|
|
|
|
|
$compiler
|
2010-09-30 09:14:28 +01:00
|
|
|
->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
|
2010-09-27 08:45:58 +01:00
|
|
|
->subcompile($msg)
|
|
|
|
;
|
|
|
|
|
|
|
|
$compiler->raw(', ');
|
|
|
|
|
2010-10-02 08:39:29 +01:00
|
|
|
if (null !== $this->getNode('count')) {
|
2010-09-27 08:45:58 +01:00
|
|
|
$compiler
|
2010-10-02 08:39:29 +01:00
|
|
|
->subcompile($this->getNode('count'))
|
2010-09-27 08:45:58 +01:00
|
|
|
->raw(', ')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2010-10-02 08:39:48 +01:00
|
|
|
$compiler->raw('array_merge(');
|
2010-09-27 08:45:58 +01:00
|
|
|
|
2010-10-02 08:39:48 +01:00
|
|
|
if (null === $defaults) {
|
|
|
|
$compiler->raw('array()');
|
|
|
|
} else {
|
|
|
|
$compiler->raw('array(');
|
|
|
|
foreach ($defaults as $default) {
|
2010-09-30 18:13:01 +01:00
|
|
|
$compiler
|
2010-10-02 08:39:48 +01:00
|
|
|
->string('{{ '.$default->getAttribute('name').' }}')
|
2010-09-30 18:13:01 +01:00
|
|
|
->raw(' => ')
|
2010-10-02 08:39:48 +01:00
|
|
|
->subcompile($default)
|
2010-09-30 18:13:01 +01:00
|
|
|
->raw(', ')
|
|
|
|
;
|
|
|
|
}
|
2010-10-02 08:39:48 +01:00
|
|
|
$compiler->raw(')');
|
|
|
|
}
|
|
|
|
|
|
|
|
$compiler->raw(', ');
|
|
|
|
|
|
|
|
if (null === $this->getNode('vars')) {
|
2010-09-30 18:13:01 +01:00
|
|
|
$compiler->raw('array()');
|
2010-10-02 08:39:48 +01:00
|
|
|
} else {
|
|
|
|
$compiler->subcompile($this->getNode('vars'));
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$compiler
|
2010-10-02 08:39:48 +01:00
|
|
|
->raw('), ')
|
2010-10-02 08:39:29 +01:00
|
|
|
->subcompile($this->getNode('domain'))
|
2010-09-27 08:45:58 +01:00
|
|
|
->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) {
|
2010-10-02 08:39:29 +01:00
|
|
|
$n = $node->getNode('expr');
|
2010-09-27 08:45:58 +01:00
|
|
|
while ($n instanceof \Twig_Node_Expression_Filter) {
|
2010-10-02 08:39:29 +01:00
|
|
|
$n = $n->getNode('node');
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
2010-10-02 08:39:29 +01:00
|
|
|
$msg .= sprintf('{{ %s }}', $n->getAttribute('name'));
|
|
|
|
$vars[] = new \Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
|
2010-09-27 08:45:58 +01:00
|
|
|
} else {
|
2010-10-02 08:39:29 +01:00
|
|
|
$msg .= $node->getAttribute('data');
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-03 19:09:48 +00:00
|
|
|
return array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()), $vars);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
}
|