Helper.php 7.6 KB

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