This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bridge/Twig/Node/TransNode.php

133 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\Node;
2017-05-30 11:15:38 +01:00
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\TextNode;
// BC/FC with namespaced Twig
class_exists('Twig\Node\Expression\ArrayExpression');
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
2017-05-30 11:15:38 +01:00
class TransNode extends Node
{
2017-05-30 11:15:38 +01:00
public function __construct(Node $body, Node $domain = null, AbstractExpression $count = null, AbstractExpression $vars = null, AbstractExpression $locale = null, $lineno = 0, $tag = null)
{
$nodes = array('body' => $body);
if (null !== $domain) {
$nodes['domain'] = $domain;
}
if (null !== $count) {
$nodes['count'] = $count;
}
if (null !== $vars) {
$nodes['vars'] = $vars;
}
if (null !== $locale) {
$nodes['locale'] = $locale;
}
parent::__construct($nodes, array(), $lineno, $tag);
}
2017-05-30 11:15:38 +01:00
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
2017-05-30 11:15:38 +01:00
$defaults = new ArrayExpression(array(), -1);
if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof ArrayExpression) {
$defaults = $this->getNode('vars');
$vars = null;
}
2014-04-28 10:30:24 +01:00
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
$method = !$this->hasNode('count') ? 'trans' : 'transChoice';
$compiler
2016-09-28 19:43:20 +01:00
->write('echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->getTranslator()->'.$method.'(')
->subcompile($msg)
;
$compiler->raw(', ');
if ($this->hasNode('count')) {
$compiler
->subcompile($this->getNode('count'))
->raw(', ')
;
}
if (null !== $vars) {
$compiler
2011-12-23 07:55:49 +00:00
->raw('array_merge(')
->subcompile($defaults)
->raw(', ')
->subcompile($this->getNode('vars'))
->raw(')')
;
} else {
2011-12-23 07:55:49 +00:00
$compiler->subcompile($defaults);
}
$compiler->raw(', ');
if (!$this->hasNode('domain')) {
$compiler->repr('messages');
} else {
$compiler->subcompile($this->getNode('domain'));
}
if ($this->hasNode('locale')) {
$compiler
->raw(', ')
->subcompile($this->getNode('locale'))
;
}
$compiler->raw(");\n");
}
2017-05-30 11:15:38 +01:00
protected function compileString(Node $body, ArrayExpression $vars, $ignoreStrictCheck = false)
{
2017-05-30 11:15:38 +01:00
if ($body instanceof ConstantExpression) {
$msg = $body->getAttribute('value');
2017-05-30 11:15:38 +01:00
} elseif ($body instanceof TextNode) {
$msg = $body->getAttribute('data');
} else {
return array($body, $vars);
}
preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
foreach ($matches[1] as $var) {
2017-05-30 11:15:38 +01:00
$key = new ConstantExpression('%'.$var.'%', $body->getTemplateLine());
if (!$vars->hasElement($key)) {
if ('count' === $var && $this->hasNode('count')) {
$vars->addElement($this->getNode('count'), $key);
} else {
2017-05-30 11:15:38 +01:00
$varExpr = new NameExpression($var, $body->getTemplateLine());
$varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
$vars->addElement($varExpr, $key);
}
}
}
2017-05-30 11:15:38 +01:00
return array(new ConstantExpression(str_replace('%%', '%', trim($msg)), $body->getTemplateLine()), $vars);
}
}