Admin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 Easy admin version.
  30. *
  31. * @var string
  32. */
  33. const VERSION = '1.0.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. * Returns the long version of dcat-admin.
  52. *
  53. * @return string The long application version
  54. */
  55. public static function longVersion()
  56. {
  57. return sprintf('Dcat Admin <comment>version</comment> <info>%s</info>', static::VERSION);
  58. }
  59. /**
  60. * Left sider-bar menu.
  61. *
  62. * @param Closure|null $builder
  63. *
  64. * @return Menu
  65. */
  66. public static function menu(Closure $builder = null)
  67. {
  68. $menu = app('admin.menu');
  69. $builder && $builder($menu);
  70. return $menu;
  71. }
  72. /**
  73. * Get or set admin title.
  74. *
  75. * @return string|void
  76. */
  77. public static function title($title = null)
  78. {
  79. if ($title === null) {
  80. return static::$metaTitle ?: config('admin.title');
  81. }
  82. static::$metaTitle = $title;
  83. }
  84. /**
  85. * @param null|string $favicon
  86. *
  87. * @return string|void
  88. */
  89. public static function favicon($favicon = null)
  90. {
  91. if (is_null($favicon)) {
  92. return static::$favicon;
  93. }
  94. static::$favicon = $favicon;
  95. }
  96. /**
  97. * @param $repository
  98. * @param Closure|null $callback
  99. *
  100. * @return Grid
  101. */
  102. public static function grid($repository = null, \Closure $callback = null)
  103. {
  104. if ($repository instanceof \Closure) {
  105. return new Grid(null, $callback);
  106. }
  107. return new Grid($repository, $callback);
  108. }
  109. /**
  110. * @param $repository
  111. * @param Closure $callable
  112. *
  113. * @return Form
  114. */
  115. public static function form($repository, Closure $callable = null)
  116. {
  117. return new Form($repository, $callable);
  118. }
  119. /**
  120. * Build a tree.
  121. *
  122. * @param $model
  123. * @param Closure|null $callable
  124. *
  125. * @return Tree
  126. */
  127. public static function tree($model, Closure $callable = null)
  128. {
  129. return new Tree($model, $callable);
  130. }
  131. /**
  132. * Build show page.
  133. *
  134. * @example
  135. * $show = Admin::show();
  136. *
  137. * $show = Admin::show(new Repository);
  138. *
  139. * $show = Admin::show(new Repository, function (Show $show) {});
  140. *
  141. * $show = Admin::show($id, new Repository, function (Show $show) {});
  142. *
  143. * @param $repository
  144. * @param mixed $callable
  145. *
  146. * @return Show
  147. */
  148. public static function show($id = null, $repository = null, \Closure $callable = null)
  149. {
  150. switch (func_num_args()) {
  151. case 0:
  152. $show = new Show();
  153. break;
  154. case 1:
  155. $show = new Show($id);
  156. break;
  157. case 2:
  158. $show = new Show($id, $repository);
  159. break;
  160. case 3:
  161. $show = new Show($repository, $callable);
  162. $show->key($id);
  163. }
  164. return $show;
  165. }
  166. /**
  167. * @param Closure $callable
  168. *
  169. * @return Content
  170. */
  171. public static function content(Closure $callable = null)
  172. {
  173. return new Content($callable);
  174. }
  175. /**
  176. * Get current login user.
  177. *
  178. * @return Model|Authenticatable|HasPermissions
  179. */
  180. public static function user()
  181. {
  182. return static::guard()->user();
  183. }
  184. /**
  185. * Attempt to get the guard from the local cache.
  186. *
  187. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|GuardHelpers
  188. */
  189. public static function guard()
  190. {
  191. return Auth::guard(config('admin.auth.guard') ?: 'admin');
  192. }
  193. /**
  194. * Navbar.
  195. *
  196. * @param Closure|null $builder
  197. *
  198. * @return Navbar
  199. */
  200. public static function navbar(Closure $builder = null)
  201. {
  202. $navbar = app('admin.navbar');
  203. $builder && $builder($navbar);
  204. return $navbar;
  205. }
  206. /**
  207. * Get section manager.
  208. *
  209. * @param Closure|null $builder
  210. *
  211. * @return SectionManager
  212. */
  213. public static function section(Closure $builder = null)
  214. {
  215. $manager = app('admin.sections');
  216. $builder && $builder($manager);
  217. return $manager;
  218. }
  219. /**
  220. * Register the admin routes.
  221. *
  222. * @return void
  223. */
  224. public static function routes()
  225. {
  226. $attributes = [
  227. 'prefix' => config('admin.route.prefix'),
  228. 'middleware' => config('admin.route.middleware'),
  229. ];
  230. app('router')->group($attributes, function ($router) {
  231. $enableAuth = config('admin.auth.enable', true);
  232. /* @var \Illuminate\Routing\Router $router */
  233. $router->namespace('Dcat\Admin\Controllers')->group(function ($router) use ($enableAuth) {
  234. if ($enableAuth) {
  235. /* @var \Illuminate\Routing\Router $router */
  236. $router->resource('auth/users', 'UserController');
  237. $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
  238. $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
  239. if (config('admin.permission.enable')) {
  240. $router->resource('auth/roles', 'RoleController');
  241. $router->resource('auth/permissions', 'PermissionController');
  242. }
  243. }
  244. $router->post('_handle_action_', 'HandleActionController@handle')->name('admin.handle-action');
  245. $router->post('_handle_form_', 'HandleFormController@handle')->name('admin.handle-form');
  246. });
  247. if ($enableAuth) {
  248. $authController = config('admin.auth.controller', AuthController::class);
  249. $router->get('auth/login', $authController.'@getLogin');
  250. $router->post('auth/login', $authController.'@postLogin');
  251. $router->get('auth/logout', $authController.'@getLogout');
  252. $router->get('auth/setting', $authController.'@getSetting');
  253. $router->put('auth/setting', $authController.'@putSetting');
  254. }
  255. });
  256. static::registerHelperRoutes();
  257. }
  258. /**
  259. * Register the helpers routes.
  260. *
  261. * @return void
  262. */
  263. protected static function registerHelperRoutes()
  264. {
  265. if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
  266. return;
  267. }
  268. $attributes = [
  269. 'prefix' => config('admin.route.prefix'),
  270. 'middleware' => config('admin.route.middleware'),
  271. ];
  272. app('router')->group($attributes, function ($router) {
  273. /* @var \Illuminate\Routing\Router $router */
  274. $router->get('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@index');
  275. $router->post('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@store');
  276. $router->post('helpers/scaffold/table', 'Dcat\Admin\Controllers\ScaffoldController@table');
  277. $router->get('helpers/routes', 'Dcat\Admin\Controllers\RouteController@index');
  278. $router->get('helpers/icons', 'Dcat\Admin\Controllers\IconController@index');
  279. $router->resource('helpers/extensions', 'Dcat\Admin\Controllers\ExtensionController', ['only' => ['index', 'update']]);
  280. $router->post('helpers/extensions/import', 'Dcat\Admin\Controllers\ExtensionController@import');
  281. $router->post('helpers/extensions/create', 'Dcat\Admin\Controllers\ExtensionController@create');
  282. });
  283. }
  284. /**
  285. * Create a repository instance.
  286. *
  287. * @param string|Repository|Model|Builder $class
  288. * @param array $args
  289. *
  290. * @return Repository
  291. */
  292. public static function repository($class, array $args = [])
  293. {
  294. $repository = $class;
  295. if (is_string($repository)) {
  296. $repository = new $class($args);
  297. }
  298. if ($repository instanceof Model || $repository instanceof Builder) {
  299. $repository = EloquentRepository::make($repository);
  300. }
  301. if (! $repository instanceof Repository) {
  302. throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
  303. }
  304. if ($repository instanceof Proxy) {
  305. return $repository;
  306. }
  307. return new Proxy($repository);
  308. }
  309. /**
  310. * Get all registered extensions.
  311. *
  312. * @return array
  313. */
  314. public static function extensions()
  315. {
  316. return static::$extensions;
  317. }
  318. /**
  319. * Get available extensions.
  320. *
  321. * @return Extension[]
  322. */
  323. public static function availableExtensions()
  324. {
  325. if (static::$availableExtensions !== null) {
  326. return static::$availableExtensions;
  327. }
  328. static::$availableExtensions = [];
  329. foreach (static::$extensions as $k => $extension) {
  330. if (! config("admin-extensions.{$k}.enable")) {
  331. continue;
  332. }
  333. static::$availableExtensions[$k] = $extension::make();
  334. }
  335. return static::$availableExtensions;
  336. }
  337. /**
  338. * Extend a extension.
  339. *
  340. * @param string $class
  341. *
  342. * @return void
  343. */
  344. public static function extend(string $class)
  345. {
  346. static::$extensions[$class::NAME] = $class;
  347. }
  348. /**
  349. * Enable the extension.
  350. *
  351. * @param string $class
  352. * @param bool $enable
  353. *
  354. * @return bool
  355. */
  356. public static function enableExtenstion(string $class, bool $enable = true)
  357. {
  358. if (! $class || ! is_subclass_of($class, Extension::class)) {
  359. return false;
  360. }
  361. $name = $class::NAME;
  362. $config = (array) config('admin-extensions');
  363. $config[$name] = (array) ($config[$name] ?? []);
  364. $config[$name]['enable'] = $enable;
  365. return Helper::updateExtensionConfig($config);
  366. }
  367. /**
  368. * Disable the extension.
  369. *
  370. * @param string $class
  371. *
  372. * @return bool
  373. */
  374. public static function disableExtenstion(string $class)
  375. {
  376. return static::enableExtenstion($class, false);
  377. }
  378. /**
  379. * @return Handler
  380. */
  381. public static function makeExceptionHandler()
  382. {
  383. return app(
  384. config('admin.exception_handler') ?: Handler::class
  385. );
  386. }
  387. /**
  388. * @param callable $callback
  389. */
  390. public static function booting($callback)
  391. {
  392. Event::listen('admin.booting', $callback);
  393. }
  394. /**
  395. * @param callable $callback
  396. */
  397. public static function booted($callback)
  398. {
  399. Event::listen('admin.booted', $callback);
  400. }
  401. /**
  402. * Call booting callbacks.
  403. */
  404. public static function callBooting()
  405. {
  406. Event::dispatch('admin.booting');
  407. }
  408. /**
  409. * Call booted callbacks.
  410. */
  411. public static function callBooted()
  412. {
  413. Event::dispatch('admin.booted');
  414. }
  415. }