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 Illuminate\Database\Eloquent\Model as EloquentModel; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Support\Facades\Config; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Admin. |
15
|
|
|
*/ |
16
|
|
|
class Admin |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public static $script = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public static $css = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public static $js = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected static $initialized = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected static $bootstrapped = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initialize. |
45
|
|
|
*/ |
46
|
|
|
public static function init() |
47
|
|
|
{ |
48
|
|
|
if (!static::$initialized) { |
49
|
|
|
Form::registerBuiltinFields(); |
50
|
|
|
|
51
|
|
|
static::$initialized = true; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Bootstrap. |
57
|
|
|
*/ |
58
|
|
|
public static function bootstrap() |
59
|
|
|
{ |
60
|
|
|
if (!static::$bootstrapped) { |
61
|
|
|
if (file_exists($bootstrap = admin_path('bootstrap.php'))) { |
62
|
|
|
require $bootstrap; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
static::$bootstrapped = true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $model |
71
|
|
|
* @param Closure $callable |
72
|
|
|
* |
73
|
|
|
* @return Grid |
74
|
|
|
*/ |
75
|
|
|
public function grid($model, Closure $callable) |
76
|
|
|
{ |
77
|
|
|
return new Grid($this->getModel($model), $callable); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $model |
82
|
|
|
* @param Closure $callable |
83
|
|
|
* |
84
|
|
|
* @return Form |
85
|
|
|
*/ |
86
|
|
|
public function form($model, Closure $callable) |
87
|
|
|
{ |
88
|
|
|
static::init(); |
89
|
|
|
static::bootstrap(); |
90
|
|
|
|
91
|
|
|
return new Form($this->getModel($model), $callable); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Build a tree. |
96
|
|
|
* |
97
|
|
|
* @param $model |
98
|
|
|
* |
99
|
|
|
* @return Tree |
100
|
|
|
*/ |
101
|
|
|
public function tree($model) |
102
|
|
|
{ |
103
|
|
|
return new Tree($this->getModel($model)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Closure $callable |
108
|
|
|
* |
109
|
|
|
* @return Content |
110
|
|
|
*/ |
111
|
|
|
public function content(Closure $callable) |
112
|
|
|
{ |
113
|
|
|
static::init(); |
114
|
|
|
static::bootstrap(); |
115
|
|
|
|
116
|
|
|
Form::collectFieldAssets(); |
117
|
|
|
|
118
|
|
|
return new Content($callable); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $model |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function getModel($model) |
127
|
|
|
{ |
128
|
|
|
if ($model instanceof EloquentModel) { |
129
|
|
|
return $model; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (is_string($model) && class_exists($model)) { |
133
|
|
|
return $this->getModel(new $model()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
throw new InvalidArgumentException("$model is not a valid model"); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get namespace of controllers. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function controllerNamespace() |
145
|
|
|
{ |
146
|
|
|
$directory = config('admin.directory'); |
147
|
|
|
|
148
|
|
|
return 'App\\'.ucfirst(basename($directory)).'\\Controllers'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add css or get all css. |
153
|
|
|
* |
154
|
|
|
* @param null $css |
155
|
|
|
* |
156
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public static function css($css = null) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
if (!is_null($css)) { |
161
|
|
|
self::$css = array_merge(self::$css, (array) $css); |
162
|
|
|
|
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$css = array_get(Form::collectFieldAssets(), 'css', []); |
167
|
|
|
|
168
|
|
|
static::$css = array_merge(static::$css, $css); |
169
|
|
|
|
170
|
|
|
return view('admin::partials.css', ['css' => array_unique(static::$css)]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add js or get all js. |
175
|
|
|
* |
176
|
|
|
* @param null $js |
177
|
|
|
* |
178
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public static function js($js = null) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
if (!is_null($js)) { |
183
|
|
|
self::$js = array_merge(self::$js, (array) $js); |
184
|
|
|
|
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$js = array_get(Form::collectFieldAssets(), 'js', []); |
189
|
|
|
|
190
|
|
|
static::$js = array_merge(static::$js, $js); |
191
|
|
|
|
192
|
|
|
return view('admin::partials.js', ['js' => array_unique(static::$js)]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $script |
197
|
|
|
* |
198
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
199
|
|
|
*/ |
200
|
|
|
public static function script($script = '') |
201
|
|
|
{ |
202
|
|
|
if (!empty($script)) { |
203
|
|
|
self::$script = array_merge(self::$script, (array) $script); |
204
|
|
|
|
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return view('admin::partials.script', ['script' => array_unique(self::$script)]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Admin url. |
213
|
|
|
* |
214
|
|
|
* @param $url |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public static function url($url) |
219
|
|
|
{ |
220
|
|
|
$prefix = (string) config('admin.prefix'); |
221
|
|
|
|
222
|
|
|
if (empty($prefix) || $prefix == '/') { |
223
|
|
|
return '/'.trim($url, '/'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return "/$prefix/".trim($url, '/'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Left sider-bar menu. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function menu() |
235
|
|
|
{ |
236
|
|
|
return Menu::toTree(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get admin title. |
241
|
|
|
* |
242
|
|
|
* @return Config |
243
|
|
|
*/ |
244
|
|
|
public function title() |
245
|
|
|
{ |
246
|
|
|
return config('admin.title'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
|
|
public function user() |
253
|
|
|
{ |
254
|
|
|
return Auth::guard('admin')->user(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
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.