1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Traits; |
4
|
|
|
|
5
|
|
|
trait HasAssets |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
public static $script = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public static $deferredScript = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public static $style = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public static $css = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public static $js = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public static $html = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public static $headerJs = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public static $manifest = 'vendor/laravel-admin/minify-manifest.json'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
public static $manifestData = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
public static $min = [ |
56
|
|
|
'js' => 'vendor/laravel-admin/laravel-admin.min.js', |
57
|
|
|
'css' => 'vendor/laravel-admin/laravel-admin.min.css', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public static $baseCss = [ |
64
|
|
|
'vendor/laravel-admin/AdminLTE/bootstrap/css/bootstrap.min.css', |
65
|
|
|
'vendor/laravel-admin/font-awesome/css/font-awesome.min.css', |
66
|
|
|
'vendor/laravel-admin/laravel-admin/laravel-admin.css', |
67
|
|
|
'vendor/laravel-admin/nprogress/nprogress.css', |
68
|
|
|
'vendor/laravel-admin/sweetalert2/dist/sweetalert2.css', |
69
|
|
|
'vendor/laravel-admin/nestable/nestable.css', |
70
|
|
|
'vendor/laravel-admin/toastr/build/toastr.min.css', |
71
|
|
|
'vendor/laravel-admin/bootstrap3-editable/css/bootstrap-editable.css', |
72
|
|
|
'vendor/laravel-admin/google-fonts/fonts.css', |
73
|
|
|
'vendor/laravel-admin/AdminLTE/dist/css/AdminLTE.min.css', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
public static $baseJs = [ |
80
|
|
|
'vendor/laravel-admin/AdminLTE/bootstrap/js/bootstrap.min.js', |
81
|
|
|
'vendor/laravel-admin/AdminLTE/plugins/slimScroll/jquery.slimscroll.min.js', |
82
|
|
|
'vendor/laravel-admin/AdminLTE/dist/js/app.min.js', |
83
|
|
|
'vendor/laravel-admin/jquery-pjax/jquery.pjax.js', |
84
|
|
|
'vendor/laravel-admin/nprogress/nprogress.js', |
85
|
|
|
'vendor/laravel-admin/nestable/jquery.nestable.js', |
86
|
|
|
'vendor/laravel-admin/toastr/build/toastr.min.js', |
87
|
|
|
'vendor/laravel-admin/bootstrap3-editable/js/bootstrap-editable.min.js', |
88
|
|
|
'vendor/laravel-admin/sweetalert2/dist/sweetalert2.min.js', |
89
|
|
|
'vendor/laravel-admin/laravel-admin/laravel-admin.js', |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
public static $jQuery = 'vendor/laravel-admin/AdminLTE/plugins/jQuery/jQuery-2.1.4.min.js'; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
public static $minifyIgnores = []; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add css or get all css. |
104
|
|
|
* |
105
|
|
|
* @param null $css |
106
|
|
|
* @param bool $minify |
107
|
|
|
* |
108
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public static function css($css = null, $minify = true) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
static::ignoreMinify($css, $minify); |
113
|
|
|
|
114
|
|
|
if (!is_null($css)) { |
115
|
|
|
return self::$css = array_merge(self::$css, (array) $css); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$css = static::getMinifiedCss()) { |
119
|
|
|
$css = array_merge(static::$css, static::baseCss()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$css = array_filter(array_unique($css)); |
123
|
|
|
|
124
|
|
|
return view('admin::partials.css', compact('css')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param null $css |
129
|
|
|
* @param bool $minify |
130
|
|
|
* |
131
|
|
|
* @return array|null |
132
|
|
|
*/ |
133
|
|
|
public static function baseCss($css = null, $minify = true) |
134
|
|
|
{ |
135
|
|
|
static::ignoreMinify($css, $minify); |
136
|
|
|
|
137
|
|
|
if (!is_null($css)) { |
138
|
|
|
return static::$baseCss = $css; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$skin = config('admin.skin', 'skin-blue-light'); |
142
|
|
|
|
143
|
|
|
array_unshift(static::$baseCss, "vendor/laravel-admin/AdminLTE/dist/css/skins/{$skin}.min.css"); |
144
|
|
|
|
145
|
|
|
return static::$baseCss; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add js or get all js. |
150
|
|
|
* |
151
|
|
|
* @param null $js |
152
|
|
|
* @param bool $minify |
153
|
|
|
* |
154
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public static function js($js = null, $minify = true) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
static::ignoreMinify($js, $minify); |
159
|
|
|
|
160
|
|
|
if (!is_null($js)) { |
161
|
|
|
return self::$js = array_merge(self::$js, (array) $js); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!$js = static::getMinifiedJs()) { |
165
|
|
|
$js = array_merge(static::baseJs(), static::$js); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$js = array_filter(array_unique($js)); |
169
|
|
|
|
170
|
|
|
return view('admin::partials.js', compact('js')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add js or get all js. |
175
|
|
|
* |
176
|
|
|
* @param null $js |
177
|
|
|
* |
178
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
179
|
|
|
*/ |
180
|
|
|
public static function headerJs($js = null) |
181
|
|
|
{ |
182
|
|
|
if (!is_null($js)) { |
183
|
|
|
return self::$headerJs = array_merge(self::$headerJs, (array) $js); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return view('admin::partials.js', ['js' => array_unique(static::$headerJs)]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param null $js |
191
|
|
|
* @param bool $minify |
192
|
|
|
* |
193
|
|
|
* @return array|null |
194
|
|
|
*/ |
195
|
|
|
public static function baseJs($js = null, $minify = true) |
196
|
|
|
{ |
197
|
|
|
static::ignoreMinify($js, $minify); |
198
|
|
|
|
199
|
|
|
if (!is_null($js)) { |
200
|
|
|
return static::$baseJs = $js; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return static::$baseJs; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $assets |
208
|
|
|
* @param bool $ignore |
209
|
|
|
*/ |
210
|
|
|
public static function ignoreMinify($assets, $ignore = true) |
211
|
|
|
{ |
212
|
|
|
if (!$ignore) { |
213
|
|
|
static::$minifyIgnores[] = $assets; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $script |
219
|
|
|
* @param bool $deferred |
220
|
|
|
* |
221
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
222
|
|
|
*/ |
223
|
|
|
public static function script($script = '', $deferred = false) |
224
|
|
|
{ |
225
|
|
|
if (!empty($script)) { |
226
|
|
|
if ($deferred) { |
227
|
|
|
return self::$deferredScript = array_merge(self::$deferredScript, (array) $script); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return self::$script = array_merge(self::$script, (array) $script); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$script = array_unique(array_merge(static::$script, static::$deferredScript)); |
234
|
|
|
|
235
|
|
|
return view('admin::partials.script', compact('script')); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $style |
240
|
|
|
* |
241
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
242
|
|
|
*/ |
243
|
|
View Code Duplication |
public static function style($style = '') |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
if (!empty($style)) { |
246
|
|
|
return self::$style = array_merge(self::$style, (array) $style); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return view('admin::partials.style', ['style' => array_unique(self::$style)]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $html |
254
|
|
|
* |
255
|
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public static function html($html = '') |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
if (!empty($html)) { |
260
|
|
|
return self::$html = array_merge(self::$html, (array) $html); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return view('admin::partials.html', ['html' => array_unique(self::$html)]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $key |
268
|
|
|
* |
269
|
|
|
* @return mixed |
270
|
|
|
*/ |
271
|
|
|
protected static function getManifestData($key) |
272
|
|
|
{ |
273
|
|
|
if (!empty(static::$manifestData)) { |
274
|
|
|
return static::$manifestData[$key]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
static::$manifestData = json_decode( |
278
|
|
|
file_get_contents(public_path(static::$manifest)), |
279
|
|
|
true |
280
|
|
|
); |
281
|
|
|
|
282
|
|
|
return static::$manifestData[$key]; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return bool|mixed |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
protected static function getMinifiedCss() |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
if (!config('admin.minify_assets') || !file_exists(public_path(static::$manifest))) { |
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return static::getManifestData('css'); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return bool|mixed |
299
|
|
|
*/ |
300
|
|
View Code Duplication |
protected static function getMinifiedJs() |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
if (!config('admin.minify_assets') || !file_exists(public_path(static::$manifest))) { |
303
|
|
|
return false; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return static::getManifestData('js'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return string |
311
|
|
|
*/ |
312
|
|
|
public function jQuery() |
313
|
|
|
{ |
314
|
|
|
return admin_asset(static::$jQuery); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param $component |
319
|
|
|
*/ |
320
|
|
|
public static function component($component, $data = []) |
321
|
|
|
{ |
322
|
|
|
$str = view($component, $data)->render(); |
|
|
|
|
323
|
|
|
|
324
|
|
|
$dom = new \DOMDocument(); |
325
|
|
|
|
326
|
|
|
libxml_use_internal_errors(true); |
327
|
|
|
|
328
|
|
|
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $str); |
329
|
|
|
|
330
|
|
|
libxml_use_internal_errors(false); |
331
|
|
|
|
332
|
|
|
if ($dom->getElementsByTagName('body')->length <= 0) { |
333
|
|
|
return; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$body = $dom->getElementsByTagName('body')[0]; |
337
|
|
|
|
338
|
|
|
$render = ''; |
339
|
|
|
|
340
|
|
|
foreach ($body->childNodes as $child) { |
341
|
|
|
|
342
|
|
|
if ($child instanceof \DOMElement && $child->tagName == 'style') { |
343
|
|
|
static::style($child->nodeValue); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if ($child instanceof \DOMElement && $child->tagName == 'script') { |
347
|
|
|
static::script(';(function () {' . $child->nodeValue . '})();'); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if ($child instanceof \DOMElement && $child->tagName == 'template') { |
351
|
|
|
|
352
|
|
|
$html = ''; |
353
|
|
|
|
354
|
|
|
foreach ($child->childNodes as $childNode) { |
355
|
|
|
$html .= $child->ownerDocument->saveHTML($childNode); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if ($html) { |
359
|
|
|
static::html($html); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
if ($child instanceof \DOMElement && in_array($child->tagName, ['template', 'style', 'script'])) { |
364
|
|
|
continue; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
$render .= $body->ownerDocument->saveHTML($child); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $render; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
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.