[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/TokenParser/ -> TransTokenParser.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\TokenParser;
  13  
  14  use Symfony\Bridge\Twig\Node\TransNode;
  15  use Twig\Error\SyntaxError;
  16  use Twig\Node\Expression\AbstractExpression;
  17  use Twig\Node\Expression\ArrayExpression;
  18  use Twig\Node\TextNode;
  19  use Twig\Token;
  20  use Twig\TokenParser\AbstractTokenParser;
  21  
  22  /**
  23   * Token Parser for the 'trans' tag.
  24   *
  25   * @author Fabien Potencier <fabien@symfony.com>
  26   */
  27  class TransTokenParser extends AbstractTokenParser
  28  {
  29      /**
  30       * {@inheritdoc}
  31       */
  32      public function parse(Token $token)
  33      {
  34          $lineno = $token->getLine();
  35          $stream = $this->parser->getStream();
  36  
  37          $vars = new ArrayExpression([], $lineno);
  38          $domain = null;
  39          $locale = null;
  40          if (!$stream->test(Token::BLOCK_END_TYPE)) {
  41              if ($stream->test('with')) {
  42                  // {% trans with vars %}
  43                  $stream->next();
  44                  $vars = $this->parser->getExpressionParser()->parseExpression();
  45              }
  46  
  47              if ($stream->test('from')) {
  48                  // {% trans from "messages" %}
  49                  $stream->next();
  50                  $domain = $this->parser->getExpressionParser()->parseExpression();
  51              }
  52  
  53              if ($stream->test('into')) {
  54                  // {% trans into "fr" %}
  55                  $stream->next();
  56                  $locale = $this->parser->getExpressionParser()->parseExpression();
  57              } elseif (!$stream->test(Token::BLOCK_END_TYPE)) {
  58                  throw new SyntaxError('Unexpected token. Twig was looking for the "with", "from", or "into" keyword.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
  59              }
  60          }
  61  
  62          // {% trans %}message{% endtrans %}
  63          $stream->expect(Token::BLOCK_END_TYPE);
  64          $body = $this->parser->subparse([$this, 'decideTransFork'], true);
  65  
  66          if (!$body instanceof TextNode && !$body instanceof AbstractExpression) {
  67              throw new SyntaxError('A message inside a trans tag must be a simple text.', $body->getTemplateLine(), $stream->getSourceContext());
  68          }
  69  
  70          $stream->expect(Token::BLOCK_END_TYPE);
  71  
  72          return new TransNode($body, $domain, null, $vars, $locale, $lineno, $this->getTag());
  73      }
  74  
  75      public function decideTransFork($token)
  76      {
  77          return $token->test(['endtrans']);
  78      }
  79  
  80      /**
  81       * {@inheritdoc}
  82       */
  83      public function getTag()
  84      {
  85          return 'trans';
  86      }
  87  }


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