vendor/twig/twig/src/TokenParser/ApplyTokenParser.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  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 Twig\TokenParser;
  11. use Twig\ExpressionParser\Infix\FilterExpressionParser;
  12. use Twig\Node\Expression\Variable\LocalVariable;
  13. use Twig\Node\Node;
  14. use Twig\Node\Nodes;
  15. use Twig\Node\PrintNode;
  16. use Twig\Node\SetNode;
  17. use Twig\Token;
  18. /**
  19. * Applies filters on a section of a template.
  20. *
  21. * {% apply upper %}
  22. * This text becomes uppercase
  23. * {% endapply %}
  24. *
  25. * @internal
  26. */
  27. final class ApplyTokenParser extends AbstractTokenParser
  28. {
  29. public function parse(Token $token): Node
  30. {
  31. $lineno = $token->getLine();
  32. $ref = new LocalVariable(null, $lineno);
  33. $filter = $ref;
  34. $op = $this->parser->getEnvironment()->getExpressionParsers()->getByClass(FilterExpressionParser::class);
  35. while (true) {
  36. $filter = $op->parse($this->parser, $filter, $this->parser->getCurrentToken());
  37. if (!$this->parser->getStream()->test(Token::OPERATOR_TYPE, '|')) {
  38. break;
  39. }
  40. $this->parser->getStream()->next();
  41. }
  42. $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  43. $body = $this->parser->subparse([$this, 'decideApplyEnd'], true);
  44. $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  45. return new Nodes([
  46. new SetNode(true, $ref, $body, $lineno),
  47. new PrintNode($filter, $lineno),
  48. ], $lineno);
  49. }
  50. public function decideApplyEnd(Token $token): bool
  51. {
  52. return $token->test('endapply');
  53. }
  54. public function getTag(): string
  55. {
  56. return 'apply';
  57. }
  58. }