TransNode.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\Node;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. *
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.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, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param \Twig_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. ->raw(");\n")
  64. ;
  65. }
  66. protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
  67. {
  68. $compiler->raw('array(');
  69. foreach ($defaults as $name => $default) {
  70. $compiler
  71. ->repr($name)
  72. ->raw(' => ')
  73. ->subcompile($default)
  74. ->raw(', ')
  75. ;
  76. }
  77. $compiler->raw(')');
  78. }
  79. protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
  80. {
  81. if ($body instanceof \Twig_Node_Expression_Constant) {
  82. $msg = $body->getAttribute('value');
  83. } elseif ($body instanceof \Twig_Node_Text) {
  84. $msg = $body->getAttribute('data');
  85. } else {
  86. return array($body, $vars);
  87. }
  88. $current = array();
  89. foreach ($vars as $name => $var) {
  90. $current[$name] = true;
  91. }
  92. preg_match_all('/\%([^\%]+)\%/', $msg, $matches);
  93. foreach ($matches[1] as $var) {
  94. if (!isset($current['%'.$var.'%'])) {
  95. $vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
  96. }
  97. }
  98. return array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()), $vars);
  99. }
  100. }