TranslationNodeVisitor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\NodeVisitor;
  11. use Symfony\Bridge\Twig\Node\TransNode;
  12. use Twig\Environment;
  13. use Twig\Node\Expression\ConstantExpression;
  14. use Twig\Node\Expression\FilterExpression;
  15. use Twig\Node\Node;
  16. use Twig\NodeVisitor\AbstractNodeVisitor;
  17. /**
  18. * TranslationNodeVisitor extracts translation messages.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class TranslationNodeVisitor extends AbstractNodeVisitor
  23. {
  24. const UNDEFINED_DOMAIN = '_undefined';
  25. private $enabled = false;
  26. private $messages = array();
  27. public function enable()
  28. {
  29. $this->enabled = true;
  30. $this->messages = array();
  31. }
  32. public function disable()
  33. {
  34. $this->enabled = false;
  35. $this->messages = array();
  36. }
  37. public function getMessages()
  38. {
  39. return $this->messages;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function doEnterNode(Node $node, Environment $env)
  45. {
  46. if (!$this->enabled) {
  47. return $node;
  48. }
  49. if (
  50. $node instanceof FilterExpression &&
  51. 'trans' === $node->getNode('filter')->getAttribute('value') &&
  52. $node->getNode('node') instanceof ConstantExpression
  53. ) {
  54. // extract constant nodes with a trans filter
  55. $this->messages[] = array(
  56. $node->getNode('node')->getAttribute('value'),
  57. $this->getReadDomainFromArguments($node->getNode('arguments'), 1),
  58. );
  59. } elseif (
  60. $node instanceof FilterExpression &&
  61. 'transchoice' === $node->getNode('filter')->getAttribute('value') &&
  62. $node->getNode('node') instanceof ConstantExpression
  63. ) {
  64. // extract constant nodes with a trans filter
  65. $this->messages[] = array(
  66. $node->getNode('node')->getAttribute('value'),
  67. $this->getReadDomainFromArguments($node->getNode('arguments'), 2),
  68. );
  69. } elseif ($node instanceof TransNode) {
  70. // extract trans nodes
  71. $this->messages[] = array(
  72. $node->getNode('body')->getAttribute('data'),
  73. $node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
  74. );
  75. }
  76. return $node;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function doLeaveNode(Node $node, Environment $env)
  82. {
  83. return $node;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getPriority()
  89. {
  90. return 0;
  91. }
  92. /**
  93. * @param Node $arguments
  94. * @param int $index
  95. *
  96. * @return string|null
  97. */
  98. private function getReadDomainFromArguments(Node $arguments, $index)
  99. {
  100. if ($arguments->hasNode('domain')) {
  101. $argument = $arguments->getNode('domain');
  102. } elseif ($arguments->hasNode($index)) {
  103. $argument = $arguments->getNode($index);
  104. } else {
  105. return;
  106. }
  107. return $this->getReadDomainFromNode($argument);
  108. }
  109. /**
  110. * @param Node $node
  111. *
  112. * @return string|null
  113. */
  114. private function getReadDomainFromNode(Node $node)
  115. {
  116. if ($node instanceof ConstantExpression) {
  117. return $node->getAttribute('value');
  118. }
  119. return self::UNDEFINED_DOMAIN;
  120. }
  121. }