Extension.php 7.8 KB

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