Admin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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\Repositories\EloquentRepository;
  12. use Dcat\Admin\Repositories\Proxy;
  13. use Dcat\Admin\Support\Helper;
  14. use Dcat\Admin\Traits\HasAssets;
  15. use Dcat\Admin\Traits\HasPermissions;
  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. use Illuminate\Support\Fluent;
  23. /**
  24. * Class Admin.
  25. */
  26. class Admin
  27. {
  28. use HasAssets;
  29. /**
  30. * 版本号.
  31. *
  32. * @var string
  33. */
  34. const VERSION = '1.2.0';
  35. /**
  36. * @var array
  37. */
  38. protected static $extensions = [];
  39. /**
  40. * @var array
  41. */
  42. protected static $availableExtensions;
  43. /**
  44. * @var string
  45. */
  46. protected static $metaTitle;
  47. /**
  48. * @var string
  49. */
  50. protected static $favicon;
  51. /**
  52. * @var array
  53. */
  54. public static $jsVariables = [];
  55. /**
  56. * @var string
  57. */
  58. public static $pjaxContainerId = 'pjax-container';
  59. /**
  60. * 版本.
  61. *
  62. * @return string
  63. */
  64. public static function longVersion()
  65. {
  66. return sprintf('Dcat Admin <comment>version</comment> <info>%s</info>', static::VERSION);
  67. }
  68. /**
  69. * @return Color
  70. */
  71. public static function color()
  72. {
  73. return app('admin.color');
  74. }
  75. /**
  76. * 菜单管理.
  77. *
  78. * @param Closure|null $builder
  79. *
  80. * @return Menu
  81. */
  82. public static function menu(Closure $builder = null)
  83. {
  84. $menu = app('admin.menu');
  85. $builder && $builder($menu);
  86. return $menu;
  87. }
  88. /**
  89. * 设置 title.
  90. *
  91. * @return string|void
  92. */
  93. public static function title($title = null)
  94. {
  95. if ($title === null) {
  96. return static::$metaTitle ?: config('admin.title');
  97. }
  98. static::$metaTitle = $title;
  99. }
  100. /**
  101. * @param null|string $favicon
  102. *
  103. * @return string|void
  104. */
  105. public static function favicon($favicon = null)
  106. {
  107. if (is_null($favicon)) {
  108. return static::$favicon;
  109. }
  110. static::$favicon = $favicon;
  111. }
  112. /**
  113. * @param Closure $callable
  114. *
  115. * @return Content
  116. */
  117. public static function content(Closure $callable = null)
  118. {
  119. return new Content($callable);
  120. }
  121. /**
  122. * 获取登录用户模型.
  123. *
  124. * @return Model|Authenticatable|HasPermissions
  125. */
  126. public static function user()
  127. {
  128. return static::guard()->user();
  129. }
  130. /**
  131. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|GuardHelpers
  132. */
  133. public static function guard()
  134. {
  135. return Auth::guard(config('admin.auth.guard') ?: 'admin');
  136. }
  137. /**
  138. * @param Closure|null $builder
  139. *
  140. * @return Navbar
  141. */
  142. public static function navbar(Closure $builder = null)
  143. {
  144. $navbar = app('admin.navbar');
  145. $builder && $builder($navbar);
  146. return $navbar;
  147. }
  148. /**
  149. * section.
  150. *
  151. * @param Closure|null $builder
  152. *
  153. * @return SectionManager
  154. */
  155. public static function section(Closure $builder = null)
  156. {
  157. $manager = app('admin.sections');
  158. $builder && $builder($manager);
  159. return $manager;
  160. }
  161. /**
  162. * 注册路由.
  163. *
  164. * @return void
  165. */
  166. public static function routes()
  167. {
  168. $attributes = [
  169. 'prefix' => config('admin.route.prefix'),
  170. 'middleware' => config('admin.route.middleware'),
  171. ];
  172. if (config('admin.auth.enable', true)) {
  173. app('router')->group($attributes, function ($router) {
  174. /* @var \Illuminate\Routing\Router $router */
  175. $router->namespace('Dcat\Admin\Controllers')->group(function ($router) {
  176. /* @var \Illuminate\Routing\Router $router */
  177. $router->resource('auth/users', 'UserController');
  178. $router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
  179. $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
  180. if (config('admin.permission.enable')) {
  181. $router->resource('auth/roles', 'RoleController');
  182. $router->resource('auth/permissions', 'PermissionController');
  183. }
  184. });
  185. $authController = config('admin.auth.controller', AuthController::class);
  186. $router->get('auth/login', $authController.'@getLogin');
  187. $router->post('auth/login', $authController.'@postLogin');
  188. $router->get('auth/logout', $authController.'@getLogout');
  189. $router->get('auth/setting', $authController.'@getSetting');
  190. $router->put('auth/setting', $authController.'@putSetting');
  191. });
  192. }
  193. static::registerHelperRoutes();
  194. }
  195. /**
  196. * 注册api路由.
  197. *
  198. * @return void
  199. */
  200. public static function registerApiRoutes()
  201. {
  202. $attributes = [
  203. 'prefix' => admin_base_path('dcat-api'),
  204. 'middleware' => config('admin.route.middleware'),
  205. 'as' => 'dcat.api.',
  206. ];
  207. app('router')->group($attributes, function ($router) {
  208. /* @var \Illuminate\Routing\Router $router */
  209. $router->namespace('Dcat\Admin\Controllers')->group(function ($router) {
  210. /* @var \Illuminate\Routing\Router $router */
  211. $router->post('action', 'HandleActionController@handle')->name('action');
  212. $router->post('form', 'HandleFormController@handle')->name('form');
  213. $router->post('value', 'ValueController@handle')->name('value');
  214. $router->post('tinymce/upload', 'TinymceController@upload')->name('tinymce.upload');
  215. $router->post('editor-md/upload', 'EditorMDController@upload')->name('editor-md.upload');
  216. });
  217. });
  218. }
  219. /**
  220. * 注册开发工具路由.
  221. *
  222. * @return void
  223. */
  224. public static function registerHelperRoutes()
  225. {
  226. if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
  227. return;
  228. }
  229. $attributes = [
  230. 'prefix' => config('admin.route.prefix'),
  231. 'middleware' => config('admin.route.middleware'),
  232. ];
  233. app('router')->group($attributes, function ($router) {
  234. /* @var \Illuminate\Routing\Router $router */
  235. $router->get('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@index');
  236. $router->post('helpers/scaffold', 'Dcat\Admin\Controllers\ScaffoldController@store');
  237. $router->post('helpers/scaffold/table', 'Dcat\Admin\Controllers\ScaffoldController@table');
  238. $router->get('helpers/icons', 'Dcat\Admin\Controllers\IconController@index');
  239. $router->resource('helpers/extensions', 'Dcat\Admin\Controllers\ExtensionController', ['only' => ['index', 'store', 'update']]);
  240. $router->post('helpers/extensions/import', 'Dcat\Admin\Controllers\ExtensionController@import');
  241. });
  242. }
  243. /**
  244. * 创建数据仓库实例.
  245. *
  246. * @param string|Repository|Model|Builder $value
  247. * @param array $args
  248. *
  249. * @return Repository
  250. */
  251. public static function repository($repository, array $args = [])
  252. {
  253. if (is_string($repository)) {
  254. $repository = new $repository($args);
  255. }
  256. if ($repository instanceof Model || $repository instanceof Builder) {
  257. $repository = EloquentRepository::make($repository);
  258. }
  259. if (! $repository instanceof Repository) {
  260. $class = is_object($repository) ? get_class($repository) : $repository;
  261. throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
  262. }
  263. if ($repository instanceof Proxy) {
  264. return $repository;
  265. }
  266. return new Proxy($repository);
  267. }
  268. /**
  269. * 获取所有已注册的扩展.
  270. *
  271. * @return array
  272. */
  273. public static function extensions()
  274. {
  275. return static::$extensions;
  276. }
  277. /**
  278. * 获取所有可用扩展.
  279. *
  280. * @return Extension[]
  281. */
  282. public static function availableExtensions()
  283. {
  284. if (static::$availableExtensions !== null) {
  285. return static::$availableExtensions;
  286. }
  287. static::$availableExtensions = [];
  288. foreach (static::$extensions as $k => $extension) {
  289. if (! config("admin-extensions.{$k}.enable")) {
  290. continue;
  291. }
  292. static::$availableExtensions[$k] = $extension::make();
  293. }
  294. return static::$availableExtensions;
  295. }
  296. /**
  297. * 注册扩展.
  298. *
  299. * @param string $class
  300. *
  301. * @return void
  302. */
  303. public static function extend(string $class)
  304. {
  305. static::$extensions[$class::NAME] = $class;
  306. }
  307. /**
  308. * 启用扩展.
  309. *
  310. * @param string $class
  311. * @param bool $enable
  312. *
  313. * @return bool
  314. */
  315. public static function enableExtenstion(string $class, bool $enable = true)
  316. {
  317. if (! $class || ! is_subclass_of($class, Extension::class)) {
  318. return false;
  319. }
  320. $name = $class::NAME;
  321. $config = (array) config('admin-extensions');
  322. $config[$name] = (array) ($config[$name] ?? []);
  323. $config[$name]['enable'] = $enable;
  324. return Helper::updateExtensionConfig($config);
  325. }
  326. /**
  327. * 禁用扩展.
  328. *
  329. * @param string $class
  330. *
  331. * @return bool
  332. */
  333. public static function disableExtenstion(string $class)
  334. {
  335. return static::enableExtenstion($class, false);
  336. }
  337. /**
  338. * @return Handler
  339. */
  340. public static function makeExceptionHandler()
  341. {
  342. return app(
  343. config('admin.exception_handler') ?: Handler::class
  344. );
  345. }
  346. /**
  347. * @param callable $callback
  348. */
  349. public static function booting($callback)
  350. {
  351. Event::listen('admin.booting', $callback);
  352. }
  353. /**
  354. * @param callable $callback
  355. */
  356. public static function booted($callback)
  357. {
  358. Event::listen('admin.booted', $callback);
  359. }
  360. /**
  361. * @return void
  362. */
  363. public static function callBooting()
  364. {
  365. Event::dispatch('admin.booting');
  366. }
  367. /**
  368. * @return void
  369. */
  370. public static function callBooted()
  371. {
  372. Event::dispatch('admin.booted');
  373. }
  374. /**
  375. * @return Fluent
  376. */
  377. public static function context()
  378. {
  379. return app('admin.context');
  380. }
  381. /**
  382. * @param array|string $name
  383. *
  384. * @return void
  385. */
  386. public static function addIgnoreQueryName($name)
  387. {
  388. $context = static::context();
  389. $ignoreQueries = $context->ignoreQueries ?? [];
  390. $context->ignoreQueries = array_merge($ignoreQueries, (array) $name);
  391. }
  392. /**
  393. * @return array
  394. */
  395. public static function getIgnoreQueryNames()
  396. {
  397. return static::context()->ignoreQueries ?? [];
  398. }
  399. /**
  400. * 获取js配置.
  401. *
  402. * @return string
  403. */
  404. public static function jsVariables()
  405. {
  406. static::$jsVariables['pjax_container_selector'] = '#'.static::$pjaxContainerId;
  407. static::$jsVariables['token'] = csrf_token();
  408. static::$jsVariables['lang'] = __('admin.client') ?: [];
  409. static::$jsVariables['colors'] = static::color()->all();
  410. return json_encode(static::$jsVariables);
  411. }
  412. }