Admin.php 11 KB

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