FormThemeTokenParser.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\FormThemeNode;
  12. use Twig\Node\Expression\ArrayExpression;
  13. use Twig\Node\Node;
  14. use Twig\Token;
  15. use Twig\TokenParser\AbstractTokenParser;
  16. /**
  17. * Token Parser for the 'form_theme' tag.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. final class FormThemeTokenParser extends AbstractTokenParser
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function parse(Token $token): Node
  27. {
  28. $lineno = $token->getLine();
  29. $stream = $this->parser->getStream();
  30. $form = $this->parser->getExpressionParser()->parseExpression();
  31. $only = false;
  32. if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
  33. $this->parser->getStream()->next();
  34. $resources = $this->parser->getExpressionParser()->parseExpression();
  35. if ($this->parser->getStream()->nextIf(Token::NAME_TYPE, 'only')) {
  36. $only = true;
  37. }
  38. } else {
  39. $resources = new ArrayExpression([], $stream->getCurrent()->getLine());
  40. do {
  41. $resources->addElement($this->parser->getExpressionParser()->parseExpression());
  42. } while (!$stream->test(Token::BLOCK_END_TYPE));
  43. }
  44. $stream->expect(Token::BLOCK_END_TYPE);
  45. return new FormThemeNode($form, $resources, $lineno, $this->getTag(), $only);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getTag(): string
  51. {
  52. return 'form_theme';
  53. }
  54. }