forked from jqhph/dcat-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.php
More file actions
executable file
·660 lines (560 loc) · 16.9 KB
/
Admin.php
File metadata and controls
executable file
·660 lines (560 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?php
namespace Dcat\Admin;
use Closure;
use Dcat\Admin\Contracts\ExceptionHandler;
use Dcat\Admin\Contracts\Repository;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Http\Controllers\AuthController;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Menu;
use Dcat\Admin\Layout\Navbar;
use Dcat\Admin\Layout\SectionManager;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Support\Composer;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Traits\HasAssets;
use Dcat\Admin\Traits\HasHtml;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Symfony\Component\HttpFoundation\Response;
class Admin
{
use HasAssets;
use HasHtml;
const VERSION = '2.2.2-beta';
const SECTION = [
// 往 <head> 标签内输入内容
'HEAD' => 'ADMIN_HEAD',
// 往body标签内部输入内容
'BODY_INNER_BEFORE' => 'ADMIN_BODY_INNER_BEFORE',
'BODY_INNER_AFTER' => 'ADMIN_BODY_INNER_AFTER',
// 往#app内部输入内容
'APP_INNER_BEFORE' => 'ADMIN_APP_INNER_BEFORE',
'APP_INNER_AFTER' => 'ADMIN_APP_INNER_AFTER',
// 顶部导航栏用户面板
'NAVBAR_USER_PANEL' => 'ADMIN_NAVBAR_USER_PANEL',
'NAVBAR_AFTER_USER_PANEL' => 'ADMIN_NAVBAR_AFTER_USER_PANEL',
// 顶部导航栏之前
'NAVBAR_BEFORE' => 'ADMIN_NAVBAR_BEFORE',
// 顶部导航栏底下
'NAVBAR_AFTER' => 'ADMIN_NAVBAR_AFTER',
// 侧边栏顶部用户信息面板
'LEFT_SIDEBAR_USER_PANEL' => 'ADMIN_LEFT_SIDEBAR_USER_PANEL',
// 菜单栏
'LEFT_SIDEBAR_MENU' => 'ADMIN_LEFT_SIDEBAR_MENU',
// 菜单栏顶部
'LEFT_SIDEBAR_MENU_TOP' => 'ADMIN_LEFT_SIDEBAR_MENU_TOP',
// 菜单栏底部
'LEFT_SIDEBAR_MENU_BOTTOM' => 'ADMIN_LEFT_SIDEBAR_MENU_BOTTOM',
];
private static $defaultPjaxContainerId = 'pjax-container';
/**
* 版本.
*
* @return string
*/
public static function longVersion()
{
return sprintf('Dcat Admin <comment>version</comment> <info>%s</info>', static::VERSION);
}
/**
* @return Color
*/
public static function color()
{
return app('admin.color');
}
/**
* 菜单管理.
*
* @param Closure|null $builder
* @return Menu
*/
public static function menu(Closure $builder = null)
{
$menu = app('admin.menu');
$builder && $builder($menu);
return $menu;
}
/**
* 设置 title.
*
* @return string|void
*/
public static function title($title = null)
{
if ($title === null) {
return static::context()->metaTitle ?: config('admin.title');
}
static::context()->metaTitle = $title;
}
/**
* @param null|string $favicon
* @return string|void
*/
public static function favicon($favicon = null)
{
if ($favicon === null) {
return static::context()->favicon ?: config('admin.favicon');
}
static::context()->favicon = $favicon;
}
/**
* 设置翻译文件路径.
*
* @param string|null $path
*/
public static function translation(?string $path)
{
static::context()->translation = $path;
}
/**
* 获取登录用户模型.
*
* @return Model|Authenticatable|HasPermissions
*/
public static function user()
{
return static::guard()->user();
}
/**
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|GuardHelpers
*/
public static function guard()
{
return Auth::guard(config('admin.auth.guard') ?: 'admin');
}
/**
* @param Closure|null $builder
* @return Navbar
*/
public static function navbar(Closure $builder = null)
{
$navbar = app('admin.navbar');
$builder && $builder($navbar);
return $navbar;
}
/**
* 启用或禁用Pjax.
*
* @param bool $value
* @return void
*/
public static function pjax(bool $value = true)
{
static::context()->pjaxContainerId = $value ? static::$defaultPjaxContainerId : false;
}
/**
* 禁用pjax.
*
* @return void
*/
public static function disablePjax()
{
static::pjax(false);
}
/**
* 获取pjax ID.
*
* @return string|void
*/
public static function getPjaxContainerId()
{
$id = static::context()->pjaxContainerId;
if ($id === false) {
return;
}
return $id ?: static::$defaultPjaxContainerId;
}
/**
* section.
*
* @param Closure|null $builder
* @return SectionManager
*/
public static function section(Closure $builder = null)
{
$manager = app('admin.sections');
$builder && $builder($manager);
return $manager;
}
/**
* 配置.
*
* @return \Dcat\Admin\Support\Setting
*/
public static function setting()
{
return app('admin.setting');
}
/**
* 创建数据仓库实例.
*
* @param string|Repository|Model|Builder $value
* @param array $args
* @return Repository
*/
public static function repository($repository, array $args = [])
{
if (is_string($repository)) {
$repository = new $repository($args);
}
if ($repository instanceof Model || $repository instanceof Builder) {
$repository = EloquentRepository::make($repository);
}
if (! $repository instanceof Repository) {
$class = is_object($repository) ? get_class($repository) : $repository;
throw new InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
}
return $repository;
}
/**
* 应用管理.
*
* @return Application
*/
public static function app()
{
return app('admin.app');
}
/**
* 处理异常.
*
* @param \Throwable $e
* @return mixed
*/
public static function handleException(\Throwable $e)
{
return app(ExceptionHandler::class)->handle($e);
}
/**
* 上报异常.
*
* @param \Throwable $e
* @return mixed
*/
public static function reportException(\Throwable $e)
{
return app(ExceptionHandler::class)->report($e);
}
/**
* 显示异常信息.
*
* @param \Throwable $e
* @return mixed
*/
public static function renderException(\Throwable $e)
{
return app(ExceptionHandler::class)->render($e);
}
/**
* @param callable $callback
*/
public static function booting($callback)
{
Event::listen('admin:booting', $callback);
}
/**
* @param callable $callback
*/
public static function booted($callback)
{
Event::listen('admin:booted', $callback);
}
/**
* @return void
*/
public static function callBooting()
{
Event::dispatch('admin:booting');
}
/**
* @return void
*/
public static function callBooted()
{
Event::dispatch('admin:booted');
}
/**
* 上下文管理.
*
* @return \Dcat\Admin\Support\Context
*/
public static function context()
{
return app('admin.context');
}
/**
* 翻译器.
*
* @return \Dcat\Admin\Support\Translator
*/
public static function translator()
{
return app('admin.translator');
}
/**
* @param array|string $name
* @return void
*/
public static function addIgnoreQueryName($name)
{
$context = static::context();
$ignoreQueries = $context->ignoreQueries ?? [];
$context->ignoreQueries = array_merge($ignoreQueries, (array) $name);
}
/**
* @return array
*/
public static function getIgnoreQueryNames()
{
return static::context()->ignoreQueries ?? [];
}
/**
* 中断默认的渲染逻辑.
*
* @param string|\Illuminate\Contracts\Support\Renderable|\Closure $value
*/
public static function prevent($value)
{
if ($value !== null) {
static::context()->add('contents', $value);
}
}
/**
* @return bool
*/
public static function shouldPrevent()
{
return count(static::context()->getArray('contents')) > 0;
}
/**
* 渲染内容.
*
* @return string|void
*/
public static function renderContents()
{
if (! static::shouldPrevent()) {
return;
}
$results = '';
foreach (static::context()->getArray('contents') as $content) {
$results .= Helper::render($content);
}
// 等待JS脚本加载完成
static::script('Dcat.wait()', true);
$asset = static::asset();
static::baseCss([], false);
static::baseJs([], false);
static::headerJs([], false);
static::fonts([]);
return $results
.static::html()
.$asset->jsToHtml()
.$asset->cssToHtml()
.$asset->scriptToHtml()
.$asset->styleToHtml();
}
/**
* 响应json数据.
*
* @param array $data
* @return JsonResponse
*/
public static function json(array $data = [])
{
return JsonResponse::make($data);
}
/**
* 插件管理.
*
* @param string $name
* @return \Dcat\Admin\Extend\Manager|\Dcat\Admin\Extend\ServiceProvider|null
*/
public static function extension(?string $name = null)
{
if ($name) {
return app('admin.extend')->get($name);
}
return app('admin.extend');
}
/**
* 响应并中断后续逻辑.
*
* @param Response|string|array $response
*
* @throws HttpResponseException
*/
public static function exit($response = '')
{
if (is_array($response)) {
$response = response()->json($response);
} elseif ($response instanceof JsonResponse) {
$response = $response->send();
}
throw new HttpResponseException($response instanceof Response ? $response : response($response));
}
/**
* 类自动加载器.
*
* @return \Composer\Autoload\ClassLoader
*/
public static function classLoader()
{
return Composer::loader();
}
/**
* 往分组插入中间件.
*
* @param array $mix
*/
public static function mixMiddlewareGroup(array $mix = [])
{
$router = app('router');
$group = $router->getMiddlewareGroups()['admin'] ?? [];
if ($mix) {
$finalGroup = [];
foreach ($group as $i => $mid) {
$next = $i + 1;
$finalGroup[] = $mid;
if (! isset($group[$next]) || $group[$next] !== 'admin.permission') {
continue;
}
$finalGroup = array_merge($finalGroup, $mix);
$mix = [];
}
if ($mix) {
$finalGroup = array_merge($finalGroup, $mix);
}
$group = $finalGroup;
}
$router->middlewareGroup('admin', $group);
}
/**
* 获取js配置.
*
* @param array|null $variables
* @return string
*/
public static function jsVariables(array $variables = null)
{
$jsVariables = static::context()->jsVariables ?: [];
if ($variables !== null) {
static::context()->jsVariables = array_merge(
$jsVariables,
$variables
);
return;
}
$sidebarStyle = config('admin.layout.sidebar_style') ?: 'light';
$pjaxId = static::getPjaxContainerId();
$jsVariables['pjax_container_selector'] = $pjaxId ? ('#'.$pjaxId) : '';
$jsVariables['token'] = csrf_token();
$jsVariables['lang'] = ($lang = __('admin.client')) ? array_merge($lang, $jsVariables['lang'] ?? []) : [];
$jsVariables['colors'] = static::color()->all();
$jsVariables['dark_mode'] = static::isDarkMode();
$jsVariables['sidebar_dark'] = config('admin.layout.sidebar_dark') || ($sidebarStyle === 'dark');
$jsVariables['sidebar_light_style'] = in_array($sidebarStyle, ['dark', 'light'], true) ? 'sidebar-light-primary' : 'sidebar-primary';
return admin_javascript_json($jsVariables);
}
/**
* @return bool
*/
public static function isDarkMode()
{
$bodyClass = config('admin.layout.body_class');
return in_array(
'dark-mode',
is_array($bodyClass) ? $bodyClass : explode(' ', $bodyClass),
true
);
}
/**
* 注册路由.
*
* @return void
*/
public static function routes()
{
$attributes = [
'prefix' => config('admin.route.prefix'),
'middleware' => config('admin.route.middleware'),
];
if (config('admin.auth.enable', true)) {
app('router')->group($attributes, function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->namespace('Dcat\Admin\Http\Controllers')->group(function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->resource('auth/users', 'UserController');
$router->resource('auth/menu', 'MenuController', ['except' => ['create', 'show']]);
if (config('admin.permission.enable')) {
$router->resource('auth/roles', 'RoleController');
$router->resource('auth/permissions', 'PermissionController');
}
});
$router->resource('auth/extensions', 'Dcat\Admin\Http\Controllers\ExtensionController', ['only' => ['index', 'store', 'update']]);
$authController = config('admin.auth.controller', AuthController::class);
$router->get('auth/login', $authController.'@getLogin');
$router->post('auth/login', $authController.'@postLogin');
$router->get('auth/logout', $authController.'@getLogout');
$router->get('auth/setting', $authController.'@getSetting');
$router->put('auth/setting', $authController.'@putSetting');
});
}
static::registerHelperRoutes();
}
/**
* 注册api路由.
*
* @return void
*/
public static function registerApiRoutes()
{
$attributes = [
'prefix' => admin_base_path('dcat-api'),
'middleware' => config('admin.route.middleware'),
'namespace' => 'Dcat\Admin\Http\Controllers',
'as' => 'dcat-api.',
];
app('router')->group($attributes, function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->post('action', 'HandleActionController@handle')->name('action');
$router->post('form', 'HandleFormController@handle')->name('form');
$router->post('form/upload', 'HandleFormController@uploadFile')->name('form.upload');
$router->post('form/destroy-file', 'HandleFormController@destroyFile')->name('form.destroy-file');
$router->post('value', 'ValueController@handle')->name('value');
$router->get('render', 'RenderableController@handle')->name('render');
$router->post('tinymce/upload', 'TinymceController@upload')->name('tinymce.upload');
$router->post('editor-md/upload', 'EditorMDController@upload')->name('editor-md.upload');
});
}
/**
* 注册开发工具路由.
*
* @return void
*/
public static function registerHelperRoutes()
{
if (! config('admin.helpers.enable', true) || ! config('app.debug')) {
return;
}
$attributes = [
'prefix' => config('admin.route.prefix'),
'middleware' => config('admin.route.middleware'),
];
app('router')->group($attributes, function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->get('helpers/scaffold', 'Dcat\Admin\Http\Controllers\ScaffoldController@index');
$router->post('helpers/scaffold', 'Dcat\Admin\Http\Controllers\ScaffoldController@store');
$router->post('helpers/scaffold/table', 'Dcat\Admin\Http\Controllers\ScaffoldController@table');
$router->get('helpers/icons', 'Dcat\Admin\Http\Controllers\IconController@index');
});
}
}