[ Index ]

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


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