PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/twig/extensions/lib/Twig/Extensions/Node/Trans.php

https://gitlab.com/ineszribi/SmartBookStoreWeb
PHP | 146 lines | 100 code | 21 blank | 25 comment | 18 complexity | 4f9e89ca0bbd63b1c8751a8ebcf92271 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Represents a trans node.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class Twig_Extensions_Node_Trans extends Twig_Node
  17. {
  18. public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, Twig_NodeInterface $notes = null, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural, 'notes' => $notes), 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. list($msg, $vars) = $this->compileString($this->getNode('body'));
  31. if (null !== $this->getNode('plural')) {
  32. list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
  33. $vars = array_merge($vars, $vars1);
  34. }
  35. $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
  36. if (null !== $notes = $this->getNode('notes')) {
  37. $message = trim($notes->getAttribute('data'));
  38. // line breaks are not allowed cause we want a single line comment
  39. $message = str_replace(array("\n", "\r"), " ", $message);
  40. $compiler->write("// notes: {$message}\n");
  41. }
  42. if ($vars) {
  43. $compiler
  44. ->write('echo strtr('.$function.'(')
  45. ->subcompile($msg)
  46. ;
  47. if (null !== $this->getNode('plural')) {
  48. $compiler
  49. ->raw(', ')
  50. ->subcompile($msg1)
  51. ->raw(', abs(')
  52. ->subcompile($this->getNode('count'))
  53. ->raw(')')
  54. ;
  55. }
  56. $compiler->raw('), array(');
  57. foreach ($vars as $var) {
  58. if ('count' === $var->getAttribute('name')) {
  59. $compiler
  60. ->string('%count%')
  61. ->raw(' => abs(')
  62. ->subcompile($this->getNode('count'))
  63. ->raw('), ')
  64. ;
  65. } else {
  66. $compiler
  67. ->string('%'.$var->getAttribute('name').'%')
  68. ->raw(' => ')
  69. ->subcompile($var)
  70. ->raw(', ')
  71. ;
  72. }
  73. }
  74. $compiler->raw("));\n");
  75. } else {
  76. $compiler
  77. ->write('echo '.$function.'(')
  78. ->subcompile($msg)
  79. ;
  80. if (null !== $this->getNode('plural')) {
  81. $compiler
  82. ->raw(', ')
  83. ->subcompile($msg1)
  84. ->raw(', abs(')
  85. ->subcompile($this->getNode('count'))
  86. ->raw(')')
  87. ;
  88. }
  89. $compiler->raw(");\n");
  90. }
  91. }
  92. /**
  93. * @param Twig_NodeInterface $body A Twig_NodeInterface instance
  94. *
  95. * @return array
  96. */
  97. protected function compileString(Twig_NodeInterface $body)
  98. {
  99. if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) {
  100. return array($body, array());
  101. }
  102. $vars = array();
  103. if (count($body)) {
  104. $msg = '';
  105. foreach ($body as $node) {
  106. if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) {
  107. $node = $node->getNode(1);
  108. }
  109. if ($node instanceof Twig_Node_Print) {
  110. $n = $node->getNode('expr');
  111. while ($n instanceof Twig_Node_Expression_Filter) {
  112. $n = $n->getNode('node');
  113. }
  114. $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
  115. $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
  116. } else {
  117. $msg .= $node->getAttribute('data');
  118. }
  119. }
  120. } else {
  121. $msg = $body->getAttribute('data');
  122. }
  123. return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
  124. }
  125. }