vendor/twig/twig/src/TokenParser/EmbedTokenParser.php line 58

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\Node\EmbedNode;
  12. use Twig\Node\Expression\ConstantExpression;
  13. use Twig\Node\Expression\NameExpression;
  14. use Twig\Token;
  15. /**
  16. * Embeds a template.
  17. */
  18. final class EmbedTokenParser extends IncludeTokenParser
  19. {
  20. public function parse(Token $token)
  21. {
  22. $stream = $this->parser->getStream();
  23. $parent = $this->parser->getExpressionParser()->parseExpression();
  24. list($variables, $only, $ignoreMissing) = $this->parseArguments();
  25. $parentToken = $fakeParentToken = new Token(/* Token::STRING_TYPE */ 7, '__parent__', $token->getLine());
  26. if ($parent instanceof ConstantExpression) {
  27. $parentToken = new Token(/* Token::STRING_TYPE */ 7, $parent->getAttribute('value'), $token->getLine());
  28. } elseif ($parent instanceof NameExpression) {
  29. $parentToken = new Token(/* Token::NAME_TYPE */ 5, $parent->getAttribute('name'), $token->getLine());
  30. }
  31. // inject a fake parent to make the parent() function work
  32. $stream->injectTokens([
  33. new Token(/* Token::BLOCK_START_TYPE */ 1, '', $token->getLine()),
  34. new Token(/* Token::NAME_TYPE */ 5, 'extends', $token->getLine()),
  35. $parentToken,
  36. new Token(/* Token::BLOCK_END_TYPE */ 3, '', $token->getLine()),
  37. ]);
  38. $module = $this->parser->parse($stream, [$this, 'decideBlockEnd'], true);
  39. // override the parent with the correct one
  40. if ($fakeParentToken === $parentToken) {
  41. $module->setNode('parent', $parent);
  42. }
  43. $this->parser->embedTemplate($module);
  44. $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
  45. return new EmbedNode($module->getTemplateName(), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
  46. }
  47. public function decideBlockEnd(Token $token)
  48. {
  49. return $token->test('endembed');
  50. }
  51. public function getTag()
  52. {
  53. return 'embed';
  54. }
  55. }
  56. class_alias('Twig\TokenParser\EmbedTokenParser', 'Twig_TokenParser_Embed');