Admin.php 11 KB

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