Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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('sectionManager');
  216. $builder && $builder($manager);
  217. return $manager;
  218. }
  219. /**
  220. * Register the auth routes.
  221. *
  222. * @return void
  223. */
  224. public static function registerAuthRoutes()
  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->namespace('Dcat\Admin\Controllers')->group(function ($router) {
  233. /* @var \Illuminate\Routing\Router $router */
  234. $router->resource('auth/users', 'UserController');
  235. $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
  236. $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
  237. if (config('admin.permission.enable')) {
  238. $router->resource('auth/roles', 'RoleController');
  239. $router->resource('auth/permissions', 'PermissionController');
  240. }
  241. $router->post('_handle_action_', 'HandleActionController@handle')->name('admin.handle-action');
  242. $router->post('_handle_form_', 'HandleFormController@handle')->name('admin.handle-form');
  243. });
  244. $authController = config('admin.auth.controller', AuthController::class);
  245. $router->get('auth/login', $authController.'@getLogin');
  246. $router->post('auth/login', $authController.'@postLogin');
  247. $router->get('auth/logout', $authController.'@getLogout');
  248. $router->get('auth/setting', $authController.'@getSetting');
  249. $router->put('auth/setting', $authController.'@putSetting');
  250. });
  251. }
  252. /**
  253. * Register the helpers routes.
  254. *
  255. * @return void
  256. */
  257. public static function registerHelperRoutes()
  258. {
  259. $attributes = [
  260. 'prefix' => config('admin.route.prefix'),
  261. 'middleware' => config('admin.route.middleware'),
  262. ];
  263. app('router')->group($attributes, function ($router) {
  264. /* @var \Illuminate\Routing\Router $router */
  265. $router->get('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@index');
  266. $router->post('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@store');
  267. $router->post('helpers/scaffold/table', 'Dcat\Admin\Controllers\ScaffoldController@table');
  268. $router->get('helpers/routes', 'Dcat\Admin\Controllers\RouteController@index');
  269. $router->get('helpers/icons', 'Dcat\Admin\Controllers\IconController@index');
  270. $router->resource('helpers/extensions', 'Dcat\Admin\Controllers\ExtensionController', ['only' => ['index', 'update']]);
  271. $router->post('helpers/extensions/import', 'Dcat\Admin\Controllers\ExtensionController@import');
  272. $router->post('helpers/extensions/create', 'Dcat\Admin\Controllers\ExtensionController@create');
  273. });
  274. }
  275. /**
  276. * Create a repository instance.
  277. *
  278. * @param string|Repository|Model|Builder $class
  279. * @param array $args
  280. *
  281. * @return Repository
  282. */
  283. public static function repository($class, array $args = [])
  284. {
  285. $repository = $class;
  286. if (is_string($repository)) {
  287. $repository = new $class($args);
  288. }
  289. if ($repository instanceof Model || $repository instanceof Builder) {
  290. $repository = EloquentRepository::make($repository);
  291. }
  292. if (! $repository instanceof Repository) {
  293. throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
  294. }
  295. if ($repository instanceof Proxy) {
  296. return $repository;
  297. }
  298. return new Proxy($repository);
  299. }
  300. /**
  301. * Get all registered extensions.
  302. *
  303. * @return array
  304. */
  305. public static function extensions()
  306. {
  307. return static::$extensions;
  308. }
  309. /**
  310. * Get available extensions.
  311. *
  312. * @return Extension[]
  313. */
  314. public static function availableExtensions()
  315. {
  316. if (static::$availableExtensions !== null) {
  317. return static::$availableExtensions;
  318. }
  319. static::$availableExtensions = [];
  320. foreach (static::$extensions as $k => $extension) {
  321. if (! config("admin-extensions.{$k}.enable")) {
  322. continue;
  323. }
  324. static::$availableExtensions[$k] = $extension::make();
  325. }
  326. return static::$availableExtensions;
  327. }
  328. /**
  329. * Extend a extension.
  330. *
  331. * @param string $class
  332. *
  333. * @return void
  334. */
  335. public static function extend(string $class)
  336. {
  337. static::$extensions[$class::NAME] = $class;
  338. }
  339. /**
  340. * Enable the extension.
  341. *
  342. * @param string $class
  343. * @param bool $enable
  344. *
  345. * @return bool
  346. */
  347. public static function enableExtenstion(string $class, bool $enable = true)
  348. {
  349. if (! $class || ! is_subclass_of($class, Extension::class)) {
  350. return false;
  351. }
  352. $name = $class::NAME;
  353. $config = (array) config('admin-extensions');
  354. $config[$name] = (array) ($config[$name] ?? []);
  355. $config[$name]['enable'] = $enable;
  356. return Helper::updateExtensionConfig($config);
  357. }
  358. /**
  359. * Disable the extension.
  360. *
  361. * @param string $class
  362. *
  363. * @return bool
  364. */
  365. public static function disableExtenstion(string $class)
  366. {
  367. return static::enableExtenstion($class, false);
  368. }
  369. /**
  370. * @return Handler
  371. */
  372. public static function makeExceptionHandler()
  373. {
  374. return app(
  375. config('admin.exception_handler') ?: Handler::class
  376. );
  377. }
  378. /**
  379. * @param callable $callback
  380. */
  381. public static function booting($callback)
  382. {
  383. Event::listen('admin.booting', $callback);
  384. }
  385. /**
  386. * @param callable $callback
  387. */
  388. public static function booted($callback)
  389. {
  390. Event::listen('admin.booted', $callback);
  391. }
  392. /**
  393. * Call booting callbacks.
  394. */
  395. public static function callBooting()
  396. {
  397. Event::dispatch('admin.booting');
  398. }
  399. /**
  400. * Call booted callbacks.
  401. */
  402. public static function callBooted()
  403. {
  404. Event::dispatch('admin.booted');
  405. }
  406. }