Admin.php 9.6 KB

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