TransNode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, $isSimple, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array('is_simple' => $isSimple), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param \Twig_Compiler A Twig_Compiler instance
  26. */
  27. public function compile($compiler)
  28. {
  29. $compiler->addDebugInfo($this);
  30. if ($this['is_simple']) {
  31. list($msg, $vars) = $this->compileString($this->body);
  32. } else {
  33. $msg = $this->body;
  34. $vars = $this->vars;
  35. }
  36. $method = null === $this->count ? 'trans' : 'transChoice';
  37. $compiler
  38. ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
  39. ->subcompile($msg)
  40. ;
  41. $compiler->raw(', ');
  42. if (null !== $this->count) {
  43. $compiler
  44. ->subcompile($this->count)
  45. ->raw(', ')
  46. ;
  47. }
  48. $compiler->raw('array(');
  49. if (is_array($vars)) {
  50. foreach ($vars as $var) {
  51. $compiler
  52. ->string('{{ '.$var['name'].' }}')
  53. ->raw(' => ')
  54. ->subcompile($var)
  55. ->raw(', ')
  56. ;
  57. }
  58. } elseif (null !== $vars) {
  59. $compiler->subcompile($vars);
  60. } else {
  61. $compiler->raw('array()');
  62. }
  63. $compiler
  64. ->raw("), ")
  65. ->subcompile($this->domain)
  66. ->raw(");\n")
  67. ;
  68. }
  69. protected function compileString(\Twig_NodeInterface $body)
  70. {
  71. if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant) {
  72. return array($body, array());
  73. }
  74. $msg = '';
  75. $vars = array();
  76. foreach ($body as $node) {
  77. if ($node instanceof \Twig_Node_Print) {
  78. $n = $node->expr;
  79. while ($n instanceof \Twig_Node_Expression_Filter) {
  80. $n = $n->node;
  81. }
  82. $msg .= sprintf('{{ %s }}', $n['name']);
  83. $vars[] = new \Twig_Node_Expression_Name($n['name'], $n->getLine());
  84. } else {
  85. $msg .= $node['data'];
  86. }
  87. }
  88. return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars);
  89. }
  90. }