1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The all functions. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/maab16 |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Illuminate\Container\Container; |
10
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
|
|
|
|
11
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
12
|
|
|
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; |
13
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
14
|
|
|
use Illuminate\Contracts\Cookie\Factory as CookieFactory; |
15
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
16
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
17
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
18
|
|
|
// use Illuminate\Foundation\Bus\PendingDispatch; |
19
|
|
|
use Illuminate\Contracts\Support\Responsable; |
20
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory; |
21
|
|
|
use Illuminate\Database\Eloquent\Factory as EloquentFactory; |
22
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
|
|
|
|
23
|
|
|
use Illuminate\Support\Carbon; |
24
|
|
|
use Illuminate\Support\HtmlString; |
25
|
|
|
use Illuminate\Support\Str; |
26
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
27
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
|
|
|
28
|
|
|
use WPB\Application; |
29
|
|
|
|
30
|
|
|
if (!function_exists('wpb_csrf_token')) { |
31
|
|
|
/** |
32
|
|
|
* Generate wp nonce. |
33
|
|
|
* |
34
|
|
|
* @param string|null $action This is the nonce action name. |
35
|
|
|
* |
36
|
|
|
* @return null|string |
37
|
|
|
*/ |
38
|
|
|
function wpb_csrf_token($action = 'wpb_nonce') |
39
|
|
|
{ |
40
|
|
|
return wp_create_nonce($action); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!function_exists('wpb_config')) { |
45
|
|
|
/** |
46
|
|
|
* Get / set the specified configuration value. |
47
|
|
|
* |
48
|
|
|
* If an array is passed as the key, we will assume you want to set an array of values. |
49
|
|
|
* |
50
|
|
|
* @param array|string|null $key This is the key for config array. |
51
|
|
|
* @param mixed $default This is the default config value. |
52
|
|
|
* |
53
|
|
|
* @return mixed|\Illuminate\Config\Repository |
54
|
|
|
*/ |
55
|
|
|
function wpb_config($key = null, $default = null) |
56
|
|
|
{ |
57
|
|
|
if (is_null($key)) { |
58
|
|
|
return app('config'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (is_array($key)) { |
62
|
|
|
return app('config')->set($key); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return app('config')->get($key, $default); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!function_exists('wpb_view')) { |
70
|
|
|
/** |
71
|
|
|
* Render blade view. |
72
|
|
|
* |
73
|
|
|
* @param string $view This is the filename. |
74
|
|
|
* @param array $data This is the view data. |
75
|
|
|
* @param array $merge_data This is the merge data for view. |
76
|
|
|
* |
77
|
|
|
* @throws \Exception This will throw an exception if view class doesn't exists. |
78
|
|
|
* |
79
|
|
|
* @return null|string |
80
|
|
|
*/ |
81
|
|
|
function wpb_view($view, $data = [], $merge_data = []) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (!class_exists(\CodexShaper\Blade\View::class)) { |
84
|
|
|
throw new \Exception('View not resolved. Please install View'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return ( new \CodexShaper\Blade\View([__DIR__.'/../resources/views'], __DIR__.'/../storage/cache') )->make($view, $data = [], $merge_data = []); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!function_exists('wpb_abort')) { |
92
|
|
|
/** |
93
|
|
|
* Throw an HttpException with the given data. |
94
|
|
|
* |
95
|
|
|
* @param \Symfony\Component\HttpFoundation\Response|int $code |
96
|
|
|
* @param string $message |
97
|
|
|
* @param array $headers |
98
|
|
|
* |
99
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
100
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
function wpb_abort($code, $message = '', array $headers = []) |
105
|
|
|
{ |
106
|
|
|
if ($code instanceof Response) { |
107
|
|
|
throw new HttpResponseException($code); |
108
|
|
|
} elseif ($code instanceof Responsable) { |
|
|
|
|
109
|
|
|
throw new HttpResponseException($code->toResponse(request())); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
wpb_app()->abort($code, $message, $headers); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!function_exists('wpb_abort_if')) { |
117
|
|
|
/** |
118
|
|
|
* Throw an HttpException with the given data if the given condition is true. |
119
|
|
|
* |
120
|
|
|
* @param bool $boolean |
121
|
|
|
* @param int $code |
122
|
|
|
* @param string $message |
123
|
|
|
* @param array $headers |
124
|
|
|
* |
125
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
126
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
function wpb_abort_if($boolean, $code, $message = '', array $headers = []) |
131
|
|
|
{ |
132
|
|
|
if ($boolean) { |
133
|
|
|
wpb_abort($code, $message, $headers); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!function_exists('wpb_abort_unless')) { |
139
|
|
|
/** |
140
|
|
|
* Throw an HttpException with the given data unless the given condition is true. |
141
|
|
|
* |
142
|
|
|
* @param bool $boolean |
143
|
|
|
* @param int $code |
144
|
|
|
* @param string $message |
145
|
|
|
* @param array $headers |
146
|
|
|
* |
147
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
148
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
function wpb_abort_unless($boolean, $code, $message = '', array $headers = []) |
153
|
|
|
{ |
154
|
|
|
if (!$boolean) { |
155
|
|
|
wpb_abort($code, $message, $headers); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!function_exists('wpb_action')) { |
161
|
|
|
/** |
162
|
|
|
* Generate the URL to a controller action. |
163
|
|
|
* |
164
|
|
|
* @param string $name |
165
|
|
|
* @param mixed $parameters |
166
|
|
|
* @param bool $absolute |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
function wpb_action($name, $parameters = [], $absolute = true) |
171
|
|
|
{ |
172
|
|
|
return wpb_app('url')->action($name, $parameters, $absolute); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (!function_exists('wpb_app')) { |
177
|
|
|
/** |
178
|
|
|
* Get the available container instance. |
179
|
|
|
* |
180
|
|
|
* @param string $abstract |
181
|
|
|
* @param array $parameters |
182
|
|
|
* |
183
|
|
|
* @return mixed|\Illuminate\Foundation\Application |
|
|
|
|
184
|
|
|
*/ |
185
|
|
|
function wpb_app($abstract = null, array $parameters = []) |
186
|
|
|
{ |
187
|
|
|
global $wpb_app; |
188
|
|
|
|
189
|
|
|
if (is_null($abstract)) { |
190
|
|
|
return $wpb_app; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $wpb_app->make($abstract, $parameters); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (!function_exists('wpb_app_path')) { |
198
|
|
|
/** |
199
|
|
|
* Get the path to the application folder. |
200
|
|
|
* |
201
|
|
|
* @param string $path |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
function wpb_app_path($path = '') |
206
|
|
|
{ |
207
|
|
|
return wpb_app('path').($path ? DIRECTORY_SEPARATOR.$path : $path); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!function_exists('wpb_asset')) { |
212
|
|
|
/** |
213
|
|
|
* Generate an asset path for the application. |
214
|
|
|
* |
215
|
|
|
* @param string $path |
216
|
|
|
* @param bool $secure |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
function wpb_asset($path, $secure = null) |
221
|
|
|
{ |
222
|
|
|
return wpb_app('url')->asset($path, $secure); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (!function_exists('wpb_auth')) { |
227
|
|
|
/** |
228
|
|
|
* Get the available auth instance. |
229
|
|
|
* |
230
|
|
|
* @param string|null $guard |
231
|
|
|
* |
232
|
|
|
* @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard |
233
|
|
|
*/ |
234
|
|
|
function wpb_auth($guard = null) |
235
|
|
|
{ |
236
|
|
|
if (is_null($guard)) { |
237
|
|
|
return wpb_app(AuthFactory::class); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return wpb_app(AuthFactory::class)->guard($guard); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (!function_exists('wpb_back')) { |
245
|
|
|
/** |
246
|
|
|
* Create a new redirect response to the previous location. |
247
|
|
|
* |
248
|
|
|
* @param int $status |
249
|
|
|
* @param array $headers |
250
|
|
|
* @param mixed $fallback |
251
|
|
|
* |
252
|
|
|
* @return \Illuminate\Http\RedirectResponse |
253
|
|
|
*/ |
254
|
|
|
function wpb_back($status = 302, $headers = [], $fallback = false) |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
return wpb_app('redirect')->back($status, $headers, $fallback); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if (!function_exists('wpb_base_path')) { |
261
|
|
|
/** |
262
|
|
|
* Get the path to the base of the install. |
263
|
|
|
* |
264
|
|
|
* @param string $path |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
function wpb_base_path($path = '') |
269
|
|
|
{ |
270
|
|
|
return wpb_app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if (!function_exists('wpb_bcrypt')) { |
275
|
|
|
/** |
276
|
|
|
* Hash the given value against the bcrypt algorithm. |
277
|
|
|
* |
278
|
|
|
* @param string $value |
279
|
|
|
* @param array $options |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
function wpb_bcrypt($value, $options = []) |
284
|
|
|
{ |
285
|
|
|
return wpb_app('hash')->driver('bcrypt')->make($value, $options); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if (!function_exists('wpb_broadcast')) { |
290
|
|
|
/** |
291
|
|
|
* Begin broadcasting an event. |
292
|
|
|
* |
293
|
|
|
* @param mixed|null $event |
294
|
|
|
* |
295
|
|
|
* @return \Illuminate\Broadcasting\PendingBroadcast |
|
|
|
|
296
|
|
|
*/ |
297
|
|
|
function wpb_broadcast($event = null) |
298
|
|
|
{ |
299
|
|
|
return wpb_app(BroadcastFactory::class)->event($event); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (!function_exists('wpb_cache')) { |
304
|
|
|
/** |
305
|
|
|
* Get / set the specified cache value. |
306
|
|
|
* |
307
|
|
|
* If an array is passed, we'll assume you want to put to the cache. |
308
|
|
|
* |
309
|
|
|
* @param dynamic key|key,default|data,expiration|null |
|
|
|
|
310
|
|
|
* |
311
|
|
|
* @throws \Exception |
312
|
|
|
* |
313
|
|
|
* @return mixed|\Illuminate\Cache\CacheManager |
|
|
|
|
314
|
|
|
*/ |
315
|
|
|
function wpb_cache() |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
$arguments = func_get_args(); |
318
|
|
|
|
319
|
|
|
if (empty($arguments)) { |
320
|
|
|
return wpb_app('cache'); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
if (is_string($arguments[0])) { |
324
|
|
|
return wpb_app('cache')->get($arguments[0], $arguments[1] ?? null); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if (!is_array($arguments[0])) { |
328
|
|
|
throw new Exception( |
329
|
|
|
'When setting a value in the cache, you must pass an array of key / value pairs.' |
330
|
|
|
); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (!isset($arguments[1])) { |
334
|
|
|
throw new Exception( |
335
|
|
|
'You must specify an expiration time when setting a value in the cache.' |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return wpb_app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
if (!function_exists('wpb_config_path')) { |
344
|
|
|
/** |
345
|
|
|
* Get the configuration path. |
346
|
|
|
* |
347
|
|
|
* @param string $path |
348
|
|
|
* |
349
|
|
|
* @return string |
350
|
|
|
*/ |
351
|
|
|
function wpb_config_path($path = '') |
352
|
|
|
{ |
353
|
|
|
return wpb_app()->make('path.config').($path ? DIRECTORY_SEPARATOR.$path : $path); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
if (!function_exists('wpb_cookie')) { |
358
|
|
|
/** |
359
|
|
|
* Create a new cookie instance. |
360
|
|
|
* |
361
|
|
|
* @param string $name |
362
|
|
|
* @param string $value |
363
|
|
|
* @param int $minutes |
364
|
|
|
* @param string $path |
365
|
|
|
* @param string $domain |
366
|
|
|
* @param bool $secure |
367
|
|
|
* @param bool $httpOnly |
368
|
|
|
* @param bool $raw |
369
|
|
|
* @param string|null $sameSite |
370
|
|
|
* |
371
|
|
|
* @return \Illuminate\Cookie\CookieJar|\Symfony\Component\HttpFoundation\Cookie |
|
|
|
|
372
|
|
|
*/ |
373
|
|
|
function wpb_cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null) |
374
|
|
|
{ |
375
|
|
|
$cookie = wpb_app(CookieFactory::class); |
376
|
|
|
|
377
|
|
|
if (is_null($name)) { |
378
|
|
|
return $cookie; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly, $raw, $sameSite); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
if (!function_exists('wpb_csrf_field')) { |
386
|
|
|
/** |
387
|
|
|
* Generate a CSRF token form field. |
388
|
|
|
* |
389
|
|
|
* @return \Illuminate\Support\HtmlString |
390
|
|
|
*/ |
391
|
|
|
function wpb_csrf_field() |
392
|
|
|
{ |
393
|
|
|
return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'">'); |
|
|
|
|
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
if (!function_exists('wpb_database_path')) { |
398
|
|
|
/** |
399
|
|
|
* Get the database path. |
400
|
|
|
* |
401
|
|
|
* @param string $path |
402
|
|
|
* |
403
|
|
|
* @return string |
404
|
|
|
*/ |
405
|
|
|
function wpb_database_path($path = '') |
406
|
|
|
{ |
407
|
|
|
return wpb_app()->databasePath($path); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
if (!function_exists('wpb_decrypt')) { |
412
|
|
|
/** |
413
|
|
|
* Decrypt the given value. |
414
|
|
|
* |
415
|
|
|
* @param string $value |
416
|
|
|
* @param bool $unserialize |
417
|
|
|
* |
418
|
|
|
* @return mixed |
419
|
|
|
*/ |
420
|
|
|
function wpb_decrypt($value, $unserialize = true) |
421
|
|
|
{ |
422
|
|
|
return wpb_app('encrypter')->decrypt($value, $unserialize); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (!function_exists('wpb_dispatch')) { |
427
|
|
|
/** |
428
|
|
|
* Dispatch a job to its appropriate handler. |
429
|
|
|
* |
430
|
|
|
* @param mixed $job |
431
|
|
|
* |
432
|
|
|
* @return \Illuminate\Foundation\Bus\PendingDispatch |
|
|
|
|
433
|
|
|
*/ |
434
|
|
|
function wpb_dispatch($job) |
|
|
|
|
435
|
|
|
{ |
436
|
|
|
// return new PendingDispatch($job); |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
if (!function_exists('wpb_dispatch_now')) { |
441
|
|
|
/** |
442
|
|
|
* Dispatch a command to its appropriate handler in the current process. |
443
|
|
|
* |
444
|
|
|
* @param mixed $job |
445
|
|
|
* @param mixed $handler |
446
|
|
|
* |
447
|
|
|
* @return mixed |
448
|
|
|
*/ |
449
|
|
|
function wpb_dispatch_now($job, $handler = null) |
450
|
|
|
{ |
451
|
|
|
return wpb_app(Dispatcher::class)->dispatchNow($job, $handler); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
if (!function_exists('wpb_elixir')) { |
456
|
|
|
/** |
457
|
|
|
* Get the path to a versioned Elixir file. |
458
|
|
|
* |
459
|
|
|
* @param string $file |
460
|
|
|
* @param string $buildDirectory |
461
|
|
|
* |
462
|
|
|
* @throws \InvalidArgumentException |
463
|
|
|
* |
464
|
|
|
* @return string |
465
|
|
|
*/ |
466
|
|
|
function wpb_elixir($file, $buildDirectory = 'build') |
|
|
|
|
467
|
|
|
{ |
468
|
|
|
static $manifest = []; |
469
|
|
|
static $manifestPath; |
470
|
|
|
|
471
|
|
|
if (empty($manifest) || $manifestPath !== $buildDirectory) { |
472
|
|
|
$path = wpb_public_path($buildDirectory.'/rev-manifest.json'); |
473
|
|
|
|
474
|
|
|
if (file_exists($path)) { |
475
|
|
|
$manifest = json_decode(file_get_contents($path), true); |
476
|
|
|
$manifestPath = $buildDirectory; |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
$file = ltrim($file, '/'); |
481
|
|
|
|
482
|
|
|
if (isset($manifest[$file])) { |
483
|
|
|
return '/'.trim($buildDirectory.'/'.$manifest[$file], '/'); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$unversioned = wpb_public_path($file); |
487
|
|
|
|
488
|
|
|
if (file_exists($unversioned)) { |
489
|
|
|
return '/'.trim($file, '/'); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
throw new InvalidArgumentException("File {$file} not defined in asset manifest."); |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
if (!function_exists('wpb_encrypt')) { |
497
|
|
|
/** |
498
|
|
|
* Encrypt the given value. |
499
|
|
|
* |
500
|
|
|
* @param mixed $value |
501
|
|
|
* @param bool $serialize |
502
|
|
|
* |
503
|
|
|
* @return string |
504
|
|
|
*/ |
505
|
|
|
function wpb_encrypt($value, $serialize = true) |
506
|
|
|
{ |
507
|
|
|
return wpb_app('encrypter')->encrypt($value, $serialize); |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
if (!function_exists('wpb_event')) { |
512
|
|
|
/** |
513
|
|
|
* Dispatch an event and call the listeners. |
514
|
|
|
* |
515
|
|
|
* @param string|object $event |
516
|
|
|
* @param mixed $payload |
517
|
|
|
* @param bool $halt |
518
|
|
|
* |
519
|
|
|
* @return array|null |
520
|
|
|
*/ |
521
|
|
|
function wpb_event(...$args) |
522
|
|
|
{ |
523
|
|
|
return wpb_app('events')->dispatch(...$args); |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
if (!function_exists('wpb_factory')) { |
528
|
|
|
/** |
529
|
|
|
* Create a model factory builder for a given class, name, and amount. |
530
|
|
|
* |
531
|
|
|
* @param dynamic class|class,name|class,amount|class,name,amount |
532
|
|
|
* |
533
|
|
|
* @return \Illuminate\Database\Eloquent\FactoryBuilder |
534
|
|
|
*/ |
535
|
|
|
function wpb_factory() |
536
|
|
|
{ |
537
|
|
|
$factory = wpb_app(EloquentFactory::class); |
538
|
|
|
|
539
|
|
|
$arguments = func_get_args(); |
540
|
|
|
|
541
|
|
|
if (isset($arguments[1]) && is_string($arguments[1])) { |
542
|
|
|
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null); |
543
|
|
|
} elseif (isset($arguments[1])) { |
544
|
|
|
return $factory->of($arguments[0])->times($arguments[1]); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return $factory->of($arguments[0]); |
548
|
|
|
} |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
if (!function_exists('wpb_info')) { |
552
|
|
|
/** |
553
|
|
|
* Write some information to the log. |
554
|
|
|
* |
555
|
|
|
* @param string $message |
556
|
|
|
* @param array $context |
557
|
|
|
* |
558
|
|
|
* @return void |
559
|
|
|
*/ |
560
|
|
|
function wpb_info($message, $context = []) |
561
|
|
|
{ |
562
|
|
|
wpb_app('log')->info($message, $context); |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
if (!function_exists('wpb_logger')) { |
567
|
|
|
/** |
568
|
|
|
* Log a debug message to the logs. |
569
|
|
|
* |
570
|
|
|
* @param string $message |
571
|
|
|
* @param array $context |
572
|
|
|
* |
573
|
|
|
* @return \Illuminate\Log\LogManager|null |
|
|
|
|
574
|
|
|
*/ |
575
|
|
|
function wpb_logger($message = null, array $context = []) |
576
|
|
|
{ |
577
|
|
|
if (is_null($message)) { |
578
|
|
|
return wpb_app('log'); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
return wpb_app('log')->debug($message, $context); |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
if (!function_exists('wpb_logs')) { |
586
|
|
|
/** |
587
|
|
|
* Get a log driver instance. |
588
|
|
|
* |
589
|
|
|
* @param string $driver |
590
|
|
|
* |
591
|
|
|
* @return \Illuminate\Log\LogManager|\Psr\Log\LoggerInterface |
592
|
|
|
*/ |
593
|
|
|
function wpb_logs($driver = null) |
594
|
|
|
{ |
595
|
|
|
return $driver ? wpb_app('log')->driver($driver) : wpb_app('log'); |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
if (!function_exists('wpb_method_field')) { |
600
|
|
|
/** |
601
|
|
|
* Generate a form field to spoof the HTTP verb used by forms. |
602
|
|
|
* |
603
|
|
|
* @param string $method |
604
|
|
|
* |
605
|
|
|
* @return \Illuminate\Support\HtmlString |
606
|
|
|
*/ |
607
|
|
|
function wpb_method_field($method) |
608
|
|
|
{ |
609
|
|
|
return new HtmlString('<input type="hidden" name="_method" value="'.$method.'">'); |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
if (!function_exists('wpb_mix')) { |
614
|
|
|
/** |
615
|
|
|
* Get the path to a versioned Mix file. |
616
|
|
|
* |
617
|
|
|
* @param string $path |
618
|
|
|
* @param string $manifestDirectory |
619
|
|
|
* |
620
|
|
|
* @throws \Exception |
621
|
|
|
* |
622
|
|
|
* @return \Illuminate\Support\HtmlString|string |
623
|
|
|
*/ |
624
|
|
|
function wpb_mix($path, $manifestDirectory = '') |
625
|
|
|
{ |
626
|
|
|
static $manifests = []; |
627
|
|
|
|
628
|
|
|
if (!Str::startsWith($path, '/')) { |
629
|
|
|
$path = "/{$path}"; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
if ($manifestDirectory && !Str::startsWith($manifestDirectory, '/')) { |
633
|
|
|
$manifestDirectory = "/{$manifestDirectory}"; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
if (file_exists(public_path($manifestDirectory.'/hot'))) { |
|
|
|
|
637
|
|
|
$url = file_get_contents(public_path($manifestDirectory.'/hot')); |
638
|
|
|
|
639
|
|
|
if (Str::startsWith($url, ['http://', 'https://'])) { |
640
|
|
|
return new HtmlString(Str::after($url, ':').$path); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
return new HtmlString("//localhost:8080{$path}"); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
$manifestPath = wpb_public_path($manifestDirectory.'/mix-manifest.json'); |
647
|
|
|
|
648
|
|
|
if (!isset($manifests[$manifestPath])) { |
649
|
|
|
if (!file_exists($manifestPath)) { |
650
|
|
|
throw new Exception('The Mix manifest does not exist.'); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
$manifest = $manifests[$manifestPath]; |
657
|
|
|
|
658
|
|
|
if (!isset($manifest[$path])) { |
659
|
|
|
wpb_report(new Exception("Unable to locate Mix file: {$path}.")); |
660
|
|
|
|
661
|
|
|
if (!wpb_app('config')->get('app.debug')) { |
662
|
|
|
return $path; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
return new HtmlString($manifestDirectory.$manifest[$path]); |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
if (!function_exists('wpb_now')) { |
671
|
|
|
/** |
672
|
|
|
* Create a new Carbon instance for the current time. |
673
|
|
|
* |
674
|
|
|
* @param \DateTimeZone|string|null $tz |
675
|
|
|
* |
676
|
|
|
* @return \Illuminate\Support\Carbon |
677
|
|
|
*/ |
678
|
|
|
function wpb_wpb_now($tz = null) |
679
|
|
|
{ |
680
|
|
|
return Carbon::now($tz); |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
if (!function_exists('wpb_old')) { |
685
|
|
|
/** |
686
|
|
|
* Retrieve an old input item. |
687
|
|
|
* |
688
|
|
|
* @param string $key |
689
|
|
|
* @param mixed $default |
690
|
|
|
* |
691
|
|
|
* @return mixed |
692
|
|
|
*/ |
693
|
|
|
function wpb_old($key = null, $default = null) |
694
|
|
|
{ |
695
|
|
|
return wpb_app('request')->old($key, $default); |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
if (!function_exists('wpb_policy')) { |
700
|
|
|
/** |
701
|
|
|
* Get a policy instance for a given class. |
702
|
|
|
* |
703
|
|
|
* @param object|string $class |
704
|
|
|
* |
705
|
|
|
* @throws \InvalidArgumentException |
706
|
|
|
* |
707
|
|
|
* @return mixed |
708
|
|
|
*/ |
709
|
|
|
function wpb_policy($class) |
710
|
|
|
{ |
711
|
|
|
return wpb_app(Gate::class)->getPolicyFor($class); |
712
|
|
|
} |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
if (!function_exists('wpb_public_path')) { |
716
|
|
|
/** |
717
|
|
|
* Get the path to the public folder. |
718
|
|
|
* |
719
|
|
|
* @param string $path |
720
|
|
|
* |
721
|
|
|
* @return string |
722
|
|
|
*/ |
723
|
|
|
function wpb_public_path($path = '') |
|
|
|
|
724
|
|
|
{ |
725
|
|
|
return wpb_app()->make('path.public').($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path); |
726
|
|
|
} |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
if (!function_exists('wpb_redirect')) { |
730
|
|
|
/** |
731
|
|
|
* Get an instance of the redirector. |
732
|
|
|
* |
733
|
|
|
* @param string|null $to |
734
|
|
|
* @param int $status |
735
|
|
|
* @param array $headers |
736
|
|
|
* @param bool $secure |
737
|
|
|
* |
738
|
|
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse |
739
|
|
|
*/ |
740
|
|
|
function wpb_redirect($to = null, $status = 302, $headers = [], $secure = null) |
741
|
|
|
{ |
742
|
|
|
if (is_null($to)) { |
743
|
|
|
return wpb_app('redirect'); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
return wpb_app('redirect')->to($to, $status, $headers, $secure); |
747
|
|
|
} |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
if (!function_exists('wpb_report')) { |
751
|
|
|
/** |
752
|
|
|
* Report an exception. |
753
|
|
|
* |
754
|
|
|
* @param \Exception $exception |
755
|
|
|
* |
756
|
|
|
* @return void |
757
|
|
|
*/ |
758
|
|
|
function wpb_report($exception) |
759
|
|
|
{ |
760
|
|
|
if ($exception instanceof Throwable && |
761
|
|
|
!$exception instanceof Exception) { |
|
|
|
|
762
|
|
|
$exception = new FatalThrowableError($exception); |
|
|
|
|
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
wpb_app(ExceptionHandler::class)->report($exception); |
766
|
|
|
} |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
if (!function_exists('wpb_request')) { |
770
|
|
|
/** |
771
|
|
|
* Get an instance of the current request or an input item from the request. |
772
|
|
|
* |
773
|
|
|
* @param array|string $key |
774
|
|
|
* @param mixed $default |
775
|
|
|
* |
776
|
|
|
* @return \Illuminate\Http\Request|string|array |
777
|
|
|
*/ |
778
|
|
|
function wpb_request($key = null, $default = null) |
|
|
|
|
779
|
|
|
{ |
780
|
|
|
if (is_null($key)) { |
781
|
|
|
return wpb_app('request'); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
if (is_array($key)) { |
785
|
|
|
return wpb_app('request')->only($key); |
786
|
|
|
} |
787
|
|
|
|
788
|
|
|
$value = wpb_app('request')->__get($key); |
789
|
|
|
|
790
|
|
|
return is_null($value) ? value($default) : $value; |
791
|
|
|
} |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
if (!function_exists('wpb_rescue')) { |
795
|
|
|
/** |
796
|
|
|
* Catch a potential exception and return a default value. |
797
|
|
|
* |
798
|
|
|
* @param callable $callback |
799
|
|
|
* @param mixed $rescue |
800
|
|
|
* |
801
|
|
|
* @return mixed |
802
|
|
|
*/ |
803
|
|
|
function wpb_rescue(callable $callback, $rescue = null) |
804
|
|
|
{ |
805
|
|
|
try { |
806
|
|
|
return $callback(); |
807
|
|
|
} catch (Throwable $e) { |
808
|
|
|
wpb_report($e); |
809
|
|
|
|
810
|
|
|
return value($rescue); |
811
|
|
|
} |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
if (!function_exists('wpb_resolve')) { |
816
|
|
|
/** |
817
|
|
|
* Resolve a service from the container. |
818
|
|
|
* |
819
|
|
|
* @param string $name |
820
|
|
|
* |
821
|
|
|
* @return mixed |
822
|
|
|
*/ |
823
|
|
|
function wpb_resolve($name) |
824
|
|
|
{ |
825
|
|
|
return wpb_app($name); |
826
|
|
|
} |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
if (!function_exists('wpb_resource_path')) { |
830
|
|
|
/** |
831
|
|
|
* Get the path to the resources folder. |
832
|
|
|
* |
833
|
|
|
* @param string $path |
834
|
|
|
* |
835
|
|
|
* @return string |
836
|
|
|
*/ |
837
|
|
|
function wpb_resource_path($path = '') |
838
|
|
|
{ |
839
|
|
|
return wpb_app()->resourcePath($path); |
840
|
|
|
} |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
if (!function_exists('wpb_response')) { |
844
|
|
|
/** |
845
|
|
|
* Return a new response from the application. |
846
|
|
|
* |
847
|
|
|
* @param \Illuminate\View\View|string|array|null $content |
848
|
|
|
* @param int $status |
849
|
|
|
* @param array $headers |
850
|
|
|
* |
851
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory |
852
|
|
|
*/ |
853
|
|
|
function wpb_response($content = '', $status = 200, array $headers = []) |
854
|
|
|
{ |
855
|
|
|
$factory = wpb_app(ResponseFactory::class); |
856
|
|
|
|
857
|
|
|
if (func_num_args() === 0) { |
858
|
|
|
return $factory; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
return $factory->make($content, $status, $headers); |
862
|
|
|
} |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
if (!function_exists('wpb_route')) { |
866
|
|
|
/** |
867
|
|
|
* Generate the URL to a named route. |
868
|
|
|
* |
869
|
|
|
* @param array|string $name |
870
|
|
|
* @param mixed $parameters |
871
|
|
|
* @param bool $absolute |
872
|
|
|
* |
873
|
|
|
* @return string |
874
|
|
|
*/ |
875
|
|
|
function wpb_route($name, $parameters = [], $absolute = true) |
876
|
|
|
{ |
877
|
|
|
return wpb_app('url')->route($name, $parameters, $absolute); |
878
|
|
|
} |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
if (!function_exists('wpb_secure_asset')) { |
882
|
|
|
/** |
883
|
|
|
* Generate an asset path for the application. |
884
|
|
|
* |
885
|
|
|
* @param string $path |
886
|
|
|
* |
887
|
|
|
* @return string |
888
|
|
|
*/ |
889
|
|
|
function wpb_secure_asset($path) |
890
|
|
|
{ |
891
|
|
|
return wpb_asset($path, true); |
892
|
|
|
} |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
if (!function_exists('wpb_secure_url')) { |
896
|
|
|
/** |
897
|
|
|
* Generate a HTTPS url for the application. |
898
|
|
|
* |
899
|
|
|
* @param string $path |
900
|
|
|
* @param mixed $parameters |
901
|
|
|
* |
902
|
|
|
* @return string |
903
|
|
|
*/ |
904
|
|
|
function wpb_secure_url($path, $parameters = []) |
905
|
|
|
{ |
906
|
|
|
return wpb_url($path, $parameters, true); |
|
|
|
|
907
|
|
|
} |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
if (!function_exists('wpb_session')) { |
911
|
|
|
/** |
912
|
|
|
* Get / set the specified session value. |
913
|
|
|
* |
914
|
|
|
* If an array is passed as the key, we will assume you want to set an array of values. |
915
|
|
|
* |
916
|
|
|
* @param array|string $key |
917
|
|
|
* @param mixed $default |
918
|
|
|
* |
919
|
|
|
* @return mixed|\Illuminate\Session\Store|\Illuminate\Session\SessionManager |
920
|
|
|
*/ |
921
|
|
|
function wpb_session($key = null, $default = null) |
922
|
|
|
{ |
923
|
|
|
if (is_null($key)) { |
924
|
|
|
return wpb_app('session'); |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
if (is_array($key)) { |
928
|
|
|
return wpb_app('session')->put($key); |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
return wpb_app('session')->get($key, $default); |
932
|
|
|
} |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
if (!function_exists('wpb_storage_path')) { |
936
|
|
|
/** |
937
|
|
|
* Get the path to the storage folder. |
938
|
|
|
* |
939
|
|
|
* @param string $path |
940
|
|
|
* |
941
|
|
|
* @return string |
942
|
|
|
*/ |
943
|
|
|
function wpb_storage_path($path = '') |
944
|
|
|
{ |
945
|
|
|
return wpb_app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path); |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
if (!function_exists('wpb_today')) { |
950
|
|
|
/** |
951
|
|
|
* Create a new Carbon instance for the current date. |
952
|
|
|
* |
953
|
|
|
* @param \DateTimeZone|string|null $tz |
954
|
|
|
* |
955
|
|
|
* @return \Illuminate\Support\Carbon |
956
|
|
|
*/ |
957
|
|
|
function wpb_today($tz = null) |
958
|
|
|
{ |
959
|
|
|
return Carbon::today($tz); |
960
|
|
|
} |
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
if (!function_exists('wpb_trans')) { |
964
|
|
|
/** |
965
|
|
|
* Translate the given message. |
966
|
|
|
* |
967
|
|
|
* @param string $key |
968
|
|
|
* @param array $replace |
969
|
|
|
* @param string $locale |
970
|
|
|
* |
971
|
|
|
* @return \Illuminate\Contracts\Translation\Translator|string|array|null |
972
|
|
|
*/ |
973
|
|
|
function wpb_trans($key = null, $replace = [], $locale = null) |
974
|
|
|
{ |
975
|
|
|
if (is_null($key)) { |
976
|
|
|
return wpb_app('translator'); |
977
|
|
|
} |
978
|
|
|
|
979
|
|
|
return wpb_app('translator')->trans($key, $replace, $locale); |
980
|
|
|
} |
981
|
|
|
} |
982
|
|
|
|
983
|
|
|
if (!function_exists('wpb_trans_choice')) { |
984
|
|
|
/** |
985
|
|
|
* Translates the given message based on a count. |
986
|
|
|
* |
987
|
|
|
* @param string $key |
988
|
|
|
* @param int|array|\Countable $number |
989
|
|
|
* @param array $replace |
990
|
|
|
* @param string $locale |
991
|
|
|
* |
992
|
|
|
* @return string |
993
|
|
|
*/ |
994
|
|
|
function wpb_trans_choice($key, $number, array $replace = [], $locale = null) |
995
|
|
|
{ |
996
|
|
|
return wpb_app('translator')->transChoice($key, $number, $replace, $locale); |
997
|
|
|
} |
998
|
|
|
} |
999
|
|
|
|
1000
|
|
|
if (!function_exists('__')) { |
1001
|
|
|
/** |
1002
|
|
|
* Translate the given message. |
1003
|
|
|
* |
1004
|
|
|
* @param string $key |
1005
|
|
|
* @param array $replace |
1006
|
|
|
* @param string $locale |
1007
|
|
|
* |
1008
|
|
|
* @return string|array|null |
1009
|
|
|
*/ |
1010
|
|
|
function __($key, $replace = [], $locale = null) |
1011
|
|
|
{ |
1012
|
|
|
return wpb_app('translator')->getFromJson($key, $replace, $locale); |
1013
|
|
|
} |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
|
if (!function_exists('wpb_url')) { |
1017
|
|
|
/** |
1018
|
|
|
* Generate a url for the application. |
1019
|
|
|
* |
1020
|
|
|
* @param string $path |
1021
|
|
|
* @param mixed $parameters |
1022
|
|
|
* @param bool $secure |
1023
|
|
|
* |
1024
|
|
|
* @return \Illuminate\Contracts\Routing\UrlGenerator|string |
1025
|
|
|
*/ |
1026
|
|
|
function wpb_url($path = null, $parameters = [], $secure = null) |
1027
|
|
|
{ |
1028
|
|
|
if (is_null($path)) { |
1029
|
|
|
return wpb_app(UrlGenerator::class); |
1030
|
|
|
} |
1031
|
|
|
|
1032
|
|
|
return wpb_app(UrlGenerator::class)->to($path, $parameters, $secure); |
1033
|
|
|
} |
1034
|
|
|
} |
1035
|
|
|
|
1036
|
|
|
if (!function_exists('wpb_validator')) { |
1037
|
|
|
/** |
1038
|
|
|
* Create a new Validator instance. |
1039
|
|
|
* |
1040
|
|
|
* @param array $data |
1041
|
|
|
* @param array $rules |
1042
|
|
|
* @param array $messages |
1043
|
|
|
* @param array $customAttributes |
1044
|
|
|
* |
1045
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
1046
|
|
|
*/ |
1047
|
|
|
function wpb_validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []) |
|
|
|
|
1048
|
|
|
{ |
1049
|
|
|
$factory = wpb_app(ValidationFactory::class); |
1050
|
|
|
|
1051
|
|
|
if (func_num_args() === 0) { |
1052
|
|
|
return $factory; |
1053
|
|
|
} |
1054
|
|
|
|
1055
|
|
|
return $factory->make($data, $rules, $messages, $customAttributes); |
1056
|
|
|
} |
1057
|
|
|
} |
1058
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: