Admin.php 11 KB

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