TestingAssetsTrait.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets\Traits
  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\Common\Assets\Traits;
  9. use FilesystemIterator;
  10. use Grav\Common\Grav;
  11. use RecursiveDirectoryIterator;
  12. use RecursiveIteratorIterator;
  13. use RegexIterator;
  14. use function strlen;
  15. /**
  16. * Trait TestingAssetsTrait
  17. * @package Grav\Common\Assets\Traits
  18. */
  19. trait TestingAssetsTrait
  20. {
  21. /**
  22. * Determines if an asset exists as a collection, CSS or JS reference
  23. *
  24. * @param string $asset
  25. * @return bool
  26. */
  27. public function exists($asset)
  28. {
  29. return isset($this->collections[$asset]) || isset($this->assets_css[$asset]) || isset($this->assets_js[$asset]);
  30. }
  31. /**
  32. * Return the array of all the registered collections
  33. *
  34. * @return array
  35. */
  36. public function getCollections()
  37. {
  38. return $this->collections;
  39. }
  40. /**
  41. * Set the array of collections explicitly
  42. *
  43. * @param array $collections
  44. * @return $this
  45. */
  46. public function setCollection($collections)
  47. {
  48. $this->collections = $collections;
  49. return $this;
  50. }
  51. /**
  52. * Return the array of all the registered CSS assets
  53. * If a $key is provided, it will try to return only that asset
  54. * else it will return null
  55. *
  56. * @param string|null $key the asset key
  57. * @return array
  58. */
  59. public function getCss($key = null)
  60. {
  61. if (null !== $key) {
  62. $asset_key = md5($key);
  63. return $this->assets_css[$asset_key] ?? null;
  64. }
  65. return $this->assets_css;
  66. }
  67. /**
  68. * Return the array of all the registered JS assets
  69. * If a $key is provided, it will try to return only that asset
  70. * else it will return null
  71. *
  72. * @param string|null $key the asset key
  73. * @return array
  74. */
  75. public function getJs($key = null)
  76. {
  77. if (null !== $key) {
  78. $asset_key = md5($key);
  79. return $this->assets_js[$asset_key] ?? null;
  80. }
  81. return $this->assets_js;
  82. }
  83. /**
  84. * Set the whole array of CSS assets
  85. *
  86. * @param array $css
  87. * @return $this
  88. */
  89. public function setCss($css)
  90. {
  91. $this->assets_css = $css;
  92. return $this;
  93. }
  94. /**
  95. * Set the whole array of JS assets
  96. *
  97. * @param array $js
  98. * @return $this
  99. */
  100. public function setJs($js)
  101. {
  102. $this->assets_js = $js;
  103. return $this;
  104. }
  105. /**
  106. * Removes an item from the CSS array if set
  107. *
  108. * @param string $key The asset key
  109. * @return $this
  110. */
  111. public function removeCss($key)
  112. {
  113. $asset_key = md5($key);
  114. if (isset($this->assets_css[$asset_key])) {
  115. unset($this->assets_css[$asset_key]);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Removes an item from the JS array if set
  121. *
  122. * @param string $key The asset key
  123. * @return $this
  124. */
  125. public function removeJs($key)
  126. {
  127. $asset_key = md5($key);
  128. if (isset($this->assets_js[$asset_key])) {
  129. unset($this->assets_js[$asset_key]);
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Sets the state of CSS Pipeline
  135. *
  136. * @param bool $value
  137. * @return $this
  138. */
  139. public function setCssPipeline($value)
  140. {
  141. $this->css_pipeline = (bool)$value;
  142. return $this;
  143. }
  144. /**
  145. * Sets the state of JS Pipeline
  146. *
  147. * @param bool $value
  148. * @return $this
  149. */
  150. public function setJsPipeline($value)
  151. {
  152. $this->js_pipeline = (bool)$value;
  153. return $this;
  154. }
  155. /**
  156. * Reset all assets.
  157. *
  158. * @return $this
  159. */
  160. public function reset()
  161. {
  162. $this->resetCss();
  163. $this->resetJs();
  164. $this->setCssPipeline(false);
  165. $this->setJsPipeline(false);
  166. $this->order = [];
  167. return $this;
  168. }
  169. /**
  170. * Reset JavaScript assets.
  171. *
  172. * @return $this
  173. */
  174. public function resetJs()
  175. {
  176. $this->assets_js = [];
  177. return $this;
  178. }
  179. /**
  180. * Reset CSS assets.
  181. *
  182. * @return $this
  183. */
  184. public function resetCss()
  185. {
  186. $this->assets_css = [];
  187. return $this;
  188. }
  189. /**
  190. * Explicitly set's a timestamp for assets
  191. *
  192. * @param string|int $value
  193. */
  194. public function setTimestamp($value)
  195. {
  196. $this->timestamp = $value;
  197. }
  198. /**
  199. * Get the timestamp for assets
  200. *
  201. * @param bool $include_join
  202. * @return string|null
  203. */
  204. public function getTimestamp($include_join = true)
  205. {
  206. if ($this->timestamp) {
  207. return $include_join ? '?' . $this->timestamp : $this->timestamp;
  208. }
  209. return null;
  210. }
  211. /**
  212. * Add all assets matching $pattern within $directory.
  213. *
  214. * @param string $directory Relative to the Grav root path, or a stream identifier
  215. * @param string $pattern (regex)
  216. * @return $this
  217. */
  218. public function addDir($directory, $pattern = self::DEFAULT_REGEX)
  219. {
  220. $root_dir = GRAV_ROOT;
  221. // Check if $directory is a stream.
  222. if (strpos($directory, '://')) {
  223. $directory = Grav::instance()['locator']->findResource($directory, null);
  224. }
  225. // Get files
  226. $files = $this->rglob($root_dir . DIRECTORY_SEPARATOR . $directory, $pattern, $root_dir . '/');
  227. // No luck? Nothing to do
  228. if (!$files) {
  229. return $this;
  230. }
  231. // Add CSS files
  232. if ($pattern === self::CSS_REGEX) {
  233. foreach ($files as $file) {
  234. $this->addCss($file);
  235. }
  236. return $this;
  237. }
  238. // Add JavaScript files
  239. if ($pattern === self::JS_REGEX) {
  240. foreach ($files as $file) {
  241. $this->addJs($file);
  242. }
  243. return $this;
  244. }
  245. // Add JavaScript Module files
  246. if ($pattern === self::JS_MODULE_REGEX) {
  247. foreach ($files as $file) {
  248. $this->addJsModule($file);
  249. }
  250. return $this;
  251. }
  252. // Unknown pattern.
  253. foreach ($files as $asset) {
  254. $this->add($asset);
  255. }
  256. return $this;
  257. }
  258. /**
  259. * Add all JavaScript assets within $directory
  260. *
  261. * @param string $directory Relative to the Grav root path, or a stream identifier
  262. * @return $this
  263. */
  264. public function addDirJs($directory)
  265. {
  266. return $this->addDir($directory, self::JS_REGEX);
  267. }
  268. /**
  269. * Add all CSS assets within $directory
  270. *
  271. * @param string $directory Relative to the Grav root path, or a stream identifier
  272. * @return $this
  273. */
  274. public function addDirCss($directory)
  275. {
  276. return $this->addDir($directory, self::CSS_REGEX);
  277. }
  278. /**
  279. * Recursively get files matching $pattern within $directory.
  280. *
  281. * @param string $directory
  282. * @param string $pattern (regex)
  283. * @param string|null $ltrim Will be trimmed from the left of the file path
  284. * @return array
  285. */
  286. protected function rglob($directory, $pattern, $ltrim = null)
  287. {
  288. $iterator = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
  289. $directory,
  290. FilesystemIterator::SKIP_DOTS
  291. )), $pattern);
  292. $offset = strlen($ltrim);
  293. $files = [];
  294. foreach ($iterator as $file) {
  295. $files[] = substr($file->getPathname(), $offset);
  296. }
  297. return $files;
  298. }
  299. }