TransNode.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Node;
  11. use Twig\Compiler;
  12. use Twig\Node\Expression\AbstractExpression;
  13. use Twig\Node\Expression\ArrayExpression;
  14. use Twig\Node\Expression\ConstantExpression;
  15. use Twig\Node\Expression\NameExpression;
  16. use Twig\Node\Node;
  17. use Twig\Node\TextNode;
  18. // BC/FC with namespaced Twig
  19. class_exists('Twig\Node\Expression\ArrayExpression');
  20. /**
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class TransNode extends Node
  24. {
  25. public function __construct(Node $body, Node $domain = null, AbstractExpression $count = null, AbstractExpression $vars = null, AbstractExpression $locale = null, $lineno = 0, $tag = null)
  26. {
  27. $nodes = array('body' => $body);
  28. if (null !== $domain) {
  29. $nodes['domain'] = $domain;
  30. }
  31. if (null !== $count) {
  32. $nodes['count'] = $count;
  33. }
  34. if (null !== $vars) {
  35. $nodes['vars'] = $vars;
  36. }
  37. if (null !== $locale) {
  38. $nodes['locale'] = $locale;
  39. }
  40. parent::__construct($nodes, array(), $lineno, $tag);
  41. }
  42. public function compile(Compiler $compiler)
  43. {
  44. $compiler->addDebugInfo($this);
  45. $defaults = new ArrayExpression(array(), -1);
  46. if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof ArrayExpression) {
  47. $defaults = $this->getNode('vars');
  48. $vars = null;
  49. }
  50. list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
  51. $method = !$this->hasNode('count') ? 'trans' : 'transChoice';
  52. $compiler
  53. ->write('echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->getTranslator()->'.$method.'(')
  54. ->subcompile($msg)
  55. ;
  56. $compiler->raw(', ');
  57. if ($this->hasNode('count')) {
  58. $compiler
  59. ->subcompile($this->getNode('count'))
  60. ->raw(', ')
  61. ;
  62. }
  63. if (null !== $vars) {
  64. $compiler
  65. ->raw('array_merge(')
  66. ->subcompile($defaults)
  67. ->raw(', ')
  68. ->subcompile($this->getNode('vars'))
  69. ->raw(')')
  70. ;
  71. } else {
  72. $compiler->subcompile($defaults);
  73. }
  74. $compiler->raw(', ');
  75. if (!$this->hasNode('domain')) {
  76. $compiler->repr('messages');
  77. } else {
  78. $compiler->subcompile($this->getNode('domain'));
  79. }
  80. if ($this->hasNode('locale')) {
  81. $compiler
  82. ->raw(', ')
  83. ->subcompile($this->getNode('locale'))
  84. ;
  85. }
  86. $compiler->raw(");\n");
  87. }
  88. protected function compileString(Node $body, ArrayExpression $vars, $ignoreStrictCheck = false)
  89. {
  90. if ($body instanceof ConstantExpression) {
  91. $msg = $body->getAttribute('value');
  92. } elseif ($body instanceof TextNode) {
  93. $msg = $body->getAttribute('data');
  94. } else {
  95. return array($body, $vars);
  96. }
  97. preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
  98. foreach ($matches[1] as $var) {
  99. $key = new ConstantExpression('%'.$var.'%', $body->getTemplateLine());
  100. if (!$vars->hasElement($key)) {
  101. if ('count' === $var && $this->hasNode('count')) {
  102. $vars->addElement($this->getNode('count'), $key);
  103. } else {
  104. $varExpr = new NameExpression($var, $body->getTemplateLine());
  105. $varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
  106. $vars->addElement($varExpr, $key);
  107. }
  108. }
  109. }
  110. return array(new ConstantExpression(str_replace('%%', '%', trim($msg)), $body->getTemplateLine()), $vars);
  111. }
  112. }