BlockAssets.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Assets;
  9. use Grav\Common\Assets;
  10. use Grav\Common\Config\Config;
  11. use Grav\Common\Grav;
  12. use Grav\Framework\ContentBlock\HtmlBlock;
  13. use function strlen;
  14. /**
  15. * Register block assets into Grav.
  16. */
  17. class BlockAssets
  18. {
  19. /**
  20. * @param HtmlBlock $block
  21. * @return void
  22. */
  23. public static function registerAssets(HtmlBlock $block): void
  24. {
  25. $grav = Grav::instance();
  26. /** @var Assets $assets */
  27. $assets = $grav['assets'];
  28. $types = $block->getAssets();
  29. foreach ($types as $type => $groups) {
  30. switch ($type) {
  31. case 'frameworks':
  32. static::registerFrameworks($assets, $groups);
  33. break;
  34. case 'styles':
  35. static::registerStyles($assets, $groups);
  36. break;
  37. case 'scripts':
  38. static::registerScripts($assets, $groups);
  39. break;
  40. case 'links':
  41. static::registerLinks($assets, $groups);
  42. break;
  43. case 'html':
  44. static::registerHtml($assets, $groups);
  45. break;
  46. }
  47. }
  48. }
  49. /**
  50. * @param Assets $assets
  51. * @param array $list
  52. * @return void
  53. */
  54. protected static function registerFrameworks(Assets $assets, array $list): void
  55. {
  56. if ($list) {
  57. throw new \RuntimeException('Not Implemented');
  58. }
  59. }
  60. /**
  61. * @param Assets $assets
  62. * @param array $groups
  63. * @return void
  64. */
  65. protected static function registerStyles(Assets $assets, array $groups): void
  66. {
  67. $grav = Grav::instance();
  68. /** @var Config $config */
  69. $config = $grav['config'];
  70. foreach ($groups as $group => $styles) {
  71. foreach ($styles as $style) {
  72. switch ($style[':type']) {
  73. case 'file':
  74. $options = [
  75. 'priority' => $style[':priority'],
  76. 'group' => $group,
  77. 'type' => $style['type'],
  78. 'media' => $style['media']
  79. ] + $style['element'];
  80. $assets->addCss(static::getRelativeUrl($style['href'], $config->get('system.assets.css_pipeline')), $options);
  81. break;
  82. case 'inline':
  83. $options = [
  84. 'priority' => $style[':priority'],
  85. 'group' => $group,
  86. 'type' => $style['type'],
  87. ] + $style['element'];
  88. $assets->addInlineCss($style['content'], $options);
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * @param Assets $assets
  96. * @param array $groups
  97. * @return void
  98. */
  99. protected static function registerScripts(Assets $assets, array $groups): void
  100. {
  101. $grav = Grav::instance();
  102. /** @var Config $config */
  103. $config = $grav['config'];
  104. foreach ($groups as $group => $scripts) {
  105. $group = $group === 'footer' ? 'bottom' : $group;
  106. foreach ($scripts as $script) {
  107. switch ($script[':type']) {
  108. case 'file':
  109. $options = [
  110. 'group' => $group,
  111. 'priority' => $script[':priority'],
  112. 'src' => $script['src'],
  113. 'type' => $script['type'],
  114. 'loading' => $script['loading'],
  115. 'defer' => $script['defer'],
  116. 'async' => $script['async'],
  117. 'handle' => $script['handle']
  118. ] + $script['element'];
  119. $assets->addJs(static::getRelativeUrl($script['src'], $config->get('system.assets.js_pipeline')), $options);
  120. break;
  121. case 'inline':
  122. $options = [
  123. 'priority' => $script[':priority'],
  124. 'group' => $group,
  125. 'type' => $script['type'],
  126. 'loading' => $script['loading']
  127. ] + $script['element'];
  128. $assets->addInlineJs($script['content'], $options);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * @param Assets $assets
  136. * @param array $groups
  137. * @return void
  138. */
  139. protected static function registerLinks(Assets $assets, array $groups): void
  140. {
  141. foreach ($groups as $group => $links) {
  142. foreach ($links as $link) {
  143. $href = $link['href'];
  144. $options = [
  145. 'group' => $group,
  146. 'priority' => $link[':priority'],
  147. 'rel' => $link['rel'],
  148. ] + $link['element'];
  149. $assets->addLink($href, $options);
  150. }
  151. }
  152. }
  153. /**
  154. * @param Assets $assets
  155. * @param array $groups
  156. * @return void
  157. */
  158. protected static function registerHtml(Assets $assets, array $groups): void
  159. {
  160. if ($groups) {
  161. throw new \RuntimeException('Not Implemented');
  162. }
  163. }
  164. /**
  165. * @param string $url
  166. * @param bool $pipeline
  167. * @return string
  168. */
  169. protected static function getRelativeUrl($url, $pipeline)
  170. {
  171. $grav = Grav::instance();
  172. $base = rtrim($grav['base_url'], '/') ?: '/';
  173. if (strpos($url, $base) === 0) {
  174. if ($pipeline) {
  175. // Remove file timestamp if CSS pipeline has been enabled.
  176. $url = preg_replace('|[?#].*|', '', $url);
  177. }
  178. return substr($url, strlen($base) - 1);
  179. }
  180. return $url;
  181. }
  182. }