Admin.php 9.2 KB

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