HtmlBlock.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * @package Grav\Framework\ContentBlock
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\ContentBlock;
  9. use RuntimeException;
  10. use function is_array;
  11. use function is_string;
  12. /**
  13. * HtmlBlock
  14. *
  15. * @package Grav\Framework\ContentBlock
  16. */
  17. class HtmlBlock extends ContentBlock implements HtmlBlockInterface
  18. {
  19. /** @var int */
  20. protected $version = 1;
  21. /** @var array */
  22. protected $frameworks = [];
  23. /** @var array */
  24. protected $styles = [];
  25. /** @var array */
  26. protected $scripts = [];
  27. /** @var array */
  28. protected $links = [];
  29. /** @var array */
  30. protected $html = [];
  31. /**
  32. * @return array
  33. */
  34. public function getAssets()
  35. {
  36. $assets = $this->getAssetsFast();
  37. $this->sortAssets($assets['styles']);
  38. $this->sortAssets($assets['scripts']);
  39. $this->sortAssets($assets['links']);
  40. $this->sortAssets($assets['html']);
  41. return $assets;
  42. }
  43. /**
  44. * @return array
  45. */
  46. public function getFrameworks()
  47. {
  48. $assets = $this->getAssetsFast();
  49. return array_keys($assets['frameworks']);
  50. }
  51. /**
  52. * @param string $location
  53. * @return array
  54. */
  55. public function getStyles($location = 'head')
  56. {
  57. return $this->getAssetsInLocation('styles', $location);
  58. }
  59. /**
  60. * @param string $location
  61. * @return array
  62. */
  63. public function getScripts($location = 'head')
  64. {
  65. return $this->getAssetsInLocation('scripts', $location);
  66. }
  67. /**
  68. * @param string $location
  69. * @return array
  70. */
  71. public function getLinks($location = 'head')
  72. {
  73. return $this->getAssetsInLocation('links', $location);
  74. }
  75. /**
  76. * @param string $location
  77. * @return array
  78. */
  79. public function getHtml($location = 'bottom')
  80. {
  81. return $this->getAssetsInLocation('html', $location);
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function toArray()
  87. {
  88. $array = parent::toArray();
  89. if ($this->frameworks) {
  90. $array['frameworks'] = $this->frameworks;
  91. }
  92. if ($this->styles) {
  93. $array['styles'] = $this->styles;
  94. }
  95. if ($this->scripts) {
  96. $array['scripts'] = $this->scripts;
  97. }
  98. if ($this->links) {
  99. $array['links'] = $this->links;
  100. }
  101. if ($this->html) {
  102. $array['html'] = $this->html;
  103. }
  104. return $array;
  105. }
  106. /**
  107. * @param array $serialized
  108. * @return void
  109. * @throws RuntimeException
  110. */
  111. public function build(array $serialized)
  112. {
  113. parent::build($serialized);
  114. $this->frameworks = isset($serialized['frameworks']) ? (array) $serialized['frameworks'] : [];
  115. $this->styles = isset($serialized['styles']) ? (array) $serialized['styles'] : [];
  116. $this->scripts = isset($serialized['scripts']) ? (array) $serialized['scripts'] : [];
  117. $this->links = isset($serialized['links']) ? (array) $serialized['links'] : [];
  118. $this->html = isset($serialized['html']) ? (array) $serialized['html'] : [];
  119. }
  120. /**
  121. * @param string $framework
  122. * @return $this
  123. */
  124. public function addFramework($framework)
  125. {
  126. $this->frameworks[$framework] = 1;
  127. return $this;
  128. }
  129. /**
  130. * @param string|array $element
  131. * @param int $priority
  132. * @param string $location
  133. * @return bool
  134. *
  135. * @example $block->addStyle('assets/js/my.js');
  136. * @example $block->addStyle(['href' => 'assets/js/my.js', 'media' => 'screen']);
  137. */
  138. public function addStyle($element, $priority = 0, $location = 'head')
  139. {
  140. if (!is_array($element)) {
  141. $element = ['href' => (string) $element];
  142. }
  143. if (empty($element['href'])) {
  144. return false;
  145. }
  146. if (!isset($this->styles[$location])) {
  147. $this->styles[$location] = [];
  148. }
  149. $id = !empty($element['id']) ? ['id' => (string) $element['id']] : [];
  150. $href = $element['href'];
  151. $type = !empty($element['type']) ? (string) $element['type'] : 'text/css';
  152. $media = !empty($element['media']) ? (string) $element['media'] : null;
  153. unset(
  154. $element['tag'],
  155. $element['id'],
  156. $element['rel'],
  157. $element['content'],
  158. $element['href'],
  159. $element['type'],
  160. $element['media']
  161. );
  162. $this->styles[$location][md5($href) . sha1($href)] = [
  163. ':type' => 'file',
  164. ':priority' => (int) $priority,
  165. 'href' => $href,
  166. 'type' => $type,
  167. 'media' => $media,
  168. 'element' => $element
  169. ] + $id;
  170. return true;
  171. }
  172. /**
  173. * @param string|array $element
  174. * @param int $priority
  175. * @param string $location
  176. * @return bool
  177. */
  178. public function addInlineStyle($element, $priority = 0, $location = 'head')
  179. {
  180. if (!is_array($element)) {
  181. $element = ['content' => (string) $element];
  182. }
  183. if (empty($element['content'])) {
  184. return false;
  185. }
  186. if (!isset($this->styles[$location])) {
  187. $this->styles[$location] = [];
  188. }
  189. $content = (string) $element['content'];
  190. $type = !empty($element['type']) ? (string) $element['type'] : 'text/css';
  191. unset($element['content'], $element['type']);
  192. $this->styles[$location][md5($content) . sha1($content)] = [
  193. ':type' => 'inline',
  194. ':priority' => (int) $priority,
  195. 'content' => $content,
  196. 'type' => $type,
  197. 'element' => $element
  198. ];
  199. return true;
  200. }
  201. /**
  202. * @param string|array $element
  203. * @param int $priority
  204. * @param string $location
  205. * @return bool
  206. */
  207. public function addScript($element, $priority = 0, $location = 'head')
  208. {
  209. if (!is_array($element)) {
  210. $element = ['src' => (string) $element];
  211. }
  212. if (empty($element['src'])) {
  213. return false;
  214. }
  215. if (!isset($this->scripts[$location])) {
  216. $this->scripts[$location] = [];
  217. }
  218. $src = $element['src'];
  219. $type = !empty($element['type']) ? (string) $element['type'] : 'text/javascript';
  220. $loading = !empty($element['loading']) ? (string) $element['loading'] : null;
  221. $defer = !empty($element['defer']);
  222. $async = !empty($element['async']);
  223. $handle = !empty($element['handle']) ? (string) $element['handle'] : '';
  224. unset($element['src'], $element['type'], $element['loading'], $element['defer'], $element['async'], $element['handle']);
  225. $this->scripts[$location][md5($src) . sha1($src)] = [
  226. ':type' => 'file',
  227. ':priority' => (int) $priority,
  228. 'src' => $src,
  229. 'type' => $type,
  230. 'loading' => $loading,
  231. 'defer' => $defer,
  232. 'async' => $async,
  233. 'handle' => $handle,
  234. 'element' => $element
  235. ];
  236. return true;
  237. }
  238. /**
  239. * @param string|array $element
  240. * @param int $priority
  241. * @param string $location
  242. * @return bool
  243. */
  244. public function addInlineScript($element, $priority = 0, $location = 'head')
  245. {
  246. if (!is_array($element)) {
  247. $element = ['content' => (string) $element];
  248. }
  249. if (empty($element['content'])) {
  250. return false;
  251. }
  252. if (!isset($this->scripts[$location])) {
  253. $this->scripts[$location] = [];
  254. }
  255. $content = (string) $element['content'];
  256. $type = !empty($element['type']) ? (string) $element['type'] : 'text/javascript';
  257. $loading = !empty($element['loading']) ? (string) $element['loading'] : null;
  258. unset($element['content'], $element['type'], $element['loading']);
  259. $this->scripts[$location][md5($content) . sha1($content)] = [
  260. ':type' => 'inline',
  261. ':priority' => (int) $priority,
  262. 'content' => $content,
  263. 'type' => $type,
  264. 'loading' => $loading,
  265. 'element' => $element
  266. ];
  267. return true;
  268. }
  269. /**
  270. * @param string|array $element
  271. * @param int $priority
  272. * @param string $location
  273. * @return bool
  274. */
  275. public function addModule($element, $priority = 0, $location = 'head')
  276. {
  277. if (!is_array($element)) {
  278. $element = ['src' => (string) $element];
  279. }
  280. $element['type'] = 'module';
  281. return $this->addScript($element, $priority, $location);
  282. }
  283. /**
  284. * @param string|array $element
  285. * @param int $priority
  286. * @param string $location
  287. * @return bool
  288. */
  289. public function addInlineModule($element, $priority = 0, $location = 'head')
  290. {
  291. if (!is_array($element)) {
  292. $element = ['content' => (string) $element];
  293. }
  294. $element['type'] = 'module';
  295. return $this->addInlineScript($element, $priority, $location);
  296. }
  297. /**
  298. * @param array $element
  299. * @param int $priority
  300. * @param string $location
  301. * @return bool
  302. */
  303. public function addLink($element, $priority = 0, $location = 'head')
  304. {
  305. if (!is_array($element) || empty($element['rel']) || empty($element['href'])) {
  306. return false;
  307. }
  308. if (!isset($this->links[$location])) {
  309. $this->links[$location] = [];
  310. }
  311. $rel = (string) $element['rel'];
  312. $href = (string) $element['href'];
  313. unset($element['rel'], $element['href']);
  314. $this->links[$location][md5($href) . sha1($href)] = [
  315. ':type' => 'file',
  316. ':priority' => (int) $priority,
  317. 'href' => $href,
  318. 'rel' => $rel,
  319. 'element' => $element,
  320. ];
  321. return true;
  322. }
  323. /**
  324. * @param string $html
  325. * @param int $priority
  326. * @param string $location
  327. * @return bool
  328. */
  329. public function addHtml($html, $priority = 0, $location = 'bottom')
  330. {
  331. if (empty($html) || !is_string($html)) {
  332. return false;
  333. }
  334. if (!isset($this->html[$location])) {
  335. $this->html[$location] = [];
  336. }
  337. $this->html[$location][md5($html) . sha1($html)] = [
  338. ':priority' => (int) $priority,
  339. 'html' => $html
  340. ];
  341. return true;
  342. }
  343. /**
  344. * @return array
  345. */
  346. protected function getAssetsFast()
  347. {
  348. $assets = [
  349. 'frameworks' => $this->frameworks,
  350. 'styles' => $this->styles,
  351. 'scripts' => $this->scripts,
  352. 'links' => $this->links,
  353. 'html' => $this->html
  354. ];
  355. foreach ($this->blocks as $block) {
  356. if ($block instanceof self) {
  357. $blockAssets = $block->getAssetsFast();
  358. $assets['frameworks'] += $blockAssets['frameworks'];
  359. foreach ($blockAssets['styles'] as $location => $styles) {
  360. if (!isset($assets['styles'][$location])) {
  361. $assets['styles'][$location] = $styles;
  362. } elseif ($styles) {
  363. $assets['styles'][$location] += $styles;
  364. }
  365. }
  366. foreach ($blockAssets['scripts'] as $location => $scripts) {
  367. if (!isset($assets['scripts'][$location])) {
  368. $assets['scripts'][$location] = $scripts;
  369. } elseif ($scripts) {
  370. $assets['scripts'][$location] += $scripts;
  371. }
  372. }
  373. foreach ($blockAssets['links'] as $location => $links) {
  374. if (!isset($assets['links'][$location])) {
  375. $assets['links'][$location] = $links;
  376. } elseif ($links) {
  377. $assets['links'][$location] += $links;
  378. }
  379. }
  380. foreach ($blockAssets['html'] as $location => $htmls) {
  381. if (!isset($assets['html'][$location])) {
  382. $assets['html'][$location] = $htmls;
  383. } elseif ($htmls) {
  384. $assets['html'][$location] += $htmls;
  385. }
  386. }
  387. }
  388. }
  389. return $assets;
  390. }
  391. /**
  392. * @param string $type
  393. * @param string $location
  394. * @return array
  395. */
  396. protected function getAssetsInLocation($type, $location)
  397. {
  398. $assets = $this->getAssetsFast();
  399. if (empty($assets[$type][$location])) {
  400. return [];
  401. }
  402. $styles = $assets[$type][$location];
  403. $this->sortAssetsInLocation($styles);
  404. return $styles;
  405. }
  406. /**
  407. * @param array $items
  408. * @return void
  409. */
  410. protected function sortAssetsInLocation(array &$items)
  411. {
  412. $count = 0;
  413. foreach ($items as &$item) {
  414. $item[':order'] = ++$count;
  415. }
  416. unset($item);
  417. uasort(
  418. $items,
  419. static function ($a, $b) {
  420. return $a[':priority'] <=> $b[':priority'] ?: $a[':order'] <=> $b[':order'];
  421. }
  422. );
  423. }
  424. /**
  425. * @param array $array
  426. * @return void
  427. */
  428. protected function sortAssets(array &$array)
  429. {
  430. foreach ($array as &$items) {
  431. $this->sortAssetsInLocation($items);
  432. }
  433. }
  434. }