Admin.php 13 KB

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