Helper.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace Dcat\Admin\Support;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\Artisan;
  8. use Illuminate\Support\Str;
  9. class Helper
  10. {
  11. /**
  12. * Update extension config.
  13. *
  14. * @param array $config
  15. * @return bool
  16. */
  17. public static function updateExtensionConfig(array $config)
  18. {
  19. $files = app('files');
  20. $result = (bool)$files->put(config_path('admin-extensions.php'), Helper::exportArrayPhp($config));
  21. if ($result && is_file(base_path('bootstrap/cache/config.php'))) {
  22. Artisan::call('config:cache');
  23. }
  24. \config(['admin-extensions' => $config]);
  25. return $result;
  26. }
  27. /**
  28. * Converts the given value to an array.
  29. *
  30. * @param $value
  31. * @param bool $filter
  32. * @return array
  33. */
  34. public static function array($value, bool $filter = true)
  35. {
  36. if (!$value) {
  37. return [];
  38. }
  39. if ($value instanceof \Closure) {
  40. $value = $value();
  41. }
  42. if (is_array($value)) {
  43. } elseif ($value instanceof Arrayable) {
  44. $value = $value->toArray();
  45. } elseif (is_string($value)) {
  46. $value = explode(',', $value);
  47. } else {
  48. $value = (array) $value;
  49. }
  50. return $filter ? array_filter($value, function ($v) {
  51. return $v !== '' && $v !== null;
  52. }) : $value;
  53. }
  54. /**
  55. * Converts the given value to string.
  56. *
  57. * @param $value
  58. * @param array $params
  59. * @param null $bindTo
  60. * @return mixed|string
  61. */
  62. public static function render($value, $params = [], $bindTo = null)
  63. {
  64. if (!$value) {
  65. return '';
  66. }
  67. if (is_string($value)) {
  68. return $value;
  69. }
  70. if ($value instanceof \Closure) {
  71. if ($bindTo) {
  72. $value->bindTo($bindTo);
  73. }
  74. $value = $value(...(array)$params);
  75. }
  76. if ($value instanceof Renderable) {
  77. return $value->render();
  78. }
  79. if ($value instanceof Htmlable) {
  80. return $value->toHtml();
  81. }
  82. return (string)$value;
  83. }
  84. /**
  85. * Build an HTML attribute string from an array.
  86. *
  87. * @param array $attributes
  88. * @return string
  89. */
  90. public static function buildHtmlAttributes($attributes)
  91. {
  92. $html = '';
  93. foreach ((array)$attributes as $key => &$value) {
  94. if (is_numeric($key)) {
  95. $key = $value;
  96. }
  97. $element = '';
  98. if ($value !== null) {
  99. $element = $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8').'"';
  100. }
  101. $html .= $element;
  102. }
  103. return $html;
  104. }
  105. /**
  106. * Get url with the added query string parameters.
  107. *
  108. * @param string $url
  109. * @param array $query
  110. * @return string
  111. */
  112. public static function urlWithQuery(?string $url, array $query = [])
  113. {
  114. if (!$url || !$query) {
  115. return $url;
  116. }
  117. $array = explode('?', $url);
  118. $url = $array[0];
  119. $originalQuery = $array[1] ?? '';
  120. parse_str($originalQuery, $originalQuery);
  121. return $url.'?'.http_build_query(array_merge($originalQuery, $query));
  122. }
  123. /**
  124. * If a request match the specific path.
  125. *
  126. * @example
  127. * Helper::matchRequestPath('auth/user')
  128. * Helper::matchRequestPath('auth/user*')
  129. * Helper::matchRequestPath('auth/user/* /edit')
  130. * Helper::matchRequestPath('GET,POST:auth/user')
  131. *
  132. * @param string $path
  133. * @param null|string $current
  134. * @return bool
  135. */
  136. public static function matchRequestPath($path, ?string $current = null)
  137. {
  138. $request = request();
  139. $current = $current ?: $request->decodedPath();
  140. if (Str::contains($path, ':')) {
  141. list($methods, $path) = explode(':', $path);
  142. $methods = array_map('strtoupper', explode(',', $methods));
  143. if (!empty($methods) && !in_array($request->method(), $methods)) {
  144. return false;
  145. }
  146. }
  147. if (!Str::contains($path, '*')) {
  148. return $path === $current;
  149. }
  150. $path = str_replace(['*', '/'], ['([0-9a-z-_,])*', "\/"], $path);
  151. return preg_match("/$path/i", $current);
  152. }
  153. /**
  154. * Build nested array.
  155. *
  156. * @param array $nodes
  157. * @param int $parentId
  158. * @param string|null $primaryKeyName
  159. * @param string|null $parentKeyName
  160. * @param string|null $childrenKeyName
  161. * @return array
  162. */
  163. public static function buildNestedArray(
  164. $nodes = [],
  165. $parentId = 0,
  166. ?string $primaryKeyName = null,
  167. ?string $parentKeyName = null,
  168. ?string $childrenKeyName = null
  169. )
  170. {
  171. $branch = [];
  172. $primaryKeyName = $primaryKeyName ?: 'id';
  173. $parentKeyName = $parentKeyName ?: 'parent_id';
  174. $childrenKeyName = $childrenKeyName ?: 'children';
  175. $parentId = is_numeric($parentId) ? (int)$parentId : $parentId;
  176. foreach ($nodes as $node) {
  177. $pk = Arr::get($node, $parentKeyName);
  178. $pk = is_numeric($pk) ? (int)$pk : $pk;
  179. if ($pk === $parentId) {
  180. $children = static::buildNestedArray(
  181. $nodes,
  182. Arr::get($node, $primaryKeyName),
  183. $primaryKeyName,
  184. $parentKeyName,
  185. $childrenKeyName
  186. );
  187. if ($children) {
  188. $node[$childrenKeyName] = $children;
  189. }
  190. $branch[] = $node;
  191. }
  192. }
  193. return $branch;
  194. }
  195. /**
  196. * Generate a URL friendly "slug" from a given string.
  197. *
  198. * @param string $name
  199. * @param string $symbol
  200. * @return mixed
  201. */
  202. public static function slug(string $name, string $symbol = '-')
  203. {
  204. $text = preg_replace_callback('/([A-Z])/', function (&$text) use ($symbol) {
  205. return $symbol . strtolower($text[1]);
  206. }, $name);
  207. return str_replace('_', $symbol, ltrim($text, $symbol));
  208. }
  209. /**
  210. * 把php数据转化成文本形式
  211. *
  212. * @param array $array
  213. * @param int $level
  214. * @return string
  215. */
  216. public static function exportArray(array &$array, $level = 1)
  217. {
  218. $start = '[';
  219. $end = ']';
  220. $txt = "$start\n";
  221. foreach ($array as $k => &$v) {
  222. if (is_array($v)) {
  223. $pre = is_string($k) ? "'$k' => " : "$k => ";
  224. $txt .= str_repeat(' ', $level * 4) . $pre . static::exportArray($v, $level + 1) . ",\n";
  225. continue;
  226. }
  227. $t = $v;
  228. if ($v === true) {
  229. $t = 'true';
  230. } elseif ($v === false) {
  231. $t = 'false';
  232. } elseif ($v === null) {
  233. $t = 'null';
  234. } elseif (is_string($v)) {
  235. $v = str_replace("'", "\\'", $v);
  236. $t = "'$v'";
  237. }
  238. $pre = is_string($k) ? "'$k' => " : "$k => ";
  239. $txt .= str_repeat(' ', $level * 4). "{$pre}{$t},\n";
  240. }
  241. return $txt . str_repeat(' ', ($level - 1) * 4) . $end;
  242. }
  243. /**
  244. * 把php数据转化成文本形式,并以"return"形式返回
  245. *
  246. * @param array $array
  247. * @return string
  248. */
  249. public static function exportArrayPhp(array $array)
  250. {
  251. return "<?php \nreturn " . static::exportArray($array) . ";\n";
  252. }
  253. }