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\Traits\HasAssets; |
9
|
|
|
use Encore\Admin\Widgets\Navbar; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use Illuminate\Support\Facades\Config; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Admin. |
17
|
|
|
*/ |
18
|
|
|
class Admin |
19
|
|
|
{ |
20
|
|
|
use HasAssets; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Laravel admin version. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const VERSION = '1.6.0'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Navbar |
31
|
|
|
*/ |
32
|
|
|
protected $navbar; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public static $extensions = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var []Closure |
41
|
|
|
*/ |
42
|
|
|
public static $booting; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var []Closure |
46
|
|
|
*/ |
47
|
|
|
public static $booted; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the long version of Laravel-admin. |
51
|
|
|
* |
52
|
|
|
* @return string The long application version |
53
|
|
|
*/ |
54
|
|
|
public static function getLongVersion() |
55
|
|
|
{ |
56
|
|
|
return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $model |
61
|
|
|
* @param Closure $callable |
62
|
|
|
* |
63
|
|
|
* @return \Encore\Admin\Grid |
64
|
|
|
* |
65
|
|
|
* @deprecated since v1.6.1 |
66
|
|
|
*/ |
67
|
|
|
public function grid($model, Closure $callable) |
68
|
|
|
{ |
69
|
|
|
return new Grid($this->getModel($model), $callable); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $model |
74
|
|
|
* @param Closure $callable |
75
|
|
|
* |
76
|
|
|
* @return \Encore\Admin\Form |
77
|
|
|
* |
78
|
|
|
* @deprecated since v1.6.1 |
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
|
|
|
* @deprecated since v1.6.1 |
106
|
|
|
*/ |
107
|
|
|
public function show($model, $callable = null) |
108
|
|
|
{ |
109
|
|
|
return new Show($this->getModel($model), $callable); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Closure $callable |
114
|
|
|
* |
115
|
|
|
* @return \Encore\Admin\Layout\Content |
116
|
|
|
* |
117
|
|
|
* @deprecated since v1.6.1 |
118
|
|
|
*/ |
119
|
|
|
public function content(Closure $callable = null) |
120
|
|
|
{ |
121
|
|
|
return new Content($callable); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $model |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function getModel($model) |
130
|
|
|
{ |
131
|
|
|
if ($model instanceof Model) { |
132
|
|
|
return $model; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (is_string($model) && class_exists($model)) { |
136
|
|
|
return $this->getModel(new $model()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
throw new InvalidArgumentException("$model is not a valid model"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Left sider-bar menu. |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function menu() |
148
|
|
|
{ |
149
|
|
|
return (new Menu())->toTree(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get admin title. |
154
|
|
|
* |
155
|
|
|
* @return Config |
156
|
|
|
*/ |
157
|
|
|
public function title() |
158
|
|
|
{ |
159
|
|
|
return config('admin.title'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get current login user. |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function user() |
168
|
|
|
{ |
169
|
|
|
return Auth::guard('admin')->user(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set navbar. |
174
|
|
|
* |
175
|
|
|
* @param Closure|null $builder |
176
|
|
|
* |
177
|
|
|
* @return Navbar |
178
|
|
|
*/ |
179
|
|
|
public function navbar(Closure $builder = null) |
180
|
|
|
{ |
181
|
|
|
if (is_null($builder)) { |
182
|
|
|
return $this->getNavbar(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
call_user_func($builder, $this->getNavbar()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get navbar object. |
190
|
|
|
* |
191
|
|
|
* @return \Encore\Admin\Widgets\Navbar |
192
|
|
|
*/ |
193
|
|
|
public function getNavbar() |
194
|
|
|
{ |
195
|
|
|
if (is_null($this->navbar)) { |
196
|
|
|
$this->navbar = new Navbar(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->navbar; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Register the auth routes. |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function registerAuthRoutes() |
208
|
|
|
{ |
209
|
|
|
$attributes = [ |
210
|
|
|
'prefix' => config('admin.route.prefix'), |
211
|
|
|
'namespace' => 'Encore\Admin\Controllers', |
212
|
|
|
'middleware' => config('admin.route.middleware'), |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
app('router')->group($attributes, function ($router) { |
216
|
|
|
|
217
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
218
|
|
|
$router->group([], function ($router) { |
219
|
|
|
|
220
|
|
|
/* @var \Illuminate\Routing\Router $router */ |
221
|
|
|
$router->resource('auth/users', 'UserController'); |
222
|
|
|
$router->resource('auth/roles', 'RoleController'); |
223
|
|
|
$router->resource('auth/permissions', 'PermissionController'); |
224
|
|
|
$router->resource('auth/menu', 'MenuController', ['except' => ['create']]); |
225
|
|
|
$router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]); |
226
|
|
|
}); |
227
|
|
|
|
228
|
|
|
$router->get('auth/login', 'AuthController@getLogin'); |
229
|
|
|
$router->post('auth/login', 'AuthController@postLogin'); |
230
|
|
|
$router->get('auth/logout', 'AuthController@getLogout'); |
231
|
|
|
$router->get('auth/setting', 'AuthController@getSetting'); |
232
|
|
|
$router->put('auth/setting', 'AuthController@putSetting'); |
233
|
|
|
}); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Extend a extension. |
238
|
|
|
* |
239
|
|
|
* @param string $name |
240
|
|
|
* @param string $class |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public static function extend($name, $class) |
245
|
|
|
{ |
246
|
|
|
static::$extensions[$name] = $class; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param callable $callback |
251
|
|
|
*/ |
252
|
|
|
public static function booting(callable $callback) |
253
|
|
|
{ |
254
|
|
|
static::$booting[] = $callback; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param callable $callback |
259
|
|
|
*/ |
260
|
|
|
public static function booted(callable $callback) |
261
|
|
|
{ |
262
|
|
|
static::$booted[] = $callback; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/* |
266
|
|
|
* Disable Pjax for current Request |
267
|
|
|
* |
268
|
|
|
* @return void |
269
|
|
|
*/ |
270
|
|
|
public function disablePjax() |
271
|
|
|
{ |
272
|
|
|
if (request()->pjax()) { |
273
|
|
|
request()->headers->set('X-PJAX', false); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|