ServiceProvider.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. namespace Dcat\Admin\Extend;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\ComposerProperty;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\Validator;
  7. use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
  8. use Symfony\Component\Console\Output\NullOutput;
  9. abstract class ServiceProvider extends LaravelServiceProvider
  10. {
  11. /**
  12. * @var ComposerProperty
  13. */
  14. protected $composerProperty;
  15. /**
  16. * @var string
  17. */
  18. protected $name;
  19. /**
  20. * @var string
  21. */
  22. protected $path;
  23. /**
  24. * @var array
  25. */
  26. protected $js = [];
  27. /**
  28. * @var array
  29. */
  30. protected $css = [];
  31. /**
  32. * @var array
  33. */
  34. protected $menu = [];
  35. /**
  36. * @var array
  37. */
  38. protected $permission = [];
  39. /**
  40. * @var array
  41. */
  42. protected $menuValidationRules = [
  43. 'title' => 'required',
  44. 'path' => 'required',
  45. 'icon' => 'required',
  46. ];
  47. /**
  48. * @var array
  49. */
  50. protected $permissionValidationRules = [
  51. 'name' => 'required',
  52. 'slug' => 'required',
  53. 'path' => 'required',
  54. ];
  55. /**
  56. * @var \Symfony\Component\Console\Output\OutputInterface
  57. */
  58. public $output;
  59. public function __construct($app)
  60. {
  61. parent::__construct($app);
  62. $this->output = new NullOutput();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function boot()
  68. {
  69. if ($views = $this->getViewPath()) {
  70. $this->loadViewsFrom($views, $this->getName());
  71. }
  72. if ($lang = $this->getLangPath()) {
  73. $this->loadTranslationsFrom($lang, $this->getName());
  74. }
  75. if ($migrations = $this->getMigrationPath()) {
  76. $this->loadMigrationsFrom($migrations);
  77. }
  78. if ($routes = $this->getRoutes()) {
  79. return $this->registerRoutes($routes);
  80. }
  81. $this->registerAssets();
  82. }
  83. /**
  84. * 获取扩展名称.
  85. *
  86. * @return string
  87. */
  88. final public function getName()
  89. {
  90. return $this->name ?: ($this->name = str_replace('/', '.', $this->composerProperty->name));
  91. }
  92. /**
  93. * 获取扩展包路径.
  94. *
  95. * @param string $path
  96. *
  97. * @return string
  98. *
  99. * @throws \ReflectionException
  100. */
  101. public function path(?string $path = null)
  102. {
  103. if (! $this->path) {
  104. $this->path = realpath(dirname((new \ReflectionClass(static::class))->getFileName()).'/..');
  105. if (! is_dir($this->path)) {
  106. throw new \Exception("The {$this->path} is not a directory.");
  107. }
  108. }
  109. $path = ltrim($path, '/');
  110. return $path ? $this->path.'/'.$path : $this->path;
  111. }
  112. /**
  113. * 判断扩展是否启用.
  114. *
  115. * @return bool
  116. */
  117. final public function enabled()
  118. {
  119. return Admin::extension()->enabled($this->getName());
  120. }
  121. /**
  122. * 判断扩展是否禁用.
  123. *
  124. * @return bool
  125. */
  126. final public function disabled()
  127. {
  128. return ! $this->enabled();
  129. }
  130. /**
  131. * 获取配置.
  132. *
  133. * @param string $key
  134. * @param null $default
  135. *
  136. * @return \Illuminate\Config\Repository|mixed
  137. */
  138. final public function config($key = null, $default = null)
  139. {
  140. }
  141. /**
  142. * 导入扩展.
  143. */
  144. public function import()
  145. {
  146. $this->importMenus();
  147. $this->importPermissions();
  148. }
  149. /**
  150. * 卸载扩展.
  151. */
  152. public function uninstall()
  153. {
  154. }
  155. /**
  156. * 注册路由.
  157. *
  158. * @param $callback
  159. */
  160. public function registerRoutes($callback)
  161. {
  162. Admin::app()->routes(function ($router) use ($callback) {
  163. $attributes = array_merge(
  164. [
  165. 'prefix' => config('admin.route.prefix'),
  166. 'middleware' => config('admin.route.middleware'),
  167. ]
  168. );
  169. $router->group($attributes, $callback);
  170. });
  171. }
  172. /**
  173. * 获取静态资源路径.
  174. *
  175. * @return string
  176. */
  177. public function getAssetPath()
  178. {
  179. return $this->path('resources/assets');
  180. }
  181. /**
  182. * 获取视图路径.
  183. *
  184. * @return string
  185. */
  186. public function getViewPath()
  187. {
  188. return $this->path('resources/views');
  189. }
  190. /**
  191. * 获取数据迁移文件路径.
  192. *
  193. * @return string
  194. */
  195. public function getMigrationPath()
  196. {
  197. return $this->path('database/migrations');
  198. }
  199. /**
  200. * 获取语言包路径.
  201. *
  202. * @return string
  203. */
  204. public function getLangPath()
  205. {
  206. return $this->path('resources/lang');
  207. }
  208. /**
  209. * 获取路由地址.
  210. *
  211. * @return string
  212. *
  213. * @throws \ReflectionException
  214. */
  215. public function getRoutes()
  216. {
  217. $path = $this->path('src/Http/routes.php');
  218. return is_file($path) ? $path : null;
  219. }
  220. /**
  221. * 获取菜单.
  222. *
  223. * @return array
  224. */
  225. protected function menu()
  226. {
  227. return $this->menu;
  228. }
  229. /**
  230. * @return array
  231. */
  232. protected function permission()
  233. {
  234. return $this->permission;
  235. }
  236. /**
  237. * @param ComposerProperty $composerProperty
  238. *
  239. * @return $this
  240. */
  241. public function withComposerProperty(ComposerProperty $composerProperty)
  242. {
  243. $this->composerProperty = $composerProperty;
  244. return $this;
  245. }
  246. /**
  247. * 导入菜单.
  248. *
  249. * @throws \Exception
  250. */
  251. protected function importMenus()
  252. {
  253. if (! ($menu = $this->menu()) || ! $this->validateMenu($menu)) {
  254. return;
  255. }
  256. extract($menu);
  257. if ($this->checkMenu($path)) {
  258. $this->output->writeln("<warn>Menu [$path] already exists!</warn>");
  259. } else {
  260. $this->createMenu($title, $path, $icon);
  261. $this->output->writeln('<info>Import extension menu succeeded!</info>');
  262. }
  263. }
  264. /**
  265. * 导入权限.
  266. *
  267. * @throws \Exception
  268. */
  269. protected function importPermissions()
  270. {
  271. if (! $this->config('admin.permission.enable')) {
  272. return;
  273. }
  274. if (! ($permission = $this->permission()) || ! $this->validatePermission($permission)) {
  275. return;
  276. }
  277. extract($permission);
  278. if ($this->checkPermission($slug)) {
  279. $this->output->writeln("<warn>Permission [$slug] already exists!</warn>");
  280. } else {
  281. $this->createPermission($name, $slug, $path);
  282. $this->output->writeln('<info>Import extension permission succeeded!</info>');
  283. }
  284. }
  285. /**
  286. * 注册别名.
  287. */
  288. protected function registerAssets()
  289. {
  290. if ($this->js || $this->css) {
  291. Admin::asset()->alias($this->getName(), $this->js, $this->css);
  292. }
  293. }
  294. /**
  295. * 验证菜单.
  296. *
  297. * @param array $menu
  298. *
  299. * @throws \Exception
  300. *
  301. * @return bool
  302. */
  303. protected function validateMenu(array $menu)
  304. {
  305. /** @var \Illuminate\Validation\Validator $validator */
  306. $validator = Validator::make($menu, $this->menuValidationRules);
  307. if ($validator->passes()) {
  308. return true;
  309. }
  310. $message = "Invalid menu:\r\n".implode("\r\n", Arr::flatten($validator->errors()->messages()));
  311. $this->output->writeln("<error>{$message}</error>");
  312. }
  313. /**
  314. * @param $path
  315. *
  316. * @return bool
  317. */
  318. protected function checkMenu($path)
  319. {
  320. $menuModel = config('admin.database.menu_model');
  321. return $result = $menuModel::where('uri', $path)->exists();
  322. }
  323. /**
  324. * 验证权限.
  325. *
  326. * @param array $permission
  327. *
  328. * @throws \Exception
  329. *
  330. * @return bool
  331. */
  332. protected function validatePermission(array $permission)
  333. {
  334. /** @var \Illuminate\Validation\Validator $validator */
  335. $validator = Validator::make($permission, $this->permissionValidationRules);
  336. if ($validator->passes()) {
  337. return true;
  338. }
  339. $message = "Invalid permission:\r\n".implode("\r\n", Arr::flatten($validator->errors()->messages()));
  340. $this->output->writeln("<error>{$message}</error>");
  341. }
  342. /**
  343. * 创建菜单.
  344. *
  345. * @param string $title
  346. * @param string $uri
  347. * @param string $icon
  348. * @param int $parentId
  349. */
  350. protected function createMenu($title, $uri, $icon = 'fa-bars', $parentId = 0)
  351. {
  352. $menuModel = config('admin.database.menu_model');
  353. $lastOrder = $menuModel::max('order');
  354. $menuModel::create([
  355. 'parent_id' => $parentId,
  356. 'order' => $lastOrder + 1,
  357. 'title' => $title,
  358. 'icon' => $icon,
  359. 'uri' => $uri,
  360. ]);
  361. }
  362. /**
  363. * @param $slug
  364. *
  365. * @return bool
  366. */
  367. protected function checkPermission($slug)
  368. {
  369. $permissionModel = config('admin.database.permissions_model');
  370. return $permissionModel::where('slug', $slug)->exists();
  371. }
  372. /**
  373. * 创建权限.
  374. *
  375. * @param $name
  376. * @param $slug
  377. * @param $path
  378. */
  379. protected function createPermission($name, $slug, $path)
  380. {
  381. $permissionModel = config('admin.database.permissions_model');
  382. $permissionModel::create([
  383. 'name' => $name,
  384. 'slug' => $slug,
  385. 'http_path' => trim($path, '/'),
  386. ]);
  387. }
  388. }