Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. /* @var \Illuminate\Routing\Router $router */
  210. $router->post('action', 'HandleActionController@handle')->name('action');
  211. $router->post('form', 'HandleFormController@handle')->name('form');
  212. $router->post('value', 'ValueController@handle')->name('value');
  213. });
  214. });
  215. }
  216. /**
  217. * 注册开发工具路由.
  218. *
  219. * @return void
  220. */
  221. public static function registerHelperRoutes()
  222. {
  223. if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
  224. return;
  225. }
  226. $attributes = [
  227. 'prefix' => config('admin.route.prefix'),
  228. 'middleware' => config('admin.route.middleware'),
  229. ];
  230. app('router')->group($attributes, function ($router) {
  231. /* @var \Illuminate\Routing\Router $router */
  232. $router->get('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@index');
  233. $router->post('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@store');
  234. $router->post('helpers/scaffold/table', 'Dcat\Admin\Controllers\ScaffoldController@table');
  235. $router->get('helpers/icons', 'Dcat\Admin\Controllers\IconController@index');
  236. $router->resource('helpers/extensions', 'Dcat\Admin\Controllers\ExtensionController', ['only' => ['index', 'store', 'update']]);
  237. $router->post('helpers/extensions/import', 'Dcat\Admin\Controllers\ExtensionController@import');
  238. });
  239. }
  240. /**
  241. * 创建数据仓库实例.
  242. *
  243. * @param string|Repository|Model|Builder $value
  244. * @param array $args
  245. *
  246. * @return Repository
  247. */
  248. public static function repository($repository, array $args = [])
  249. {
  250. if (is_string($repository)) {
  251. $repository = new $repository($args);
  252. }
  253. if ($repository instanceof Model || $repository instanceof Builder) {
  254. $repository = EloquentRepository::make($repository);
  255. }
  256. if (! $repository instanceof Repository) {
  257. $class = is_object($repository) ? get_class($repository) : $repository;
  258. throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
  259. }
  260. if ($repository instanceof Proxy) {
  261. return $repository;
  262. }
  263. return new Proxy($repository);
  264. }
  265. /**
  266. * 获取所有已注册的扩展.
  267. *
  268. * @return array
  269. */
  270. public static function extensions()
  271. {
  272. return static::$extensions;
  273. }
  274. /**
  275. * 获取所有可用扩展.
  276. *
  277. * @return Extension[]
  278. */
  279. public static function availableExtensions()
  280. {
  281. if (static::$availableExtensions !== null) {
  282. return static::$availableExtensions;
  283. }
  284. static::$availableExtensions = [];
  285. foreach (static::$extensions as $k => $extension) {
  286. if (! config("admin-extensions.{$k}.enable")) {
  287. continue;
  288. }
  289. static::$availableExtensions[$k] = $extension::make();
  290. }
  291. return static::$availableExtensions;
  292. }
  293. /**
  294. * 注册扩展.
  295. *
  296. * @param string $class
  297. *
  298. * @return void
  299. */
  300. public static function extend(string $class)
  301. {
  302. static::$extensions[$class::NAME] = $class;
  303. }
  304. /**
  305. * 启用扩展.
  306. *
  307. * @param string $class
  308. * @param bool $enable
  309. *
  310. * @return bool
  311. */
  312. public static function enableExtenstion(string $class, bool $enable = true)
  313. {
  314. if (! $class || ! is_subclass_of($class, Extension::class)) {
  315. return false;
  316. }
  317. $name = $class::NAME;
  318. $config = (array) config('admin-extensions');
  319. $config[$name] = (array) ($config[$name] ?? []);
  320. $config[$name]['enable'] = $enable;
  321. return Helper::updateExtensionConfig($config);
  322. }
  323. /**
  324. * 禁用扩展.
  325. *
  326. * @param string $class
  327. *
  328. * @return bool
  329. */
  330. public static function disableExtenstion(string $class)
  331. {
  332. return static::enableExtenstion($class, false);
  333. }
  334. /**
  335. * @return Handler
  336. */
  337. public static function makeExceptionHandler()
  338. {
  339. return app(
  340. config('admin.exception_handler') ?: Handler::class
  341. );
  342. }
  343. /**
  344. * @param callable $callback
  345. */
  346. public static function booting($callback)
  347. {
  348. Event::listen('admin.booting', $callback);
  349. }
  350. /**
  351. * @param callable $callback
  352. */
  353. public static function booted($callback)
  354. {
  355. Event::listen('admin.booted', $callback);
  356. }
  357. /**
  358. * @return void
  359. */
  360. public static function callBooting()
  361. {
  362. Event::dispatch('admin.booting');
  363. }
  364. /**
  365. * @return void
  366. */
  367. public static function callBooted()
  368. {
  369. Event::dispatch('admin.booted');
  370. }
  371. /**
  372. * 获取js配置.
  373. *
  374. * @return string
  375. */
  376. public static function jsVariables()
  377. {
  378. static::$jsVariables['pjax_container_selector'] = '#'.static::$pjaxContainerId;
  379. static::$jsVariables['token'] = csrf_token();
  380. static::$jsVariables['lang'] = __('admin.client') ?: [];
  381. static::$jsVariables['colors'] = static::color()->all();
  382. return json_encode(static::$jsVariables);
  383. }
  384. }