HtmlBlock.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * @package Grav\Framework\ContentBlock
  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\Framework\ContentBlock;
  9. /**
  10. * HtmlBlock
  11. *
  12. * @package Grav\Framework\ContentBlock
  13. */
  14. class HtmlBlock extends ContentBlock implements HtmlBlockInterface
  15. {
  16. protected $version = 1;
  17. protected $frameworks = [];
  18. protected $styles = [];
  19. protected $scripts = [];
  20. protected $html = [];
  21. /**
  22. * @return array
  23. */
  24. public function getAssets()
  25. {
  26. $assets = $this->getAssetsFast();
  27. $this->sortAssets($assets['styles']);
  28. $this->sortAssets($assets['scripts']);
  29. $this->sortAssets($assets['html']);
  30. return $assets;
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function getFrameworks()
  36. {
  37. $assets = $this->getAssetsFast();
  38. return array_keys($assets['frameworks']);
  39. }
  40. /**
  41. * @param string $location
  42. * @return array
  43. */
  44. public function getStyles($location = 'head')
  45. {
  46. return $this->getAssetsInLocation('styles', $location);
  47. }
  48. /**
  49. * @param string $location
  50. * @return array
  51. */
  52. public function getScripts($location = 'head')
  53. {
  54. return $this->getAssetsInLocation('scripts', $location);
  55. }
  56. /**
  57. * @param string $location
  58. * @return array
  59. */
  60. public function getHtml($location = 'bottom')
  61. {
  62. return $this->getAssetsInLocation('html', $location);
  63. }
  64. /**
  65. * @return array[]
  66. */
  67. public function toArray()
  68. {
  69. $array = parent::toArray();
  70. if ($this->frameworks) {
  71. $array['frameworks'] = $this->frameworks;
  72. }
  73. if ($this->styles) {
  74. $array['styles'] = $this->styles;
  75. }
  76. if ($this->scripts) {
  77. $array['scripts'] = $this->scripts;
  78. }
  79. if ($this->html) {
  80. $array['html'] = $this->html;
  81. }
  82. return $array;
  83. }
  84. /**
  85. * @param array $serialized
  86. * @throws \RuntimeException
  87. */
  88. public function build(array $serialized)
  89. {
  90. parent::build($serialized);
  91. $this->frameworks = isset($serialized['frameworks']) ? (array) $serialized['frameworks'] : [];
  92. $this->styles = isset($serialized['styles']) ? (array) $serialized['styles'] : [];
  93. $this->scripts = isset($serialized['scripts']) ? (array) $serialized['scripts'] : [];
  94. $this->html = isset($serialized['html']) ? (array) $serialized['html'] : [];
  95. }
  96. /**
  97. * @param string $framework
  98. * @return $this
  99. */
  100. public function addFramework($framework)
  101. {
  102. $this->frameworks[$framework] = 1;
  103. return $this;
  104. }
  105. /**
  106. * @param string|array $element
  107. * @param int $priority
  108. * @param string $location
  109. * @return bool
  110. *
  111. * @example $block->addStyle('assets/js/my.js');
  112. * @example $block->addStyle(['href' => 'assets/js/my.js', 'media' => 'screen']);
  113. */
  114. public function addStyle($element, $priority = 0, $location = 'head')
  115. {
  116. if (!is_array($element)) {
  117. $element = ['href' => (string) $element];
  118. }
  119. if (empty($element['href'])) {
  120. return false;
  121. }
  122. if (!isset($this->styles[$location])) {
  123. $this->styles[$location] = [];
  124. }
  125. $id = !empty($element['id']) ? ['id' => (string) $element['id']] : [];
  126. $href = $element['href'];
  127. $type = !empty($element['type']) ? (string) $element['type'] : 'text/css';
  128. $media = !empty($element['media']) ? (string) $element['media'] : null;
  129. unset(
  130. $element['tag'],
  131. $element['id'],
  132. $element['rel'],
  133. $element['content'],
  134. $element['href'],
  135. $element['type'],
  136. $element['media']
  137. );
  138. $this->styles[$location][md5($href) . sha1($href)] = [
  139. ':type' => 'file',
  140. ':priority' => (int) $priority,
  141. 'href' => $href,
  142. 'type' => $type,
  143. 'media' => $media,
  144. 'element' => $element
  145. ] + $id;
  146. return true;
  147. }
  148. /**
  149. * @param string|array $element
  150. * @param int $priority
  151. * @param string $location
  152. * @return bool
  153. */
  154. public function addInlineStyle($element, $priority = 0, $location = 'head')
  155. {
  156. if (!is_array($element)) {
  157. $element = ['content' => (string) $element];
  158. }
  159. if (empty($element['content'])) {
  160. return false;
  161. }
  162. if (!isset($this->styles[$location])) {
  163. $this->styles[$location] = [];
  164. }
  165. $content = (string) $element['content'];
  166. $type = !empty($element['type']) ? (string) $element['type'] : 'text/css';
  167. $this->styles[$location][md5($content) . sha1($content)] = [
  168. ':type' => 'inline',
  169. ':priority' => (int) $priority,
  170. 'content' => $content,
  171. 'type' => $type
  172. ];
  173. return true;
  174. }
  175. /**
  176. * @param string|array $element
  177. * @param int $priority
  178. * @param string $location
  179. * @return bool
  180. */
  181. public function addScript($element, $priority = 0, $location = 'head')
  182. {
  183. if (!is_array($element)) {
  184. $element = ['src' => (string) $element];
  185. }
  186. if (empty($element['src'])) {
  187. return false;
  188. }
  189. if (!isset($this->scripts[$location])) {
  190. $this->scripts[$location] = [];
  191. }
  192. $src = $element['src'];
  193. $type = !empty($element['type']) ? (string) $element['type'] : 'text/javascript';
  194. $defer = isset($element['defer']) ? true : false;
  195. $async = isset($element['async']) ? true : false;
  196. $handle = !empty($element['handle']) ? (string) $element['handle'] : '';
  197. $this->scripts[$location][md5($src) . sha1($src)] = [
  198. ':type' => 'file',
  199. ':priority' => (int) $priority,
  200. 'src' => $src,
  201. 'type' => $type,
  202. 'defer' => $defer,
  203. 'async' => $async,
  204. 'handle' => $handle
  205. ];
  206. return true;
  207. }
  208. /**
  209. * @param string|array $element
  210. * @param int $priority
  211. * @param string $location
  212. * @return bool
  213. */
  214. public function addInlineScript($element, $priority = 0, $location = 'head')
  215. {
  216. if (!is_array($element)) {
  217. $element = ['content' => (string) $element];
  218. }
  219. if (empty($element['content'])) {
  220. return false;
  221. }
  222. if (!isset($this->scripts[$location])) {
  223. $this->scripts[$location] = [];
  224. }
  225. $content = (string) $element['content'];
  226. $type = !empty($element['type']) ? (string) $element['type'] : 'text/javascript';
  227. $this->scripts[$location][md5($content) . sha1($content)] = [
  228. ':type' => 'inline',
  229. ':priority' => (int) $priority,
  230. 'content' => $content,
  231. 'type' => $type
  232. ];
  233. return true;
  234. }
  235. /**
  236. * @param string $html
  237. * @param int $priority
  238. * @param string $location
  239. * @return bool
  240. */
  241. public function addHtml($html, $priority = 0, $location = 'bottom')
  242. {
  243. if (empty($html) || !is_string($html)) {
  244. return false;
  245. }
  246. if (!isset($this->html[$location])) {
  247. $this->html[$location] = [];
  248. }
  249. $this->html[$location][md5($html) . sha1($html)] = [
  250. ':priority' => (int) $priority,
  251. 'html' => $html
  252. ];
  253. return true;
  254. }
  255. /**
  256. * @return array
  257. */
  258. protected function getAssetsFast()
  259. {
  260. $assets = [
  261. 'frameworks' => $this->frameworks,
  262. 'styles' => $this->styles,
  263. 'scripts' => $this->scripts,
  264. 'html' => $this->html
  265. ];
  266. foreach ($this->blocks as $block) {
  267. if ($block instanceof HtmlBlock) {
  268. $blockAssets = $block->getAssetsFast();
  269. $assets['frameworks'] += $blockAssets['frameworks'];
  270. foreach ($blockAssets['styles'] as $location => $styles) {
  271. if (!isset($assets['styles'][$location])) {
  272. $assets['styles'][$location] = $styles;
  273. } elseif ($styles) {
  274. $assets['styles'][$location] += $styles;
  275. }
  276. }
  277. foreach ($blockAssets['scripts'] as $location => $scripts) {
  278. if (!isset($assets['scripts'][$location])) {
  279. $assets['scripts'][$location] = $scripts;
  280. } elseif ($scripts) {
  281. $assets['scripts'][$location] += $scripts;
  282. }
  283. }
  284. foreach ($blockAssets['html'] as $location => $htmls) {
  285. if (!isset($assets['html'][$location])) {
  286. $assets['html'][$location] = $htmls;
  287. } elseif ($htmls) {
  288. $assets['html'][$location] += $htmls;
  289. }
  290. }
  291. }
  292. }
  293. return $assets;
  294. }
  295. /**
  296. * @param string $type
  297. * @param string $location
  298. * @return array
  299. */
  300. protected function getAssetsInLocation($type, $location)
  301. {
  302. $assets = $this->getAssetsFast();
  303. if (empty($assets[$type][$location])) {
  304. return [];
  305. }
  306. $styles = $assets[$type][$location];
  307. $this->sortAssetsInLocation($styles);
  308. return $styles;
  309. }
  310. /**
  311. * @param array $items
  312. */
  313. protected function sortAssetsInLocation(array &$items)
  314. {
  315. $count = 0;
  316. foreach ($items as &$item) {
  317. $item[':order'] = ++$count;
  318. }
  319. unset($item);
  320. uasort(
  321. $items,
  322. function ($a, $b) {
  323. return ($a[':priority'] === $b[':priority'])
  324. ? $a[':order'] - $b[':order'] : $a[':priority'] - $b[':priority'];
  325. }
  326. );
  327. }
  328. /**
  329. * @param array $array
  330. */
  331. protected function sortAssets(array &$array)
  332. {
  333. foreach ($array as $location => &$items) {
  334. $this->sortAssetsInLocation($items);
  335. }
  336. }
  337. }