TwigNodeScript.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Node;
  9. class TwigNodeScript extends \Twig_Node implements \Twig_NodeOutputInterface
  10. {
  11. protected $tagName = 'script';
  12. /**
  13. * TwigNodeScript constructor.
  14. * @param \Twig_Node|null $body
  15. * @param \Twig_Node_Expression|null $file
  16. * @param \Twig_Node_Expression|null $group
  17. * @param \Twig_Node_Expression|null $priority
  18. * @param \Twig_Node_Expression|null $attributes
  19. * @param int $lineno
  20. * @param string|null $tag
  21. */
  22. public function __construct(
  23. \Twig_Node $body = null,
  24. \Twig_Node_Expression $file = null,
  25. \Twig_Node_Expression $group = null,
  26. \Twig_Node_Expression $priority = null,
  27. \Twig_Node_Expression $attributes = null,
  28. $lineno = 0,
  29. $tag = null
  30. )
  31. {
  32. parent::__construct(['body' => $body, 'file' => $file, 'group' => $group, 'priority' => $priority, 'attributes' => $attributes], [], $lineno, $tag);
  33. }
  34. /**
  35. * Compiles the node to PHP.
  36. *
  37. * @param \Twig_Compiler $compiler A Twig_Compiler instance
  38. * @throws \LogicException
  39. */
  40. public function compile(\Twig_Compiler $compiler)
  41. {
  42. $compiler->addDebugInfo($this);
  43. if ($this->getNode('attributes') !== null) {
  44. $compiler
  45. ->write('$attributes = ')
  46. ->subcompile($this->getNode('attributes'))
  47. ->raw(";\n")
  48. ->write("if (\$attributes !== null && !is_array(\$attributes)) {\n")
  49. ->indent()
  50. ->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\n")
  51. ->outdent()
  52. ->write("}\n");
  53. } else {
  54. $compiler->write('$attributes = [];' . "\n");
  55. }
  56. if ($this->getNode('group') !== null) {
  57. $compiler
  58. ->write('$group = ')
  59. ->subcompile($this->getNode('group'))
  60. ->raw(";\n")
  61. ->write("if (\$group !== null && !is_string(\$group)) {\n")
  62. ->indent()
  63. ->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\n")
  64. ->outdent()
  65. ->write("}\n");
  66. } else {
  67. $compiler->write('$group = null;' . "\n");
  68. }
  69. if ($this->getNode('priority') !== null) {
  70. $compiler
  71. ->write('$priority = (int)(')
  72. ->subcompile($this->getNode('priority'))
  73. ->raw(");\n");
  74. } else {
  75. $compiler->write('$priority = null;' . "\n");
  76. }
  77. $compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];\n");
  78. if ($this->getNode('file') !== null) {
  79. $compiler
  80. ->write('$file = ')
  81. ->subcompile($this->getNode('file'))
  82. ->write(";\n")
  83. ->write("\$pipeline = !empty(\$attributes['pipeline']);\n")
  84. ->write("\$loading = !empty(\$attributes['defer']) ? 'defer' : (!empty(\$attributes['async']) ? 'async' : null);\n")
  85. ->write("\$assets->addJs(\$file, \$priority, \$pipeline, \$loading, \$group);\n");
  86. } else {
  87. $compiler
  88. ->write("ob_start();\n")
  89. ->subcompile($this->getNode('body'))
  90. ->write("\$content = ob_get_clean();")
  91. ->write("\$assets->addInlineJs(\$content, \$priority, \$group, \$attributes);\n");
  92. }
  93. }
  94. }