helpers.php
23.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
<?php
use Illuminate\Container\Container;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Foundation\Bus\PendingClosureDispatch;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Foundation\Mix;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\HtmlString;
use Symfony\Component\HttpFoundation\Response;
if (! function_exists('abort')) {
/**
* Throw an HttpException with the given data.
*
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
* @param string $message
* @param array $headers
* @return never
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
function abort($code, $message = '', array $headers = [])
{
if ($code instanceof Response) {
throw new HttpResponseException($code);
} elseif ($code instanceof Responsable) {
throw new HttpResponseException($code->toResponse(request()));
}
app()->abort($code, $message, $headers);
}
}
if (! function_exists('abort_if')) {
/**
* Throw an HttpException with the given data if the given condition is true.
*
* @param bool $boolean
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
* @param string $message
* @param array $headers
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
function abort_if($boolean, $code, $message = '', array $headers = [])
{
if ($boolean) {
abort($code, $message, $headers);
}
}
}
if (! function_exists('abort_unless')) {
/**
* Throw an HttpException with the given data unless the given condition is true.
*
* @param bool $boolean
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
* @param string $message
* @param array $headers
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
function abort_unless($boolean, $code, $message = '', array $headers = [])
{
if (! $boolean) {
abort($code, $message, $headers);
}
}
}
if (! function_exists('action')) {
/**
* Generate the URL to a controller action.
*
* @param string|array $name
* @param mixed $parameters
* @param bool $absolute
* @return string
*/
function action($name, $parameters = [], $absolute = true)
{
return app('url')->action($name, $parameters, $absolute);
}
}
if (! function_exists('app')) {
/**
* Get the available container instance.
*
* @param string|null $abstract
* @param array $parameters
* @return mixed|\Illuminate\Contracts\Foundation\Application
*/
function app($abstract = null, array $parameters = [])
{
if (is_null($abstract)) {
return Container::getInstance();
}
return Container::getInstance()->make($abstract, $parameters);
}
}
if (! function_exists('app_path')) {
/**
* Get the path to the application folder.
*
* @param string $path
* @return string
*/
function app_path($path = '')
{
return app()->path($path);
}
}
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
}
if (! function_exists('auth')) {
/**
* Get the available auth instance.
*
* @param string|null $guard
* @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
*/
function auth($guard = null)
{
if (is_null($guard)) {
return app(AuthFactory::class);
}
return app(AuthFactory::class)->guard($guard);
}
}
if (! function_exists('back')) {
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @param mixed $fallback
* @return \Illuminate\Http\RedirectResponse
*/
function back($status = 302, $headers = [], $fallback = false)
{
return app('redirect')->back($status, $headers, $fallback);
}
}
if (! function_exists('base_path')) {
/**
* Get the path to the base of the install.
*
* @param string $path
* @return string
*/
function base_path($path = '')
{
return app()->basePath($path);
}
}
if (! function_exists('bcrypt')) {
/**
* Hash the given value against the bcrypt algorithm.
*
* @param string $value
* @param array $options
* @return string
*/
function bcrypt($value, $options = [])
{
return app('hash')->driver('bcrypt')->make($value, $options);
}
}
if (! function_exists('broadcast')) {
/**
* Begin broadcasting an event.
*
* @param mixed|null $event
* @return \Illuminate\Broadcasting\PendingBroadcast
*/
function broadcast($event = null)
{
return app(BroadcastFactory::class)->event($event);
}
}
if (! function_exists('cache')) {
/**
* Get / set the specified cache value.
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param dynamic key|key,default|data,expiration|null
* @return mixed|\Illuminate\Cache\CacheManager
*
* @throws \Exception
*/
function cache()
{
$arguments = func_get_args();
if (empty($arguments)) {
return app('cache');
}
if (is_string($arguments[0])) {
return app('cache')->get(...$arguments);
}
if (! is_array($arguments[0])) {
throw new Exception(
'When setting a value in the cache, you must pass an array of key / value pairs.'
);
}
return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1] ?? null);
}
}
if (! function_exists('config')) {
/**
* Get / set the specified configuration value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Config\Repository
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return app('config');
}
if (is_array($key)) {
return app('config')->set($key);
}
return app('config')->get($key, $default);
}
}
if (! function_exists('config_path')) {
/**
* Get the configuration path.
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->configPath($path);
}
}
if (! function_exists('cookie')) {
/**
* Create a new cookie instance.
*
* @param string|null $name
* @param string|null $value
* @param int $minutes
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Illuminate\Cookie\CookieJar|\Symfony\Component\HttpFoundation\Cookie
*/
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
$cookie = app(CookieFactory::class);
if (is_null($name)) {
return $cookie;
}
return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
}
if (! function_exists('csrf_field')) {
/**
* Generate a CSRF token form field.
*
* @return \Illuminate\Support\HtmlString
*/
function csrf_field()
{
return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'">');
}
}
if (! function_exists('csrf_token')) {
/**
* Get the CSRF token value.
*
* @return string
*
* @throws \RuntimeException
*/
function csrf_token()
{
$session = app('session');
if (isset($session)) {
return $session->token();
}
throw new RuntimeException('Application session store not set.');
}
}
if (! function_exists('database_path')) {
/**
* Get the database path.
*
* @param string $path
* @return string
*/
function database_path($path = '')
{
return app()->databasePath($path);
}
}
if (! function_exists('decrypt')) {
/**
* Decrypt the given value.
*
* @param string $value
* @param bool $unserialize
* @return mixed
*/
function decrypt($value, $unserialize = true)
{
return app('encrypter')->decrypt($value, $unserialize);
}
}
if (! function_exists('dispatch')) {
/**
* Dispatch a job to its appropriate handler.
*
* @param mixed $job
* @return \Illuminate\Foundation\Bus\PendingDispatch
*/
function dispatch($job)
{
return $job instanceof Closure
? new PendingClosureDispatch(CallQueuedClosure::create($job))
: new PendingDispatch($job);
}
}
if (! function_exists('dispatch_sync')) {
/**
* Dispatch a command to its appropriate handler in the current process.
*
* Queueable jobs will be dispatched to the "sync" queue.
*
* @param mixed $job
* @param mixed $handler
* @return mixed
*/
function dispatch_sync($job, $handler = null)
{
return app(Dispatcher::class)->dispatchSync($job, $handler);
}
}
if (! function_exists('dispatch_now')) {
/**
* Dispatch a command to its appropriate handler in the current process.
*
* @param mixed $job
* @param mixed $handler
* @return mixed
*
* @deprecated Will be removed in a future Laravel version.
*/
function dispatch_now($job, $handler = null)
{
return app(Dispatcher::class)->dispatchNow($job, $handler);
}
}
if (! function_exists('encrypt')) {
/**
* Encrypt the given value.
*
* @param mixed $value
* @param bool $serialize
* @return string
*/
function encrypt($value, $serialize = true)
{
return app('encrypter')->encrypt($value, $serialize);
}
}
if (! function_exists('event')) {
/**
* Dispatch an event and call the listeners.
*
* @param string|object $event
* @param mixed $payload
* @param bool $halt
* @return array|null
*/
function event(...$args)
{
return app('events')->dispatch(...$args);
}
}
if (! function_exists('info')) {
/**
* Write some information to the log.
*
* @param string $message
* @param array $context
* @return void
*/
function info($message, $context = [])
{
app('log')->info($message, $context);
}
}
if (! function_exists('logger')) {
/**
* Log a debug message to the logs.
*
* @param string|null $message
* @param array $context
* @return \Illuminate\Log\LogManager|null
*/
function logger($message = null, array $context = [])
{
if (is_null($message)) {
return app('log');
}
return app('log')->debug($message, $context);
}
}
if (! function_exists('lang_path')) {
/**
* Get the path to the language folder.
*
* @param string $path
* @return string
*/
function lang_path($path = '')
{
return app('path.lang').($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
if (! function_exists('logs')) {
/**
* Get a log driver instance.
*
* @param string|null $driver
* @return \Illuminate\Log\LogManager|\Psr\Log\LoggerInterface
*/
function logs($driver = null)
{
return $driver ? app('log')->driver($driver) : app('log');
}
}
if (! function_exists('method_field')) {
/**
* Generate a form field to spoof the HTTP verb used by forms.
*
* @param string $method
* @return \Illuminate\Support\HtmlString
*/
function method_field($method)
{
return new HtmlString('<input type="hidden" name="_method" value="'.$method.'">');
}
}
if (! function_exists('mix')) {
/**
* Get the path to a versioned Mix file.
*
* @param string $path
* @param string $manifestDirectory
* @return \Illuminate\Support\HtmlString|string
*
* @throws \Exception
*/
function mix($path, $manifestDirectory = '')
{
return app(Mix::class)(...func_get_args());
}
}
if (! function_exists('now')) {
/**
* Create a new Carbon instance for the current time.
*
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function now($tz = null)
{
return Date::now($tz);
}
}
if (! function_exists('old')) {
/**
* Retrieve an old input item.
*
* @param string|null $key
* @param mixed $default
* @return mixed
*/
function old($key = null, $default = null)
{
return app('request')->old($key, $default);
}
}
if (! function_exists('policy')) {
/**
* Get a policy instance for a given class.
*
* @param object|string $class
* @return mixed
*
* @throws \InvalidArgumentException
*/
function policy($class)
{
return app(Gate::class)->getPolicyFor($class);
}
}
if (! function_exists('public_path')) {
/**
* Get the path to the public folder.
*
* @param string $path
* @return string
*/
function public_path($path = '')
{
return app()->make('path.public').($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
}
}
if (! function_exists('redirect')) {
/**
* Get an instance of the redirector.
*
* @param string|null $to
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
if (is_null($to)) {
return app('redirect');
}
return app('redirect')->to($to, $status, $headers, $secure);
}
}
if (! function_exists('report')) {
/**
* Report an exception.
*
* @param \Throwable|string $exception
* @return void
*/
function report($exception)
{
if (is_string($exception)) {
$exception = new Exception($exception);
}
app(ExceptionHandler::class)->report($exception);
}
}
if (! function_exists('request')) {
/**
* Get an instance of the current request or an input item from the request.
*
* @param array|string|null $key
* @param mixed $default
* @return \Illuminate\Http\Request|string|array|null
*/
function request($key = null, $default = null)
{
if (is_null($key)) {
return app('request');
}
if (is_array($key)) {
return app('request')->only($key);
}
$value = app('request')->__get($key);
return is_null($value) ? value($default) : $value;
}
}
if (! function_exists('rescue')) {
/**
* Catch a potential exception and return a default value.
*
* @param callable $callback
* @param mixed $rescue
* @param bool $report
* @return mixed
*/
function rescue(callable $callback, $rescue = null, $report = true)
{
try {
return $callback();
} catch (Throwable $e) {
if ($report) {
report($e);
}
return value($rescue, $e);
}
}
}
if (! function_exists('resolve')) {
/**
* Resolve a service from the container.
*
* @param string $name
* @param array $parameters
* @return mixed
*/
function resolve($name, array $parameters = [])
{
return app($name, $parameters);
}
}
if (! function_exists('resource_path')) {
/**
* Get the path to the resources folder.
*
* @param string $path
* @return string
*/
function resource_path($path = '')
{
return app()->resourcePath($path);
}
}
if (! function_exists('response')) {
/**
* Return a new response from the application.
*
* @param \Illuminate\Contracts\View\View|string|array|null $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
*/
function response($content = '', $status = 200, array $headers = [])
{
$factory = app(ResponseFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($content, $status, $headers);
}
}
if (! function_exists('route')) {
/**
* Generate the URL to a named route.
*
* @param array|string $name
* @param mixed $parameters
* @param bool $absolute
* @return string
*/
function route($name, $parameters = [], $absolute = true)
{
return app('url')->route($name, $parameters, $absolute);
}
}
if (! function_exists('secure_asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @return string
*/
function secure_asset($path)
{
return asset($path, true);
}
}
if (! function_exists('secure_url')) {
/**
* Generate a HTTPS url for the application.
*
* @param string $path
* @param mixed $parameters
* @return string
*/
function secure_url($path, $parameters = [])
{
return url($path, $parameters, true);
}
}
if (! function_exists('session')) {
/**
* Get / set the specified session value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Session\Store|\Illuminate\Session\SessionManager
*/
function session($key = null, $default = null)
{
if (is_null($key)) {
return app('session');
}
if (is_array($key)) {
return app('session')->put($key);
}
return app('session')->get($key, $default);
}
}
if (! function_exists('storage_path')) {
/**
* Get the path to the storage folder.
*
* @param string $path
* @return string
*/
function storage_path($path = '')
{
return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
if (! function_exists('today')) {
/**
* Create a new Carbon instance for the current date.
*
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function today($tz = null)
{
return Date::today($tz);
}
}
if (! function_exists('trans')) {
/**
* Translate the given message.
*
* @param string|null $key
* @param array $replace
* @param string|null $locale
* @return \Illuminate\Contracts\Translation\Translator|string|array|null
*/
function trans($key = null, $replace = [], $locale = null)
{
if (is_null($key)) {
return app('translator');
}
return app('translator')->get($key, $replace, $locale);
}
}
if (! function_exists('trans_choice')) {
/**
* Translates the given message based on a count.
*
* @param string $key
* @param \Countable|int|array $number
* @param array $replace
* @param string|null $locale
* @return string
*/
function trans_choice($key, $number, array $replace = [], $locale = null)
{
return app('translator')->choice($key, $number, $replace, $locale);
}
}
if (! function_exists('__')) {
/**
* Translate the given message.
*
* @param string|null $key
* @param array $replace
* @param string|null $locale
* @return string|array|null
*/
function __($key = null, $replace = [], $locale = null)
{
if (is_null($key)) {
return $key;
}
return trans($key, $replace, $locale);
}
}
if (! function_exists('url')) {
/**
* Generate a url for the application.
*
* @param string|null $path
* @param mixed $parameters
* @param bool|null $secure
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
*/
function url($path = null, $parameters = [], $secure = null)
{
if (is_null($path)) {
return app(UrlGenerator::class);
}
return app(UrlGenerator::class)->to($path, $parameters, $secure);
}
}
if (! function_exists('validator')) {
/**
* Create a new Validator instance.
*
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return \Illuminate\Contracts\Validation\Validator|\Illuminate\Contracts\Validation\Factory
*/
function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = [])
{
$factory = app(ValidationFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($data, $rules, $messages, $customAttributes);
}
}
if (! function_exists('view')) {
/**
* Get the evaluated view contents for the given view.
*
* @param string|null $view
* @param \Illuminate\Contracts\Support\Arrayable|array $data
* @param array $mergeData
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app(ViewFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($view, $data, $mergeData);
}
}