Extension.php 7.9 KB

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