TestingAssetsTrait.php 7.2 KB

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