1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Encore\Admin\Auth\Database\Menu; |
7
|
|
|
use Encore\Admin\Layout\Content; |
8
|
|
|
use Encore\Admin\Widgets\Navbar; |
9
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
use Illuminate\Support\Facades\Config; |
12
|
|
|
use Illuminate\Support\Facades\Request; |
13
|
|
|
use Illuminate\Support\Facades\Route; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Admin. |
18
|
|
|
*/ |
19
|
|
|
class Admin |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The Laravel admin version. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const VERSION = '1.5.19'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Navbar |
30
|
|
|
*/ |
31
|
|
|
protected $navbar; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public static $script = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public static $css = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public static $js = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
public static $extensions = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the long version of Laravel-admin. |
55
|
|
|
* |
56
|
|
|
* @return string The long application version |
57
|
|
|
*/ |
58
|
|
|
public static function getLongVersion() |
59
|
|
|
{ |
60
|
|
|
return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $model |
65
|
|
|
* @param Closure $callable |
66
|
|
|
* |
67
|
|
|
* @return \Encore\Admin\Grid |
68
|
|
|
*/ |
69
|
|
|
public function grid($model, Closure $callable) |
70
|
|
|
{ |
71
|
|
|
return new Grid($this->getModel($model), $callable); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $model |
76
|
|
|
* @param Closure $callable |
77
|
|
|
* |
78
|
|
|
* @return \Encore\Admin\Form |
79
|
|
|
*/ |
80
|
|
|
public function form($model, Closure $callable) |
81
|
|
|
{ |
82
|
|
|
return new Form($this->getModel($model), $callable); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build a tree. |
87
|
|
|
* |
88
|
|
|
* @param $model |
89
|
|
|
* |
90
|
|
|
* @return \Encore\Admin\Tree |
91
|
|
|
*/ |
92
|
|
|
public function tree($model, Closure $callable = null) |
93
|
|
|
{ |
94
|
|
|
return new Tree($this->getModel($model), $callable); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Build show page. |
99
|
|
|
* |
100
|
|
|
* @param $model |
101
|
|
|
* @param mixed $callable |
102
|
|
|
* |
103
|
|
|
* @return Show |
104
|
|
|
*/ |
105
|
|
|
public function show($model, $callable = null) |
106
|
|
|
{ |
107
|
|
|
return new Show($this->getModel($model), $callable); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Closure $callable |
112
|
|
|
* |
113
|
|
|
* @return \Encore\Admin\Layout\Content |
114
|
|
|
*/ |
115
|
|
|
public function content(Closure $callable = null) |
116
|
|
|
{ |
117
|
|
|
return new Content($callable); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $model |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function getModel($model) |
126
|
|
|
{ |
127
|
|
|
if ($model instanceof EloquentModel) { |
128
|
|
|
return $model; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (is_string($model) && class_exists($model)) { |
132
|
|
|
return $this->getModel(new $model()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw new InvalidArgumentException("$model is not a valid model"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add css or get all css. |
140
|
|
|
* |
141
|
|
|
* @param null $css |
142
|
|
|
* |
143
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public static function css($css = null) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
if (!is_null($css)) { |
148
|
|
|
self::$css = array_merge(self::$css, (array) $css); |
149
|
|
|
|
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$css = array_get(Form::collectFieldAssets(), 'css', []); |
154
|
|
|
|
155
|
|
|
static::$css = array_merge(static::$css, $css); |
156
|
|
|
|
157
|
|
|
return view('admin::partials.css', ['css' => array_unique(static::$css)]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Add js or get all js. |
162
|
|
|
* |
163
|
|
|
* @param null $js |
164
|
|
|
* |
165
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public static function js($js = null) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
if (!is_null($js)) { |
170
|
|
|
self::$js = array_merge(self::$js, (array) $js); |
171
|
|
|
|
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$js = array_get(Form::collectFieldAssets(), 'js', []); |
176
|
|
|
|
177
|
|
|
static::$js = array_merge(static::$js, $js); |
178
|
|
|
|
179
|
|
|
return view('admin::partials.js', ['js' => array_unique(static::$js)]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $script |
184
|
|
|
* |
185
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
186
|
|
|
*/ |
187
|
|
|
public static function script($script = '') |
188
|
|
|
{ |
189
|
|
|
if (!empty($script)) { |
190
|
|
|
self::$script = array_merge(self::$script, (array) $script); |
191
|
|
|
|
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return view('admin::partials.script', ['script' => array_unique(self::$script)]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Left sider-bar menu. |
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function menu() |
204
|
|
|
{ |
205
|
|
|
return (new Menu())->toTree(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get admin title. |
210
|
|
|
* |
211
|
|
|
* @return Config |
212
|
|
|
*/ |
213
|
|
|
public function title() |
214
|
|
|
{ |
215
|
|
|
return config('admin.title'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get current login user. |
220
|
|
|
* |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
|
|
public function user() |
224
|
|
|
{ |
225
|
|
|
return Auth::guard('admin')->user(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set navbar. |
230
|
|
|
* |
231
|
|
|
* @param Closure|null $builder |
232
|
|
|
* |
233
|
|
|
* @return Navbar |
234
|
|
|
*/ |
235
|
|
|
public function navbar(Closure $builder = null) |
236
|
|
|
{ |
237
|
|
|
if (is_null($builder)) { |
238
|
|
|
return $this->getNavbar(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
call_user_func($builder, $this->getNavbar()); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get navbar object. |
246
|
|
|
* |
247
|
|
|
* @return \Encore\Admin\Widgets\Navbar |
248
|
|
|
*/ |
249
|
|
|
public function getNavbar() |
250
|
|
|
{ |
251
|
|
|
if (is_null($this->navbar)) { |
252
|
|
|
$this->navbar = new Navbar(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->navbar; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Register the auth routes. |
260
|
|
|
* |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
|
public function registerAuthRoutes() |
264
|
|
|
{ |
265
|
|
|
$attributes = [ |
266
|
|
|
'prefix' => config('admin.route.prefix'), |
267
|
|
|
'namespace' => 'Encore\Admin\Controllers', |
268
|
|
|
'middleware' => config('admin.route.middleware'), |
269
|
|
|
]; |
270
|
|
|
|
271
|
|
|
Route::group($attributes, function ($router) { |
272
|
|
|
|
273
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
274
|
|
|
$router->group([], function ($router) { |
275
|
|
|
|
276
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
277
|
|
|
$router->resource('auth/users', 'UserController'); |
278
|
|
|
$router->resource('auth/roles', 'RoleController'); |
279
|
|
|
$router->resource('auth/permissions', 'PermissionController'); |
280
|
|
|
$router->resource('auth/menu', 'MenuController', ['except' => ['create']]); |
281
|
|
|
$router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]); |
282
|
|
|
}); |
283
|
|
|
|
284
|
|
|
$router->get('auth/login', 'AuthController@getLogin'); |
285
|
|
|
$router->post('auth/login', 'AuthController@postLogin'); |
286
|
|
|
$router->get('auth/logout', 'AuthController@getLogout'); |
287
|
|
|
$router->get('auth/setting', 'AuthController@getSetting'); |
288
|
|
|
$router->put('auth/setting', 'AuthController@putSetting'); |
289
|
|
|
}); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Extend a extension. |
294
|
|
|
* |
295
|
|
|
* @param string $name |
296
|
|
|
* @param string $class |
297
|
|
|
* |
298
|
|
|
* @return void |
299
|
|
|
*/ |
300
|
|
|
public static function extend($name, $class) |
301
|
|
|
{ |
302
|
|
|
static::$extensions[$name] = $class; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/* |
306
|
|
|
* Disable Pjax for current Request |
307
|
|
|
* |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
public function disablePjax() |
311
|
|
|
{ |
312
|
|
|
$request = Request::instance(); |
313
|
|
|
|
314
|
|
|
if ($request->pjax()) { |
315
|
|
|
$request->headers->set('X-PJAX', false); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.