[ Index ]

PHP Cross Reference of phpBB-3.2.11-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(array($name)), new Node(array($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'), array('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(array(), $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 false;
  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      public function getPriority()
 112      {
 113          return -10;
 114      }
 115  
 116      /**
 117       * @return bool
 118       */
 119      private function isNamedArguments($arguments)
 120      {
 121          foreach ($arguments as $name => $node) {
 122              if (!\is_int($name)) {
 123                  return true;
 124              }
 125          }
 126  
 127          return false;
 128      }
 129  
 130      private function getVarName()
 131      {
 132          return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
 133      }
 134  }


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