TransNode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /**
  12. *
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class TransNode extends \Twig_Node
  17. {
  18. public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
  19. {
  20. parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param \Twig_Compiler $compiler A Twig_Compiler instance
  26. */
  27. public function compile(\Twig_Compiler $compiler)
  28. {
  29. $compiler->addDebugInfo($this);
  30. $vars = $this->getNode('vars');
  31. $defaults = new \Twig_Node_Expression_Array(array(), -1);
  32. if ($vars instanceof \Twig_Node_Expression_Array) {
  33. $defaults = $this->getNode('vars');
  34. $vars = null;
  35. }
  36. list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
  37. $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
  38. $compiler
  39. ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
  40. ->subcompile($msg)
  41. ;
  42. $compiler->raw(', ');
  43. if (null !== $this->getNode('count')) {
  44. $compiler
  45. ->subcompile($this->getNode('count'))
  46. ->raw(', ')
  47. ;
  48. }
  49. if (null !== $vars) {
  50. $compiler->raw('array_merge(');
  51. $this->compileDefaults($compiler, $defaults);
  52. $compiler
  53. ->raw(', ')
  54. ->subcompile($this->getNode('vars'))
  55. ->raw(')')
  56. ;
  57. } else {
  58. $this->compileDefaults($compiler, $defaults);
  59. }
  60. $compiler
  61. ->raw(', ')
  62. ->subcompile($this->getNode('domain'))
  63. ;
  64. if (null !== $this->getNode('locale')) {
  65. $compiler
  66. ->raw(', ')
  67. ->subcompile($this->getNode('locale'))
  68. ;
  69. }
  70. $compiler->raw(");\n");
  71. }
  72. protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
  73. {
  74. $compiler->raw('array(');
  75. foreach ($defaults as $name => $default) {
  76. $compiler
  77. ->repr($name)
  78. ->raw(' => ')
  79. ->subcompile($default)
  80. ->raw(', ')
  81. ;
  82. }
  83. $compiler->raw(')');
  84. }
  85. protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
  86. {
  87. if ($body instanceof \Twig_Node_Expression_Constant) {
  88. $msg = $body->getAttribute('value');
  89. } elseif ($body instanceof \Twig_Node_Text) {
  90. $msg = $body->getAttribute('data');
  91. } else {
  92. return array($body, $vars);
  93. }
  94. $current = array();
  95. foreach ($vars as $name => $var) {
  96. $current[$name] = true;
  97. }
  98. preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
  99. foreach ($matches[1] as $var) {
  100. if (!isset($current['%'.$var.'%'])) {
  101. $vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
  102. }
  103. }
  104. return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
  105. }
  106. }