Extension.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace Dcat\Admin;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Route;
  6. use Illuminate\Support\Facades\Validator;
  7. abstract class Extension
  8. {
  9. const NAME = null;
  10. /**
  11. * @var string
  12. */
  13. protected $serviceProvider;
  14. /**
  15. * @var array
  16. */
  17. protected $css = [];
  18. /**
  19. * @var array
  20. */
  21. protected $js = [];
  22. /**
  23. * @var string
  24. */
  25. protected $assets = '';
  26. /**
  27. * @var string
  28. */
  29. protected $views = '';
  30. /**
  31. * @var string
  32. */
  33. protected $lang = '';
  34. /**
  35. * @var string
  36. */
  37. protected $migrations = '';
  38. /**
  39. * @var string
  40. */
  41. protected $composerJson = '';
  42. /**
  43. * @var array
  44. */
  45. protected $menu = [];
  46. /**
  47. * @var array
  48. */
  49. protected $permission = [];
  50. /**
  51. * The menu validation rules.
  52. *
  53. * @var array
  54. */
  55. protected $menuValidationRules = [
  56. 'title' => 'required',
  57. 'path' => 'required',
  58. 'icon' => 'required',
  59. ];
  60. /**
  61. * The permission validation rules.
  62. *
  63. * @var array
  64. */
  65. protected $permissionValidationRules = [
  66. 'name' => 'required',
  67. 'slug' => 'required',
  68. 'path' => 'required',
  69. ];
  70. /**
  71. * @return string
  72. */
  73. public function getName()
  74. {
  75. return static::NAME;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function composerJson()
  81. {
  82. return $this->composerJson;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function serviceProvider()
  88. {
  89. return $this->serviceProvider;
  90. }
  91. /**
  92. * Get the path of assets files.
  93. *
  94. * @return string
  95. */
  96. public function assets()
  97. {
  98. return $this->assets;
  99. }
  100. /**
  101. * Get the paths of css files.
  102. *
  103. * @return array
  104. */
  105. public function css()
  106. {
  107. return $this->css;
  108. }
  109. /**
  110. * Get the paths of js files.
  111. *
  112. * @return array
  113. */
  114. public function js()
  115. {
  116. return $this->js;
  117. }
  118. /**
  119. * Get the path of view files.
  120. *
  121. * @return string
  122. */
  123. public function views()
  124. {
  125. return $this->views;
  126. }
  127. /**
  128. * Get the path of migration files.
  129. *
  130. * @return string
  131. */
  132. public function migrations()
  133. {
  134. return $this->migrations;
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function menu()
  140. {
  141. return $this->menu;
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function permission()
  147. {
  148. return $this->permission;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function lang()
  154. {
  155. return $this->lang;
  156. }
  157. /**
  158. * Whether the extension is enabled.
  159. *
  160. * @return bool
  161. */
  162. final public static function enabled()
  163. {
  164. return config('admin-extensions.'.static::NAME.'.enable') ? true : false;
  165. }
  166. /**
  167. * Whether the extension is disabled.
  168. *
  169. * @return bool
  170. */
  171. final public static function disabled()
  172. {
  173. return ! static::enabled();
  174. }
  175. /**
  176. * Get config set in config/admin.php.
  177. *
  178. * @param string $key
  179. * @param null $default
  180. *
  181. * @return \Illuminate\Config\Repository|mixed
  182. */
  183. final public function config($key = null, $default = null)
  184. {
  185. if (is_null($key)) {
  186. $key = sprintf('admin.extensions.%s', static::NAME);
  187. } else {
  188. $key = sprintf('admin.extensions.%s.%s', static::NAME, $key);
  189. }
  190. return config($key, $default);
  191. }
  192. /**
  193. * Import menu item and permission to dcat-admin.
  194. */
  195. public function import(Command $command)
  196. {
  197. if ($menu = $this->menu()) {
  198. if ($this->validateMenu($menu)) {
  199. extract($menu);
  200. if ($this->checkMenuExist($path)) {
  201. $command->warn("Menu [$path] already exists!");
  202. } else {
  203. $this->createMenu($title, $path, $icon);
  204. $command->info('Import extension menu succeeded!');
  205. }
  206. }
  207. }
  208. if ($permission = $this->permission()) {
  209. if ($this->validatePermission($permission)) {
  210. extract($permission);
  211. if ($this->checkPermissionExist($slug)) {
  212. $command->warn("Permission [$slug] already exists!");
  213. } else {
  214. $this->createPermission($name, $slug, $path);
  215. $command->info('Import extension permission succeeded!');
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Uninstall the extension.
  222. *
  223. * @param Command $command
  224. */
  225. public function uninstall(Command $command)
  226. {
  227. }
  228. /**
  229. * Validate menu fields.
  230. *
  231. * @param array $menu
  232. *
  233. * @throws \Exception
  234. *
  235. * @return bool
  236. */
  237. public function validateMenu(array $menu)
  238. {
  239. /** @var \Illuminate\Validation\Validator $validator */
  240. $validator = Validator::make($menu, $this->menuValidationRules);
  241. if ($validator->passes()) {
  242. return true;
  243. }
  244. $message = "Invalid menu:\r\n".implode("\r\n", Arr::flatten($validator->errors()->messages()));
  245. throw new \Exception($message);
  246. }
  247. /**
  248. * @param $path
  249. *
  250. * @return bool
  251. */
  252. protected function checkMenuExist($path)
  253. {
  254. $menuModel = config('admin.database.menu_model');
  255. /* @var \Illuminate\Database\Eloquent\Builder $query */
  256. $query = $menuModel::query();
  257. $result = $query->where('uri', $path)
  258. ->get()
  259. ->first();
  260. return $result ? true : false;
  261. }
  262. /**
  263. * Validate permission fields.
  264. *
  265. * @param array $permission
  266. *
  267. * @throws \Exception
  268. *
  269. * @return bool
  270. */
  271. public function validatePermission(array $permission)
  272. {
  273. /** @var \Illuminate\Validation\Validator $validator */
  274. $validator = Validator::make($permission, $this->permissionValidationRules);
  275. if ($validator->passes()) {
  276. return true;
  277. }
  278. $message = "Invalid permission:\r\n".implode("\r\n", Arr::flatten($validator->errors()->messages()));
  279. throw new \Exception($message);
  280. }
  281. /**
  282. * Create a item in dcat-admin left side menu.
  283. *
  284. * @param string $title
  285. * @param string $uri
  286. * @param string $icon
  287. * @param int $parentId
  288. */
  289. protected function createMenu($title, $uri, $icon = 'fa-bars', $parentId = 0)
  290. {
  291. $menuModel = config('admin.database.menu_model');
  292. $lastOrder = $menuModel::max('order');
  293. $menuModel::create([
  294. 'parent_id' => $parentId,
  295. 'order' => $lastOrder + 1,
  296. 'title' => $title,
  297. 'icon' => $icon,
  298. 'uri' => $uri,
  299. ]);
  300. }
  301. /**
  302. * @param $slug
  303. *
  304. * @return bool
  305. */
  306. protected function checkPermissionExist($slug)
  307. {
  308. $permissionModel = config('admin.database.permissions_model');
  309. /* @var \Illuminate\Database\Eloquent\Builder $query */
  310. $query = $permissionModel::query();
  311. $result = $query->where('slug', $slug)
  312. ->get()
  313. ->first();
  314. return $result ? true : false;
  315. }
  316. /**
  317. * Create a permission for this extension.
  318. *
  319. * @param $name
  320. * @param $slug
  321. * @param $path
  322. */
  323. protected function createPermission($name, $slug, $path)
  324. {
  325. $permissionModel = config('admin.database.permissions_model');
  326. $permissionModel::create([
  327. 'name' => $name,
  328. 'slug' => $slug,
  329. 'http_path' => '/'.trim($path, '/'),
  330. ]);
  331. }
  332. /**
  333. * Set routes for this extension.
  334. *
  335. * @param $callback
  336. */
  337. public function routes($callback)
  338. {
  339. $attributes = array_merge(
  340. [
  341. 'prefix' => config('admin.route.prefix'),
  342. 'middleware' => config('admin.route.middleware'),
  343. ],
  344. $this->config('route', [])
  345. );
  346. Route::group($attributes, $callback);
  347. }
  348. /**
  349. * @return static
  350. */
  351. public static function make()
  352. {
  353. return new static();
  354. }
  355. }