Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. namespace Dcat\Admin;
  3. use Closure;
  4. use Dcat\Admin\Contracts\Repository;
  5. use Dcat\Admin\Controllers\AuthController;
  6. use Dcat\Admin\Exception\Handler;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Layout\Menu;
  9. use Dcat\Admin\Layout\Navbar;
  10. use Dcat\Admin\Layout\SectionManager;
  11. use Dcat\Admin\Models\HasPermissions;
  12. use Dcat\Admin\Repositories\EloquentRepository;
  13. use Dcat\Admin\Repositories\Proxy;
  14. use Dcat\Admin\Support\Helper;
  15. use Dcat\Admin\Traits\HasAssets;
  16. use Illuminate\Auth\GuardHelpers;
  17. use Illuminate\Contracts\Auth\Authenticatable;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use Illuminate\Database\Eloquent\Model;
  20. use Illuminate\Support\Facades\Auth;
  21. use Illuminate\Support\Facades\Event;
  22. /**
  23. * Class Admin.
  24. */
  25. class Admin
  26. {
  27. use HasAssets;
  28. /**
  29. * The version.
  30. *
  31. * @var string
  32. */
  33. const VERSION = '0.1.0';
  34. /**
  35. * @var array
  36. */
  37. protected static $extensions = [];
  38. /**
  39. * @var array
  40. */
  41. protected static $availableExtensions;
  42. /**
  43. * @var string
  44. */
  45. protected static $metaTitle;
  46. /**
  47. * @var string
  48. */
  49. protected static $favicon;
  50. /**
  51. * @var array
  52. */
  53. public static $jsVariables = [];
  54. /**
  55. * @var string
  56. */
  57. public static $pjaxContainerId = 'pjax-container';
  58. /**
  59. * 版本.
  60. *
  61. * @return string
  62. */
  63. public static function longVersion()
  64. {
  65. return sprintf('Dcat Admin <comment>version</comment> <info>%s</info>', static::VERSION);
  66. }
  67. /**
  68. * @return Color
  69. */
  70. public static function color()
  71. {
  72. return app('admin.color');
  73. }
  74. /**
  75. * 菜单管理.
  76. *
  77. * @param Closure|null $builder
  78. *
  79. * @return Menu
  80. */
  81. public static function menu(Closure $builder = null)
  82. {
  83. $menu = app('admin.menu');
  84. $builder && $builder($menu);
  85. return $menu;
  86. }
  87. /**
  88. * 设置 title.
  89. *
  90. * @return string|void
  91. */
  92. public static function title($title = null)
  93. {
  94. if ($title === null) {
  95. return static::$metaTitle ?: config('admin.title');
  96. }
  97. static::$metaTitle = $title;
  98. }
  99. /**
  100. * @param null|string $favicon
  101. *
  102. * @return string|void
  103. */
  104. public static function favicon($favicon = null)
  105. {
  106. if (is_null($favicon)) {
  107. return static::$favicon;
  108. }
  109. static::$favicon = $favicon;
  110. }
  111. /**
  112. * @param Closure $callable
  113. *
  114. * @return Content
  115. */
  116. public static function content(Closure $callable = null)
  117. {
  118. return new Content($callable);
  119. }
  120. /**
  121. * 获取登录用户模型.
  122. *
  123. * @return Model|Authenticatable|HasPermissions
  124. */
  125. public static function user()
  126. {
  127. return static::guard()->user();
  128. }
  129. /**
  130. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|GuardHelpers
  131. */
  132. public static function guard()
  133. {
  134. return Auth::guard(config('admin.auth.guard') ?: 'admin');
  135. }
  136. /**
  137. * @param Closure|null $builder
  138. *
  139. * @return Navbar
  140. */
  141. public static function navbar(Closure $builder = null)
  142. {
  143. $navbar = app('admin.navbar');
  144. $builder && $builder($navbar);
  145. return $navbar;
  146. }
  147. /**
  148. * section.
  149. *
  150. * @param Closure|null $builder
  151. *
  152. * @return SectionManager
  153. */
  154. public static function section(Closure $builder = null)
  155. {
  156. $manager = app('admin.sections');
  157. $builder && $builder($manager);
  158. return $manager;
  159. }
  160. /**
  161. * 注册路由.
  162. *
  163. * @return void
  164. */
  165. public static function routes()
  166. {
  167. $attributes = [
  168. 'prefix' => config('admin.route.prefix'),
  169. 'middleware' => config('admin.route.middleware'),
  170. ];
  171. if (config('admin.auth.enable', true)) {
  172. app('router')->group($attributes, function ($router) {
  173. /* @var \Illuminate\Routing\Router $router */
  174. $router->namespace('Dcat\Admin\Controllers')->group(function ($router) {
  175. /* @var \Illuminate\Routing\Router $router */
  176. $router->resource('auth/users', 'UserController');
  177. $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
  178. $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
  179. if (config('admin.permission.enable')) {
  180. $router->resource('auth/roles', 'RoleController');
  181. $router->resource('auth/permissions', 'PermissionController');
  182. }
  183. });
  184. $authController = config('admin.auth.controller', AuthController::class);
  185. $router->get('auth/login', $authController.'@getLogin');
  186. $router->post('auth/login', $authController.'@postLogin');
  187. $router->get('auth/logout', $authController.'@getLogout');
  188. $router->get('auth/setting', $authController.'@getSetting');
  189. $router->put('auth/setting', $authController.'@putSetting');
  190. });
  191. }
  192. static::registerHelperRoutes();
  193. }
  194. /**
  195. * 注册api路由
  196. *
  197. * @return void
  198. */
  199. public static function registerApiRoutes()
  200. {
  201. $attributes = [
  202. 'prefix' => admin_base_path('dcat-api'),
  203. 'middleware' => config('admin.route.middleware'),
  204. 'as' => 'dcat.api.',
  205. ];
  206. app('router')->group($attributes, function ($router) {
  207. /* @var \Illuminate\Routing\Router $router */
  208. $router->namespace('Dcat\Admin\Controllers')->group(function ($router) {
  209. $router->post('action', 'HandleActionController@handle')->name('action');
  210. $router->post('form', 'HandleFormController@handle')->name('form');
  211. $router->post('value', 'ValueController@handle')->name('value');
  212. });
  213. });
  214. }
  215. /**
  216. * 注册开发工具路由.
  217. *
  218. * @return void
  219. */
  220. public static function registerHelperRoutes()
  221. {
  222. if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
  223. return;
  224. }
  225. $attributes = [
  226. 'prefix' => config('admin.route.prefix'),
  227. 'middleware' => config('admin.route.middleware'),
  228. ];
  229. app('router')->group($attributes, function ($router) {
  230. /* @var \Illuminate\Routing\Router $router */
  231. $router->get('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@index');
  232. $router->post('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@store');
  233. $router->post('helpers/scaffold/table', 'Dcat\Admin\Controllers\ScaffoldController@table');
  234. $router->get('helpers/icons', 'Dcat\Admin\Controllers\IconController@index');
  235. $router->resource('helpers/extensions', 'Dcat\Admin\Controllers\ExtensionController', ['only' => ['index', 'store', 'update']]);
  236. $router->post('helpers/extensions/import', 'Dcat\Admin\Controllers\ExtensionController@import');
  237. });
  238. }
  239. /**
  240. * 创建数据仓库实例.
  241. *
  242. * @param string|Repository|Model|Builder $value
  243. * @param array $args
  244. *
  245. * @return Repository
  246. */
  247. public static function repository($repository, array $args = [])
  248. {
  249. if (is_string($repository)) {
  250. $repository = new $repository($args);
  251. }
  252. if ($repository instanceof Model || $repository instanceof Builder) {
  253. $repository = EloquentRepository::make($repository);
  254. }
  255. if (! $repository instanceof Repository) {
  256. $class = is_object($repository) ? get_class($repository) : $repository;
  257. throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
  258. }
  259. if ($repository instanceof Proxy) {
  260. return $repository;
  261. }
  262. return new Proxy($repository);
  263. }
  264. /**
  265. * 获取所有已注册的扩展.
  266. *
  267. * @return array
  268. */
  269. public static function extensions()
  270. {
  271. return static::$extensions;
  272. }
  273. /**
  274. * 获取所有可用扩展.
  275. *
  276. * @return Extension[]
  277. */
  278. public static function availableExtensions()
  279. {
  280. if (static::$availableExtensions !== null) {
  281. return static::$availableExtensions;
  282. }
  283. static::$availableExtensions = [];
  284. foreach (static::$extensions as $k => $extension) {
  285. if (! config("admin-extensions.{$k}.enable")) {
  286. continue;
  287. }
  288. static::$availableExtensions[$k] = $extension::make();
  289. }
  290. return static::$availableExtensions;
  291. }
  292. /**
  293. * 注册扩展.
  294. *
  295. * @param string $class
  296. *
  297. * @return void
  298. */
  299. public static function extend(string $class)
  300. {
  301. static::$extensions[$class::NAME] = $class;
  302. }
  303. /**
  304. * 启用扩展.
  305. *
  306. * @param string $class
  307. * @param bool $enable
  308. *
  309. * @return bool
  310. */
  311. public static function enableExtenstion(string $class, bool $enable = true)
  312. {
  313. if (! $class || ! is_subclass_of($class, Extension::class)) {
  314. return false;
  315. }
  316. $name = $class::NAME;
  317. $config = (array) config('admin-extensions');
  318. $config[$name] = (array) ($config[$name] ?? []);
  319. $config[$name]['enable'] = $enable;
  320. return Helper::updateExtensionConfig($config);
  321. }
  322. /**
  323. * 禁用扩展.
  324. *
  325. * @param string $class
  326. *
  327. * @return bool
  328. */
  329. public static function disableExtenstion(string $class)
  330. {
  331. return static::enableExtenstion($class, false);
  332. }
  333. /**
  334. * @return Handler
  335. */
  336. public static function makeExceptionHandler()
  337. {
  338. return app(
  339. config('admin.exception_handler') ?: Handler::class
  340. );
  341. }
  342. /**
  343. * @param callable $callback
  344. */
  345. public static function booting($callback)
  346. {
  347. Event::listen('admin.booting', $callback);
  348. }
  349. /**
  350. * @param callable $callback
  351. */
  352. public static function booted($callback)
  353. {
  354. Event::listen('admin.booted', $callback);
  355. }
  356. /**
  357. * @return void
  358. */
  359. public static function callBooting()
  360. {
  361. Event::dispatch('admin.booting');
  362. }
  363. /**
  364. * @return void
  365. */
  366. public static function callBooted()
  367. {
  368. Event::dispatch('admin.booted');
  369. }
  370. /**
  371. * 获取js配置.
  372. *
  373. * @return string
  374. */
  375. public static function jsVariables()
  376. {
  377. static::$jsVariables['pjax_container_selector'] = '#'.static::$pjaxContainerId;
  378. static::$jsVariables['token'] = csrf_token();
  379. static::$jsVariables['lang'] = __('admin.client') ?: [];
  380. static::$jsVariables['colors'] = static::color()->all();
  381. return json_encode(static::$jsVariables);
  382. }
  383. }