TwigTokenParserScript.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @package Grav.Common.Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\TokenParser;
  9. use Grav\Common\Twig\Node\TwigNodeScript;
  10. /**
  11. * Adds a script to head/bottom/custom location in the document.
  12. *
  13. * {% script 'theme://js/something.js' in 'bottom' priority: 20 with { defer: true, async: true } %}
  14. *
  15. * {% script in 'bottom' priority: 20 %}
  16. * alert('Warning!');
  17. * {% endscript %}
  18. */
  19. class TwigTokenParserScript extends \Twig_TokenParser
  20. {
  21. /**
  22. * Parses a token and returns a node.
  23. *
  24. * @param \Twig_Token $token A Twig_Token instance
  25. *
  26. * @return \Twig_Node A Twig_Node instance
  27. */
  28. public function parse(\Twig_Token $token)
  29. {
  30. $lineno = $token->getLine();
  31. $stream = $this->parser->getStream();
  32. list($file, $group, $priority, $attributes) = $this->parseArguments($token);
  33. $content = null;
  34. if ($file === null) {
  35. $content = $this->parser->subparse([$this, 'decideBlockEnd'], true);
  36. $stream->expect(\Twig_Token::BLOCK_END_TYPE);
  37. }
  38. return new TwigNodeScript($content, $file, $group, $priority, $attributes, $lineno, $this->getTag());
  39. }
  40. /**
  41. * @param \Twig_Token $token
  42. * @return array
  43. */
  44. protected function parseArguments(\Twig_Token $token)
  45. {
  46. $stream = $this->parser->getStream();
  47. $file = null;
  48. if (!$stream->test(\Twig_Token::NAME_TYPE) && !$stream->test(\Twig_Token::OPERATOR_TYPE) && !$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
  49. $file = $this->parser->getExpressionParser()->parseExpression();
  50. }
  51. $group = null;
  52. if ($stream->nextIf(\Twig_Token::OPERATOR_TYPE, 'in')) {
  53. $group = $this->parser->getExpressionParser()->parseExpression();
  54. }
  55. $priority = null;
  56. if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'priority')) {
  57. $stream->expect(\Twig_Token::PUNCTUATION_TYPE, ':');
  58. $priority = $this->parser->getExpressionParser()->parseExpression();
  59. }
  60. $attributes = null;
  61. if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
  62. $attributes = $this->parser->getExpressionParser()->parseExpression();
  63. }
  64. $stream->expect(\Twig_Token::BLOCK_END_TYPE);
  65. return [$file, $group, $priority, $attributes];
  66. }
  67. /**
  68. * @param \Twig_Token $token
  69. * @return bool
  70. */
  71. public function decideBlockEnd(\Twig_Token $token)
  72. {
  73. return $token->test('endscript');
  74. }
  75. /**
  76. * Gets the tag name associated with this token parser.
  77. *
  78. * @return string The tag name
  79. */
  80. public function getTag()
  81. {
  82. return 'script';
  83. }
  84. }