1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Encore\Admin\Auth\Database\Menu; |
7
|
|
|
use Encore\Admin\Controllers\AuthController; |
8
|
|
|
use Encore\Admin\Layout\Content; |
9
|
|
|
use Encore\Admin\Traits\HasAssets; |
10
|
|
|
use Encore\Admin\Widgets\Navbar; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
use Illuminate\Support\Facades\Auth; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Admin. |
18
|
|
|
*/ |
19
|
|
|
class Admin |
20
|
|
|
{ |
21
|
|
|
use HasAssets; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The Laravel admin version. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const VERSION = '1.7.0'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Navbar |
32
|
|
|
*/ |
33
|
|
|
protected $navbar; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $menu = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public static $metaTitle; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public static $favicon; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public static $extensions = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var []Closure |
57
|
|
|
*/ |
58
|
|
|
protected static $bootingCallbacks = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var []Closure |
62
|
|
|
*/ |
63
|
|
|
protected static $bootedCallbacks = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the long version of Laravel-admin. |
67
|
|
|
* |
68
|
|
|
* @return string The long application version |
69
|
|
|
*/ |
70
|
|
|
public static function getLongVersion() |
71
|
|
|
{ |
72
|
|
|
return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $model |
77
|
|
|
* @param Closure $callable |
78
|
|
|
* |
79
|
|
|
* @return \Encore\Admin\Grid |
80
|
|
|
* |
81
|
|
|
* @deprecated since v1.6.1 |
82
|
|
|
*/ |
83
|
|
|
public function grid($model, Closure $callable) |
84
|
|
|
{ |
85
|
|
|
return new Grid($this->getModel($model), $callable); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $model |
90
|
|
|
* @param Closure $callable |
91
|
|
|
* |
92
|
|
|
* @return \Encore\Admin\Form |
93
|
|
|
* |
94
|
|
|
* @deprecated since v1.6.1 |
95
|
|
|
*/ |
96
|
|
|
public function form($model, Closure $callable) |
97
|
|
|
{ |
98
|
|
|
return new Form($this->getModel($model), $callable); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build a tree. |
103
|
|
|
* |
104
|
|
|
* @param $model |
105
|
|
|
* @param Closure|null $callable |
106
|
|
|
* |
107
|
|
|
* @return \Encore\Admin\Tree |
108
|
|
|
*/ |
109
|
|
|
public function tree($model, Closure $callable = null) |
110
|
|
|
{ |
111
|
|
|
return new Tree($this->getModel($model), $callable); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Build show page. |
116
|
|
|
* |
117
|
|
|
* @param $model |
118
|
|
|
* @param mixed $callable |
119
|
|
|
* |
120
|
|
|
* @return Show |
121
|
|
|
* |
122
|
|
|
* @deprecated since v1.6.1 |
123
|
|
|
*/ |
124
|
|
|
public function show($model, $callable = null) |
125
|
|
|
{ |
126
|
|
|
return new Show($this->getModel($model), $callable); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Closure $callable |
131
|
|
|
* |
132
|
|
|
* @return \Encore\Admin\Layout\Content |
133
|
|
|
* |
134
|
|
|
* @deprecated since v1.6.1 |
135
|
|
|
*/ |
136
|
|
|
public function content(Closure $callable = null) |
137
|
|
|
{ |
138
|
|
|
return new Content($callable); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $model |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
|
|
public function getModel($model) |
147
|
|
|
{ |
148
|
|
|
if ($model instanceof Model) { |
149
|
|
|
return $model; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (is_string($model) && class_exists($model)) { |
153
|
|
|
return $this->getModel(new $model()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new InvalidArgumentException("$model is not a valid model"); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Left sider-bar menu. |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function menu() |
165
|
|
|
{ |
166
|
|
|
if (!empty($this->menu)) { |
167
|
|
|
return $this->menu; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$menuClass = config('admin.database.menu_model'); |
171
|
|
|
|
172
|
|
|
/** @var Menu $menuModel */ |
173
|
|
|
$menuModel = new $menuClass(); |
174
|
|
|
|
175
|
|
|
return $this->menu = $menuModel->toTree(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $menu |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
public function menuLinks($menu = []) |
184
|
|
|
{ |
185
|
|
|
if (empty($menu)) { |
186
|
|
|
$menu = $this->menu(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$links = []; |
190
|
|
|
|
191
|
|
|
foreach ($menu as $item) { |
192
|
|
|
if (!empty($item['children'])) { |
193
|
|
|
$links = array_merge($links, $this->menuLinks($item['children'])); |
194
|
|
|
} else { |
195
|
|
|
$links[] = Arr::only($item, ['title', 'uri', 'icon']); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $links; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set admin title. |
204
|
|
|
* |
205
|
|
|
* @param string $title |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public static function setTitle($title) |
210
|
|
|
{ |
211
|
|
|
self::$metaTitle = $title; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get admin title. |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function title() |
220
|
|
|
{ |
221
|
|
|
return self::$metaTitle ? self::$metaTitle : config('admin.title'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param null|string $favicon |
226
|
|
|
* |
227
|
|
|
* @return string|void |
228
|
|
|
*/ |
229
|
|
|
public function favicon($favicon = null) |
230
|
|
|
{ |
231
|
|
|
if (is_null($favicon)) { |
232
|
|
|
return static::$favicon; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
static::$favicon = $favicon; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the currently authenticated user. |
240
|
|
|
* |
241
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
242
|
|
|
*/ |
243
|
|
|
public function user() |
244
|
|
|
{ |
245
|
|
|
return $this->guard()->user(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Attempt to get the guard from the local cache. |
250
|
|
|
* |
251
|
|
|
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard |
252
|
|
|
*/ |
253
|
|
|
public function guard() |
254
|
|
|
{ |
255
|
|
|
$guard = config('admin.auth.guard') ?: 'admin'; |
256
|
|
|
|
257
|
|
|
return Auth::guard($guard); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set navbar. |
262
|
|
|
* |
263
|
|
|
* @param Closure|null $builder |
264
|
|
|
* |
265
|
|
|
* @return Navbar |
266
|
|
|
*/ |
267
|
|
|
public function navbar(Closure $builder = null) |
268
|
|
|
{ |
269
|
|
|
if (is_null($builder)) { |
270
|
|
|
return $this->getNavbar(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
call_user_func($builder, $this->getNavbar()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get navbar object. |
278
|
|
|
* |
279
|
|
|
* @return \Encore\Admin\Widgets\Navbar |
280
|
|
|
*/ |
281
|
|
|
public function getNavbar() |
282
|
|
|
{ |
283
|
|
|
if (is_null($this->navbar)) { |
284
|
|
|
$this->navbar = new Navbar(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this->navbar; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Register the laravel-admin builtin routes. |
292
|
|
|
* |
293
|
|
|
* @return void |
294
|
|
|
* |
295
|
|
|
* @deprecated Use Admin::routes() instead(); |
296
|
|
|
*/ |
297
|
|
|
public function registerAuthRoutes() |
298
|
|
|
{ |
299
|
|
|
$this->routes(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Register the laravel-admin builtin routes. |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function routes() |
308
|
|
|
{ |
309
|
|
|
$attributes = [ |
310
|
|
|
'prefix' => config('admin.route.prefix'), |
311
|
|
|
'middleware' => config('admin.route.middleware'), |
312
|
|
|
]; |
313
|
|
|
|
314
|
|
|
app('router')->group($attributes, function ($router) { |
315
|
|
|
|
316
|
|
|
/* @var \Illuminate\Support\Facades\Route $router */ |
317
|
|
|
$router->namespace('\Encore\Admin\Controllers')->group(function ($router) { |
318
|
|
|
|
319
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
320
|
|
|
$router->resource('auth/users', 'UserController')->names('admin.auth.users'); |
321
|
|
|
$router->resource('auth/roles', 'RoleController')->names('admin.auth.roles'); |
322
|
|
|
$router->resource('auth/permissions', 'PermissionController')->names('admin.auth.permissions'); |
323
|
|
|
$router->resource('auth/menu', 'MenuController', ['except' => ['create']])->names('admin.auth.menu'); |
324
|
|
|
$router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']])->names('admin.auth.logs'); |
325
|
|
|
|
326
|
|
|
$router->post('_handle_form_', 'HandleController@handleForm')->name('admin.handle-form'); |
327
|
|
|
}); |
328
|
|
|
|
329
|
|
|
$authController = config('admin.auth.controller', AuthController::class); |
330
|
|
|
|
331
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
332
|
|
|
$router->get('auth/login', $authController.'@getLogin')->name('admin.login'); |
333
|
|
|
$router->post('auth/login', $authController.'@postLogin'); |
334
|
|
|
$router->get('auth/logout', $authController.'@getLogout')->name('admin.logout'); |
335
|
|
|
$router->get('auth/setting', $authController.'@getSetting')->name('admin.setting'); |
336
|
|
|
$router->put('auth/setting', $authController.'@putSetting'); |
337
|
|
|
}); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Extend a extension. |
342
|
|
|
* |
343
|
|
|
* @param string $name |
344
|
|
|
* @param string $class |
345
|
|
|
* |
346
|
|
|
* @return void |
347
|
|
|
*/ |
348
|
|
|
public static function extend($name, $class) |
349
|
|
|
{ |
350
|
|
|
static::$extensions[$name] = $class; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param callable $callback |
355
|
|
|
*/ |
356
|
|
|
public static function booting(callable $callback) |
357
|
|
|
{ |
358
|
|
|
static::$bootingCallbacks[] = $callback; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param callable $callback |
363
|
|
|
*/ |
364
|
|
|
public static function booted(callable $callback) |
365
|
|
|
{ |
366
|
|
|
static::$bootedCallbacks[] = $callback; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Bootstrap the admin application. |
371
|
|
|
*/ |
372
|
|
|
public function bootstrap() |
373
|
|
|
{ |
374
|
|
|
$this->fireBootingCallbacks(); |
375
|
|
|
|
376
|
|
|
Form::registerBuiltinFields(); |
377
|
|
|
|
378
|
|
|
Grid::registerColumnDisplayer(); |
379
|
|
|
|
380
|
|
|
Grid\Filter::registerFilters(); |
381
|
|
|
|
382
|
|
|
require config('admin.bootstrap', admin_path('bootstrap.php')); |
383
|
|
|
|
384
|
|
|
$assets = Form::collectFieldAssets(); |
385
|
|
|
|
386
|
|
|
self::css($assets['css']); |
387
|
|
|
self::js($assets['js']); |
388
|
|
|
|
389
|
|
|
$this->fireBootedCallbacks(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Call the booting callbacks for the admin application. |
394
|
|
|
*/ |
395
|
|
|
protected function fireBootingCallbacks() |
396
|
|
|
{ |
397
|
|
|
foreach (static::$bootingCallbacks as $callable) { |
398
|
|
|
call_user_func($callable); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Call the booted callbacks for the admin application. |
404
|
|
|
*/ |
405
|
|
|
protected function fireBootedCallbacks() |
406
|
|
|
{ |
407
|
|
|
foreach (static::$bootedCallbacks as $callable) { |
408
|
|
|
call_user_func($callable); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/* |
413
|
|
|
* Disable Pjax for current Request |
414
|
|
|
* |
415
|
|
|
* @return void |
416
|
|
|
*/ |
417
|
|
|
public function disablePjax() |
418
|
|
|
{ |
419
|
|
|
if (request()->pjax()) { |
420
|
|
|
request()->headers->set('X-PJAX', false); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|