ServiceProvider.php 11 KB

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