[ Index ]

PHP Cross Reference of phpBB-3.2.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 = array();
  32  
  33      public function enable()
  34      {
  35          $this->enabled = true;
  36          $this->messages = array();
  37      }
  38  
  39      public function disable()
  40      {
  41          $this->enabled = false;
  42          $this->messages = array();
  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[] = array(
  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[] = array(
  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[] = array(
  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      public function getPriority()
 102      {
 103          return 0;
 104      }
 105  
 106      /**
 107       * @param Node $arguments
 108       * @param int  $index
 109       *
 110       * @return string|null
 111       */
 112      private function getReadDomainFromArguments(Node $arguments, $index)
 113      {
 114          if ($arguments->hasNode('domain')) {
 115              $argument = $arguments->getNode('domain');
 116          } elseif ($arguments->hasNode($index)) {
 117              $argument = $arguments->getNode($index);
 118          } else {
 119              return;
 120          }
 121  
 122          return $this->getReadDomainFromNode($argument);
 123      }
 124  
 125      /**
 126       * @return string|null
 127       */
 128      private function getReadDomainFromNode(Node $node)
 129      {
 130          if ($node instanceof ConstantExpression) {
 131              return $node->getAttribute('value');
 132          }
 133  
 134          return self::UNDEFINED_DOMAIN;
 135      }
 136  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1