Admin.php 9.6 KB

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