vendor/symfony/twig-bridge/NodeVisitor/TranslationDefaultDomainNodeVisitor.php line 37

  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\TransDefaultDomainNode;
  12. use Symfony\Bridge\Twig\Node\TransNode;
  13. use Twig\Environment;
  14. use Twig\Node\BlockNode;
  15. use Twig\Node\Expression\ArrayExpression;
  16. use Twig\Node\Expression\AssignNameExpression;
  17. use Twig\Node\Expression\ConstantExpression;
  18. use Twig\Node\Expression\FilterExpression;
  19. use Twig\Node\Expression\NameExpression;
  20. use Twig\Node\ModuleNode;
  21. use Twig\Node\Node;
  22. use Twig\Node\SetNode;
  23. use Twig\NodeVisitor\AbstractNodeVisitor;
  24. /**
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  */
  27. final class TranslationDefaultDomainNodeVisitor extends AbstractNodeVisitor
  28. {
  29.     private Scope $scope;
  30.     public function __construct()
  31.     {
  32.         $this->scope = new Scope();
  33.     }
  34.     protected function doEnterNode(Node $nodeEnvironment $env): Node
  35.     {
  36.         if ($node instanceof BlockNode || $node instanceof ModuleNode) {
  37.             $this->scope $this->scope->enter();
  38.         }
  39.         if ($node instanceof TransDefaultDomainNode) {
  40.             if ($node->getNode('expr') instanceof ConstantExpression) {
  41.                 $this->scope->set('domain'$node->getNode('expr'));
  42.                 return $node;
  43.             } else {
  44.                 $var $this->getVarName();
  45.                 $name = new AssignNameExpression($var$node->getTemplateLine());
  46.                 $this->scope->set('domain', new NameExpression($var$node->getTemplateLine()));
  47.                 return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
  48.             }
  49.         }
  50.         if (!$this->scope->has('domain')) {
  51.             return $node;
  52.         }
  53.         if ($node instanceof FilterExpression && 'trans' === $node->getNode('filter')->getAttribute('value')) {
  54.             $arguments $node->getNode('arguments');
  55.             if ($this->isNamedArguments($arguments)) {
  56.                 if (!$arguments->hasNode('domain') && !$arguments->hasNode(1)) {
  57.                     $arguments->setNode('domain'$this->scope->get('domain'));
  58.                 }
  59.             } elseif (!$arguments->hasNode(1)) {
  60.                 if (!$arguments->hasNode(0)) {
  61.                     $arguments->setNode(0, new ArrayExpression([], $node->getTemplateLine()));
  62.                 }
  63.                 $arguments->setNode(1$this->scope->get('domain'));
  64.             }
  65.         } elseif ($node instanceof TransNode) {
  66.             if (!$node->hasNode('domain')) {
  67.                 $node->setNode('domain'$this->scope->get('domain'));
  68.             }
  69.         }
  70.         return $node;
  71.     }
  72.     protected function doLeaveNode(Node $nodeEnvironment $env): ?Node
  73.     {
  74.         if ($node instanceof TransDefaultDomainNode) {
  75.             return null;
  76.         }
  77.         if ($node instanceof BlockNode || $node instanceof ModuleNode) {
  78.             $this->scope $this->scope->leave();
  79.         }
  80.         return $node;
  81.     }
  82.     public function getPriority(): int
  83.     {
  84.         return -10;
  85.     }
  86.     private function isNamedArguments(Node $arguments): bool
  87.     {
  88.         foreach ($arguments as $name => $node) {
  89.             if (!\is_int($name)) {
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95.     private function getVarName(): string
  96.     {
  97.         return sprintf('__internal_%s'hash('sha256'uniqid(mt_rand(), true), false));
  98.     }
  99. }