2010-09-27 08:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\TwigBundle\Node;
|
|
|
|
|
2010-09-27 08:45:58 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*/
|
|
|
|
class TransNode extends \Twig_Node
|
|
|
|
{
|
2011-01-03 18:50:23 +00:00
|
|
|
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno, $tag = null)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
2011-01-03 18:50:23 +00:00
|
|
|
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $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);
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
$vars = $this->getNode('vars');
|
|
|
|
$defaults = new \Twig_Node_Expression_Array(array(), -1);
|
|
|
|
if ($vars instanceof \Twig_Node_Expression_Array) {
|
|
|
|
$defaults = $this->getNode('vars');
|
|
|
|
$vars = null;
|
2010-09-30 18:13:01 +01:00
|
|
|
}
|
2010-09-27 08:45:58 +01:00
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
|
|
|
|
|
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(', ')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
if (null !== $vars) {
|
|
|
|
$compiler->raw('array_merge(');
|
|
|
|
$this->compileDefaults($compiler, $defaults);
|
|
|
|
$compiler
|
|
|
|
->raw(', ')
|
|
|
|
->subcompile($this->getNode('vars'))
|
|
|
|
->raw(')')
|
|
|
|
;
|
2010-10-02 08:39:48 +01:00
|
|
|
} else {
|
2011-01-03 18:50:23 +00:00
|
|
|
$this->compileDefaults($compiler, $defaults);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$compiler
|
2011-01-03 18:50:23 +00:00
|
|
|
->raw(', ')
|
2010-10-02 08:39:29 +01:00
|
|
|
->subcompile($this->getNode('domain'))
|
2010-09-27 08:45:58 +01:00
|
|
|
->raw(");\n")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
2011-01-03 18:50:23 +00:00
|
|
|
$compiler->raw('array(');
|
|
|
|
foreach ($defaults as $name => $default) {
|
|
|
|
$compiler
|
|
|
|
->repr($name)
|
|
|
|
->raw(' => ')
|
|
|
|
->subcompile($default)
|
|
|
|
->raw(', ')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
$compiler->raw(')');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
|
|
|
|
{
|
|
|
|
if ($body instanceof \Twig_Node_Expression_Constant) {
|
|
|
|
$msg = $body->getAttribute('value');
|
|
|
|
} elseif ($body instanceof \Twig_Node_Text) {
|
|
|
|
$msg = $body->getAttribute('data');
|
|
|
|
} else {
|
|
|
|
return array($body, $vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = array();
|
|
|
|
foreach ($vars as $name => $var) {
|
|
|
|
$current[$name] = true;
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
preg_match_all('/\%([^\%]+)\%/', $msg, $matches);
|
|
|
|
foreach ($matches[1] as $var) {
|
|
|
|
if (!isset($current['%'.$var.'%'])) {
|
|
|
|
$vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
|
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
|
|
|
}
|
|
|
|
}
|