DumpTokenParser.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\TokenParser;
  11. use Symfony\Bridge\Twig\Node\DumpNode;
  12. use Twig\Node\Node;
  13. use Twig\Token;
  14. use Twig\TokenParser\AbstractTokenParser;
  15. /**
  16. * Token Parser for the 'dump' tag.
  17. *
  18. * Dump variables with:
  19. *
  20. * {% dump %}
  21. * {% dump foo %}
  22. * {% dump foo, bar %}
  23. *
  24. * @author Julien Galenski <julien.galenski@gmail.com>
  25. */
  26. final class DumpTokenParser extends AbstractTokenParser
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function parse(Token $token): Node
  32. {
  33. $values = null;
  34. if (!$this->parser->getStream()->test(Token::BLOCK_END_TYPE)) {
  35. $values = $this->parser->getExpressionParser()->parseMultitargetExpression();
  36. }
  37. $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  38. return new DumpNode($this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getTag(): string
  44. {
  45. return 'dump';
  46. }
  47. }