[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/NodeVisitor/ -> TranslationNodeVisitor.php (source)

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


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1