FormThemeTokenParser.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class FormThemeTokenParser extends AbstractTokenParser
  22. {
  23. /**
  24. * Parses a token and returns a node.
  25. *
  26. * @param Token $token
  27. *
  28. * @return Node
  29. */
  30. public function parse(Token $token)
  31. {
  32. $lineno = $token->getLine();
  33. $stream = $this->parser->getStream();
  34. $form = $this->parser->getExpressionParser()->parseExpression();
  35. if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
  36. $this->parser->getStream()->next();
  37. $resources = $this->parser->getExpressionParser()->parseExpression();
  38. } else {
  39. $resources = new ArrayExpression(array(), $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());
  46. }
  47. /**
  48. * Gets the tag name associated with this token parser.
  49. *
  50. * @return string The tag name
  51. */
  52. public function getTag()
  53. {
  54. return 'form_theme';
  55. }
  56. }