[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/NodeVisitor/ -> TranslationDefaultDomainNodeVisitor.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\TransDefaultDomainNode;
  15  use Symfony\Bridge\Twig\Node\TransNode;
  16  use Twig\Environment;
  17  use Twig\Node\BlockNode;
  18  use Twig\Node\Expression\ArrayExpression;
  19  use Twig\Node\Expression\AssignNameExpression;
  20  use Twig\Node\Expression\ConstantExpression;
  21  use Twig\Node\Expression\FilterExpression;
  22  use Twig\Node\Expression\NameExpression;
  23  use Twig\Node\ModuleNode;
  24  use Twig\Node\Node;
  25  use Twig\Node\SetNode;
  26  use Twig\NodeVisitor\AbstractNodeVisitor;
  27  
  28  /**
  29   * @author Fabien Potencier <fabien@symfony.com>
  30   */
  31  class TranslationDefaultDomainNodeVisitor extends AbstractNodeVisitor
  32  {
  33      private $scope;
  34  
  35      public function __construct()
  36      {
  37          $this->scope = new Scope();
  38      }
  39  
  40      /**
  41       * {@inheritdoc}
  42       */
  43      protected function doEnterNode(Node $node, Environment $env)
  44      {
  45          if ($node instanceof BlockNode || $node instanceof ModuleNode) {
  46              $this->scope = $this->scope->enter();
  47          }
  48  
  49          if ($node instanceof TransDefaultDomainNode) {
  50              if ($node->getNode('expr') instanceof ConstantExpression) {
  51                  $this->scope->set('domain', $node->getNode('expr'));
  52  
  53                  return $node;
  54              } else {
  55                  $var = $this->getVarName();
  56                  $name = new AssignNameExpression($var, $node->getTemplateLine());
  57                  $this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));
  58  
  59                  return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
  60              }
  61          }
  62  
  63          if (!$this->scope->has('domain')) {
  64              return $node;
  65          }
  66  
  67          if ($node instanceof FilterExpression && \in_array($node->getNode('filter')->getAttribute('value'), ['trans', 'transchoice'])) {
  68              $arguments = $node->getNode('arguments');
  69              $ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
  70              if ($this->isNamedArguments($arguments)) {
  71                  if (!$arguments->hasNode('domain') && !$arguments->hasNode($ind)) {
  72                      $arguments->setNode('domain', $this->scope->get('domain'));
  73                  }
  74              } else {
  75                  if (!$arguments->hasNode($ind)) {
  76                      if (!$arguments->hasNode($ind - 1)) {
  77                          $arguments->setNode($ind - 1, new ArrayExpression([], $node->getTemplateLine()));
  78                      }
  79  
  80                      $arguments->setNode($ind, $this->scope->get('domain'));
  81                  }
  82              }
  83          } elseif ($node instanceof TransNode) {
  84              if (!$node->hasNode('domain')) {
  85                  $node->setNode('domain', $this->scope->get('domain'));
  86              }
  87          }
  88  
  89          return $node;
  90      }
  91  
  92      /**
  93       * {@inheritdoc}
  94       */
  95      protected function doLeaveNode(Node $node, Environment $env)
  96      {
  97          if ($node instanceof TransDefaultDomainNode) {
  98              return null;
  99          }
 100  
 101          if ($node instanceof BlockNode || $node instanceof ModuleNode) {
 102              $this->scope = $this->scope->leave();
 103          }
 104  
 105          return $node;
 106      }
 107  
 108      /**
 109       * {@inheritdoc}
 110       *
 111       * @return int
 112       */
 113      public function getPriority()
 114      {
 115          return -10;
 116      }
 117  
 118      /**
 119       * @return bool
 120       */
 121      private function isNamedArguments($arguments)
 122      {
 123          foreach ($arguments as $name => $node) {
 124              if (!\is_int($name)) {
 125                  return true;
 126              }
 127          }
 128  
 129          return false;
 130      }
 131  
 132      private function getVarName()
 133      {
 134          return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
 135      }
 136  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1