TwigNodeScript.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @package Grav\Common\Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\Node;
  9. use Twig\Compiler;
  10. use Twig\Node\Expression\AbstractExpression;
  11. use Twig\Node\Node;
  12. use Twig\Node\NodeCaptureInterface;
  13. class TwigNodeScript extends Node implements NodeCaptureInterface
  14. {
  15. protected $tagName = 'script';
  16. /**
  17. * TwigNodeScript constructor.
  18. * @param Node|null $body
  19. * @param AbstractExpression|null $file
  20. * @param AbstractExpression|null $group
  21. * @param AbstractExpression|null $priority
  22. * @param AbstractExpression|null $attributes
  23. * @param int $lineno
  24. * @param string|null $tag
  25. */
  26. public function __construct(
  27. Node $body = null,
  28. AbstractExpression $file = null,
  29. AbstractExpression $group = null,
  30. AbstractExpression $priority = null,
  31. AbstractExpression $attributes = null,
  32. $lineno = 0,
  33. $tag = null
  34. )
  35. {
  36. parent::__construct(['body' => $body, 'file' => $file, 'group' => $group, 'priority' => $priority, 'attributes' => $attributes], [], $lineno, $tag);
  37. }
  38. /**
  39. * Compiles the node to PHP.
  40. *
  41. * @param Compiler $compiler A Twig_Compiler instance
  42. * @throws \LogicException
  43. */
  44. public function compile(Compiler $compiler)
  45. {
  46. $compiler->addDebugInfo($this);
  47. $compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];\n");
  48. if ($this->getNode('attributes') !== null) {
  49. $compiler
  50. ->write('$attributes = ')
  51. ->subcompile($this->getNode('attributes'))
  52. ->raw(";\n")
  53. ->write("if (!is_array(\$attributes)) {\n")
  54. ->indent()
  55. ->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\n")
  56. ->outdent()
  57. ->write("}\n");
  58. } else {
  59. $compiler->write('$attributes = [];' . "\n");
  60. }
  61. if ($this->getNode('group') !== null) {
  62. $compiler
  63. ->write("\$attributes['group'] = ")
  64. ->subcompile($this->getNode('group'))
  65. ->raw(";\n")
  66. ->write("if (!is_string(\$attributes['group'])) {\n")
  67. ->indent()
  68. ->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\n")
  69. ->outdent()
  70. ->write("}\n");
  71. }
  72. if ($this->getNode('priority') !== null) {
  73. $compiler
  74. ->write("\$attributes['priority'] = (int)(")
  75. ->subcompile($this->getNode('priority'))
  76. ->raw(");\n");
  77. }
  78. if ($this->getNode('file') !== null) {
  79. $compiler
  80. ->write('$assets->addJs(')
  81. ->subcompile($this->getNode('file'))
  82. ->raw(", \$attributes);\n");
  83. } else {
  84. $compiler
  85. ->write("ob_start();\n")
  86. ->subcompile($this->getNode('body'))
  87. ->write('$content = ob_get_clean();' . "\n")
  88. ->write("\$assets->addInlineJs(\$content, \$attributes);\n");
  89. }
  90. }
  91. }