[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Node/ -> TransNode.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\Node;
  13  
  14  use Twig\Compiler;
  15  use Twig\Node\Expression\AbstractExpression;
  16  use Twig\Node\Expression\ArrayExpression;
  17  use Twig\Node\Expression\ConstantExpression;
  18  use Twig\Node\Expression\NameExpression;
  19  use Twig\Node\Node;
  20  use Twig\Node\TextNode;
  21  
  22  // BC/FC with namespaced Twig
  23  class_exists('Twig\Node\Expression\ArrayExpression');
  24  
  25  /**
  26   * @author Fabien Potencier <fabien@symfony.com>
  27   */
  28  class TransNode extends Node
  29  {
  30      public function __construct(Node $body, Node $domain = null, AbstractExpression $count = null, AbstractExpression $vars = null, AbstractExpression $locale = null, $lineno = 0, $tag = null)
  31      {
  32          $nodes = ['body' => $body];
  33          if (null !== $domain) {
  34              $nodes['domain'] = $domain;
  35          }
  36          if (null !== $count) {
  37              $nodes['count'] = $count;
  38          }
  39          if (null !== $vars) {
  40              $nodes['vars'] = $vars;
  41          }
  42          if (null !== $locale) {
  43              $nodes['locale'] = $locale;
  44          }
  45  
  46          parent::__construct($nodes, [], $lineno, $tag);
  47      }
  48  
  49      public function compile(Compiler $compiler)
  50      {
  51          $compiler->addDebugInfo($this);
  52  
  53          $defaults = new ArrayExpression([], -1);
  54          if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof ArrayExpression) {
  55              $defaults = $this->getNode('vars');
  56              $vars = null;
  57          }
  58          list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
  59  
  60          $method = !$this->hasNode('count') ? 'trans' : 'transChoice';
  61  
  62          $compiler
  63              ->write('echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->getTranslator()->'.$method.'(')
  64              ->subcompile($msg)
  65          ;
  66  
  67          $compiler->raw(', ');
  68  
  69          if ($this->hasNode('count')) {
  70              $compiler
  71                  ->subcompile($this->getNode('count'))
  72                  ->raw(', ')
  73              ;
  74          }
  75  
  76          if (null !== $vars) {
  77              $compiler
  78                  ->raw('array_merge(')
  79                  ->subcompile($defaults)
  80                  ->raw(', ')
  81                  ->subcompile($this->getNode('vars'))
  82                  ->raw(')')
  83              ;
  84          } else {
  85              $compiler->subcompile($defaults);
  86          }
  87  
  88          $compiler->raw(', ');
  89  
  90          if (!$this->hasNode('domain')) {
  91              $compiler->repr('messages');
  92          } else {
  93              $compiler->subcompile($this->getNode('domain'));
  94          }
  95  
  96          if ($this->hasNode('locale')) {
  97              $compiler
  98                  ->raw(', ')
  99                  ->subcompile($this->getNode('locale'))
 100              ;
 101          }
 102          $compiler->raw(");\n");
 103      }
 104  
 105      protected function compileString(Node $body, ArrayExpression $vars, $ignoreStrictCheck = false)
 106      {
 107          if ($body instanceof ConstantExpression) {
 108              $msg = $body->getAttribute('value');
 109          } elseif ($body instanceof TextNode) {
 110              $msg = $body->getAttribute('data');
 111          } else {
 112              return [$body, $vars];
 113          }
 114  
 115          preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
 116  
 117          foreach ($matches[1] as $var) {
 118              $key = new ConstantExpression('%'.$var.'%', $body->getTemplateLine());
 119              if (!$vars->hasElement($key)) {
 120                  if ('count' === $var && $this->hasNode('count')) {
 121                      $vars->addElement($this->getNode('count'), $key);
 122                  } else {
 123                      $varExpr = new NameExpression($var, $body->getTemplateLine());
 124                      $varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
 125                      $vars->addElement($varExpr, $key);
 126                  }
 127              }
 128          }
 129  
 130          return [new ConstantExpression(str_replace('%%', '%', trim($msg)), $body->getTemplateLine()), $vars];
 131      }
 132  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1