Issues (576)

functions/helpers.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja;
15
16
use Valkyrja\Annotation\Contract\Annotations;
17
use Valkyrja\Api\Contract\Api;
18
use Valkyrja\Application\Contract\Application;
19
use Valkyrja\Application\Valkyrja;
20
use Valkyrja\Auth\Contract\Auth;
21
use Valkyrja\Cache\Contract\Cache;
22
use Valkyrja\Client\Contract\Client;
23
use Valkyrja\Console\Contract\Console;
24
use Valkyrja\Console\Input\Contract\Input;
25
use Valkyrja\Console\Kernel\Contract\Kernel as ConsoleKernel;
26
use Valkyrja\Console\Output\Contract\Output;
27
use Valkyrja\Container\Contract\Container;
28
use Valkyrja\Crypt\Contract\Crypt;
29
use Valkyrja\Dispatcher\Contract\Dispatcher;
30
use Valkyrja\Event\Contract\Dispatcher as Events;
31
use Valkyrja\Filesystem\Contract\Filesystem;
32
use Valkyrja\Http\Exception\HttpException;
33
use Valkyrja\Http\Exception\HttpRedirectException;
34
use Valkyrja\Http\Factory\Contract\ResponseFactory;
35
use Valkyrja\Http\Request\Contract\ServerRequest;
36
use Valkyrja\Http\Response\Contract\JsonResponse;
37
use Valkyrja\Http\Response\Contract\RedirectResponse;
38
use Valkyrja\Http\Response\Contract\Response;
39
use Valkyrja\HttpKernel\Contract\Kernel;
40
use Valkyrja\Log\Contract\Logger;
41
use Valkyrja\Mail\Contract\Mail;
42
use Valkyrja\Mail\Message\Contract\Message as MailMessage;
43
use Valkyrja\Notification\Contract\Notification;
44
use Valkyrja\Notification\Data\Contract\Notify;
45
use Valkyrja\Notification\Entity\Contract\NotifiableUser;
46
use Valkyrja\Orm\Orm;
47
use Valkyrja\Path\Generator\Contract\Generator;
48
use Valkyrja\Path\Parser\Contract\Parser;
49
use Valkyrja\Reflection\Contract\Reflection;
50
use Valkyrja\Routing\Collector\Contract\Collector;
51
use Valkyrja\Routing\Contract\Router;
52
use Valkyrja\Routing\Exception\InvalidRouteName;
53
use Valkyrja\Routing\Model\Contract\Route;
54
use Valkyrja\Routing\Support\Abort;
55
use Valkyrja\Routing\Url\Contract\Url;
56
use Valkyrja\Session\Contract\Session;
57
use Valkyrja\Sms\Contract\Sms;
58
use Valkyrja\Sms\Message\Contract\Message as SmsMessage;
59
use Valkyrja\Support\Directory;
60
use Valkyrja\Validation\Contract\Validation;
61
use Valkyrja\View\Contract\View;
62
use Valkyrja\View\Template\Contract\Template;
63
64
use function var_dump;
65
66
/**
67
 * Return the global $app variable.
68
 *
69
 * @return Application
70
 */
71
function app(): Application
72
{
73
    return Valkyrja::app();
74
}
75
76
/**
77
 * Abort the application due to error.
78
 *
79
 * @param int|null      $statusCode The status code to use
80
 * @param string|null   $message    [optional] The Exception message to throw
81
 * @param array|null    $headers    [optional] The headers to send
82
 * @param Response|null $response   [optional] The Response to send
83
 *
84
 * @throws HttpException
85
 *
86
 * @return never
87
 */
88
function abort(
89
    int|null $statusCode = null,
90
    string|null $message = null,
91
    array|null $headers = null,
92
    Response|null $response = null
93
): never {
94
    Abort::abort($statusCode, $message, $headers, $response);
95
}
96
97
/**
98
 * Abort the application due to error with a given response to send.
99
 *
100
 * @param Response $response The Response to send
101
 *
102
 * @throws HttpException
103
 *
104
 * @return never
105
 */
106
function abortResponse(Response $response): never
107
{
108
    Abort::response($response);
109
}
110
111
/**
112
 * Return the annotator instance from the container.
113
 *
114
 * @return Annotations
115
 */
116
function annotator(): Annotations
117
{
118
    return container()->getSingleton(Annotations::class);
119
}
120
121
/**
122
 * Return the api instance from the container.
123
 *
124
 * @return Api
125
 */
126
function api(): Api
127
{
128
    return container()->getSingleton(Api::class);
129
}
130
131
/**
132
 * Return the auth instance from the container.
133
 *
134
 * @return Auth
135
 */
136
function auth(): Auth
137
{
138
    return container()->getSingleton(Auth::class);
139
}
140
141
/**
142
 * Return the cache instance from the container.
143
 *
144
 * @return Cache
145
 */
146
function cache(): Cache
147
{
148
    return container()->getSingleton(Cache::class);
149
}
150
151
/**
152
 * Return the client instance from the container.
153
 *
154
 * @return Client
155
 */
156
function client(): Client
157
{
158
    return container()->getSingleton(Client::class);
159
}
160
161
/**
162
 * Get the config.
163
 *
164
 * @param string|null $key     [optional] The key to get
165
 * @param mixed       $default [optional] The default value if the key is not found
166
 *
167
 * @return mixed
168
 */
169
function config(string|null $key = null, mixed $default = null): mixed
170
{
171
    return Valkyrja::app()->config($key, $default);
172
}
173
174
/**
175
 * Get console.
176
 *
177
 * @return Console
178
 */
179
function console(): Console
180
{
181
    return container()->getSingleton(Console::class);
182
}
183
184
/**
185
 * Get container.
186
 *
187
 * @return Container
188
 */
189
function container(): Container
190
{
191
    return app()->container();
192
}
193
194
/**
195
 * Get dispatcher.
196
 *
197
 * @return Dispatcher
198
 */
199
function dispatcher(): Dispatcher
200
{
201
    return container()->getSingleton(Dispatcher::class);
202
}
203
204
/**
205
 * Get an environment variable.
206
 *
207
 * @param string|null $key     [optional] The variable to get
208
 * @param mixed       $default [optional] The default value to return
209
 *
210
 * @return mixed
211
 */
212
function env(string|null $key = null, mixed $default = null): mixed
213
{
214
    // Does not use the app() helper due to the self::$instance property
215
    // that Valkyrja::app() relies on has not been set yet when
216
    // this helper may be used.
217
    return Valkyrja::env($key, $default);
218
}
219
220
/**
221
 * Get events.
222
 *
223
 * @return Events
224
 */
225
function events(): Events
226
{
227
    return container()->getSingleton(Events::class);
228
}
229
230
/**
231
 * Get filesystem.
232
 *
233
 * @return Filesystem
234
 */
235
function filesystem(): Filesystem
236
{
237
    return container()->getSingleton(Filesystem::class);
238
}
239
240
/**
241
 * Get input.
242
 *
243
 * @return Input
244
 */
245
function input(): Input
246
{
247
    return container()->getSingleton(Input::class);
248
}
249
250
/**
251
 * Get kernel.
252
 *
253
 * @return Kernel
254
 */
255
function kernel(): Kernel
256
{
257
    return container()->getSingleton(Kernel::class);
258
}
259
260
/**
261
 * Get console kernel.
262
 *
263
 * @return ConsoleKernel
264
 */
265
function consoleKernel(): ConsoleKernel
266
{
267
    return container()->getSingleton(ConsoleKernel::class);
268
}
269
270
/**
271
 * Get the crypt.
272
 *
273
 * @return Crypt
274
 */
275
function crypt(): Crypt
276
{
277
    return container()->getSingleton(Crypt::class);
278
}
279
280
/**
281
 * Get logger.
282
 *
283
 * @return Logger
284
 */
285
function logger(): Logger
286
{
287
    return container()->getSingleton(Logger::class);
288
}
289
290
/**
291
 * Get mail.
292
 *
293
 * @return Mail
294
 */
295
function mail(): Mail
296
{
297
    return container()->getSingleton(Mail::class);
298
}
299
300
/**
301
 * Get a new mail message.
302
 *
303
 * @param string|null $name [optional] The name
304
 *
305
 * @return MailMessage
306
 */
307
function mailMessage(string|null $name = null): MailMessage
308
{
309
    return \Valkyrja\mail()->createMessage($name);
310
}
311
312
/**
313
 * Get notification manager.
314
 *
315
 * @return Notification
316
 */
317
function notifier(): Notification
318
{
319
    return container()->getSingleton(Notification::class);
320
}
321
322
/**
323
 * Notify a user.
324
 *
325
 * @param Notify         $notification The notification
326
 * @param NotifiableUser $user         The user
327
 *
328
 * @return void
329
 */
330
function notifyUser(Notify $notification, NotifiableUser $user): void
331
{
332
    \Valkyrja\notifier()->notifyUser($notification, $user);
333
}
334
335
/**
336
 * Notify users.
337
 *
338
 * @param Notify           $notification The notification
339
 * @param NotifiableUser[] $users        The users
340
 *
341
 * @return void
342
 */
343
function notifyUsers(Notify $notification, NotifiableUser ...$users): void
344
{
345
    \Valkyrja\notifier()->notifyUsers($notification, ...$users);
346
}
347
348
/**
349
 * Get path generator.
350
 *
351
 * @return Generator
352
 */
353
function pathGenerator(): Generator
354
{
355
    return container()->getSingleton(Generator::class);
356
}
357
358
/**
359
 * Get path parser.
360
 *
361
 * @return Parser
362
 */
363
function pathParser(): Parser
364
{
365
    return container()->getSingleton(Parser::class);
366
}
367
368
/**
369
 * Get the ORM manager.
370
 *
371
 * @return Orm
372
 */
373
function orm(): Orm
374
{
375
    return container()->getSingleton(Orm::class);
376
}
377
378
/**
379
 * Get output.
380
 *
381
 * @return Output
382
 */
383
function output(): Output
384
{
385
    return container()->getSingleton(Output::class);
386
}
387
388
/**
389
 * Get reflector.
390
 *
391
 * @return Reflection
392
 */
393
function reflector(): Reflection
394
{
395
    return container()->getSingleton(Reflection::class);
396
}
397
398
/**
399
 * Get request.
400
 *
401
 * @return ServerRequest
402
 */
403
function request(): ServerRequest
404
{
405
    return container()->getSingleton(ServerRequest::class);
406
}
407
408
/**
409
 * Get router.
410
 *
411
 * @return Router
412
 */
413
function router(): Router
414
{
415
    return container()->getSingleton(Router::class);
416
}
417
418
/**
419
 * Get route collector.
420
 *
421
 * @return Collector
422
 */
423
function collector(): Collector
424
{
425
    return container()->getSingleton(Collector::class);
426
}
427
428
/**
429
 * Get routing url service.
430
 *
431
 * @return Url
432
 */
433
function url(): Url
434
{
435
    return container()->getSingleton(Url::class);
436
}
437
438
/**
439
 * Get a route by name.
440
 *
441
 * @param string $name The name of the route to get
442
 *
443
 * @throws InvalidRouteName
444
 *
445
 * @return Route
446
 */
447
function route(string $name): Route
448
{
449
    return router()->getRoute($name);
450
}
451
452
/**
453
 * Get a route url by name.
454
 *
455
 * @param string     $name     The name of the route to get
456
 * @param array|null $data     [optional] The route data if dynamic
457
 * @param bool       $absolute [optional] Whether this url should be absolute
458
 *
459
 * @return string
460
 */
461
function routeUrl(string $name, array|null $data = null, bool|null $absolute = null): string
462
{
463
    return url()->getUrl($name, $data, $absolute);
464
}
465
466
/**
467
 * Get the response builder.
468
 *
469
 * @return ResponseFactory
470
 */
471
function responseFactory(): ResponseFactory
472
{
473
    return container()->getSingleton(ResponseFactory::class);
474
}
475
476
/**
477
 * Return a new response from the application.
478
 *
479
 * @param string|null $content    [optional] The content to set
480
 * @param int|null    $statusCode [optional] The status code to set
481
 * @param array|null  $headers    [optional] The headers to set
482
 *
483
 * @return Response
484
 */
485
function response(string|null $content = null, int|null $statusCode = null, array|null $headers = null): Response
486
{
487
    return responseFactory()->createResponse($content, $statusCode, $headers);
488
}
489
490
/**
491
 * Return a new json response from the application.
492
 *
493
 * @param array|null $data       [optional] An array of data
494
 * @param int|null   $statusCode [optional] The status code to set
495
 * @param array|null $headers    [optional] The headers to set
496
 *
497
 * @return JsonResponse
498
 */
499
function json(array|null $data = null, int|null $statusCode = null, array|null $headers = null): JsonResponse
500
{
501
    return responseFactory()->createJsonResponse($data, $statusCode, $headers);
502
}
503
504
/**
505
 * Return a new redirect response from the application.
506
 *
507
 * @param string|null $uri        [optional] The URI to redirect to
508
 * @param int|null    $statusCode [optional] The response status code
509
 * @param array|null  $headers    [optional] An array of response headers
510
 *
511
 * @return RedirectResponse
512
 */
513
function redirect(string|null $uri = null, int|null $statusCode = null, array|null $headers = null): RedirectResponse
514
{
515
    return responseFactory()->createRedirectResponse($uri, $statusCode, $headers);
516
}
517
518
/**
519
 * Return a new redirect response from the application for a given route.
520
 *
521
 * @param string     $route      The route to match
522
 * @param array|null $parameters [optional] Any parameters to set for dynamic routes
523
 * @param int|null   $statusCode [optional] The response status code
524
 * @param array|null $headers    [optional] An array of response headers
525
 *
526
 * @return RedirectResponse
527
 */
528
function redirectRoute(
529
    string $route,
530
    array|null $parameters = null,
531
    int|null $statusCode = null,
532
    array|null $headers = null
533
): RedirectResponse {
534
    return responseFactory()->route($route, $parameters, $statusCode, $headers);
535
}
536
537
/**
538
 * Redirect to a given uri, and abort the application.
539
 *
540
 * @param string|null $uri        [optional] The URI to redirect to
541
 * @param int|null    $statusCode [optional] The response status code
542
 * @param array|null  $headers    [optional] An array of response headers
543
 *
544
 * @throws HttpRedirectException
545
 *
546
 * @return never
547
 */
548
function redirectTo(
549
    string|null $uri = null,
550
    int|null $statusCode = null,
551
    array|null $headers = null
552
): never {
553
    Abort::redirect($uri, $statusCode, $headers);
554
}
555
556
/**
557
 * Return the session.
558
 *
559
 * @return Session
560
 */
561
function session(): Session
562
{
563
    return container()->getSingleton(Session::class);
564
}
565
566
/**
567
 * Get SMS.
568
 *
569
 * @return Sms
570
 */
571
function sms(): Sms
572
{
573
    return container()->getSingleton(Sms::class);
574
}
575
576
/**
577
 * Get a new SMS message.
578
 *
579
 * @param string|null $name [optional] The name
580
 *
581
 * @return SmsMessage
582
 */
583
function smsMessage(string|null $name = null): SmsMessage
584
{
585
    return \Valkyrja\sms()->createMessage($name);
586
}
587
588
/**
589
 * Get validator.
590
 *
591
 * @return Validation
592
 */
593
function validator(): Validation
594
{
595
    return container()->getSingleton(Validation::class);
596
}
597
598
/**
599
 * Helper function to get a new view.
600
 *
601
 * @return View
602
 */
603
function view(): View
604
{
605
    return container()->getSingleton(View::class);
606
}
607
608
/**
609
 * Helper function to get a new template.
610
 *
611
 * @param string $template  [optional] The template to use
612
 * @param array  $variables [optional] The variables to use
613
 *
614
 * @return Template
615
 */
616
function template(string $template, array $variables = []): Template
617
{
618
    return view()->createTemplate($template, $variables);
619
}
620
621
/**
622
 * Dump the passed variables and die.
623
 *
624
 * @param mixed ...$args The arguments to dump
625
 *
626
 * @return never
627
 */
628
function dd(...$args): never
629
{
630
    var_dump($args);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($args) looks like debug code. Are you sure you do not want to remove it?
Loading history...
631
632
    exit(1);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
633
}
634
635
/**
636
 * Helper function to get base path.
637
 *
638
 * @param string|null $path [optional] The path to append
639
 *
640
 * @return string
641
 */
642
function basePath(string|null $path = null): string
643
{
644
    return Directory::basePath($path);
645
}
646
647
/**
648
 * Helper function to get app path.
649
 *
650
 * @param string|null $path [optional] The path to append
651
 *
652
 * @return string
653
 */
654
function appPath(string|null $path = null): string
655
{
656
    return Directory::appPath($path);
657
}
658
659
/**
660
 * Helper function to get bootstrap path.
661
 *
662
 * @param string|null $path [optional] The path to append
663
 *
664
 * @return string
665
 */
666
function bootstrapPath(string|null $path = null): string
667
{
668
    return Directory::bootstrapPath($path);
669
}
670
671
/**
672
 * Helper function to get env path.
673
 *
674
 * @param string|null $path [optional] The path to append
675
 *
676
 * @return string
677
 */
678
function envPath(string|null $path = null): string
679
{
680
    return Directory::envPath($path);
681
}
682
683
/**
684
 * Helper function to get config path.
685
 *
686
 * @param string|null $path [optional] The path to append
687
 *
688
 * @return string
689
 */
690
function configPath(string|null $path = null): string
691
{
692
    return Directory::configPath($path);
693
}
694
695
/**
696
 * Helper function to get commands path.
697
 *
698
 * @param string|null $path [optional] The path to append
699
 *
700
 * @return string
701
 */
702
function commandsPath(string|null $path = null): string
703
{
704
    return Directory::commandsPath($path);
705
}
706
707
/**
708
 * Helper function to get events path.
709
 *
710
 * @param string|null $path [optional] The path to append
711
 *
712
 * @return string
713
 */
714
function eventsPath(string|null $path = null): string
715
{
716
    return Directory::eventsPath($path);
717
}
718
719
/**
720
 * Helper function to get routes path.
721
 *
722
 * @param string|null $path [optional] The path to append
723
 *
724
 * @return string
725
 */
726
function routesPath(string|null $path = null): string
727
{
728
    return Directory::routesPath($path);
729
}
730
731
/**
732
 * Helper function to get services path.
733
 *
734
 * @param string|null $path [optional] The path to append
735
 *
736
 * @return string
737
 */
738
function servicesPath(string|null $path = null): string
739
{
740
    return Directory::servicesPath($path);
741
}
742
743
/**
744
 * Helper function to get public path.
745
 *
746
 * @param string|null $path [optional] The path to append
747
 *
748
 * @return string
749
 */
750
function publicPath(string|null $path = null): string
751
{
752
    return Directory::publicPath($path);
753
}
754
755
/**
756
 * Helper function to get resources path.
757
 *
758
 * @param string|null $path [optional] The path to append
759
 *
760
 * @return string
761
 */
762
function resourcesPath(string|null $path = null): string
763
{
764
    return Directory::resourcesPath($path);
765
}
766
767
/**
768
 * Helper function to get storage path.
769
 *
770
 * @param string|null $path [optional] The path to append
771
 *
772
 * @return string
773
 */
774
function storagePath(string|null $path = null): string
775
{
776
    return Directory::storagePath($path);
777
}
778
779
/**
780
 * Helper function to get framework storage path.
781
 *
782
 * @param string|null $path [optional] The path to append
783
 *
784
 * @return string
785
 */
786
function frameworkStoragePath(string|null $path = null): string
787
{
788
    return Directory::frameworkStoragePath($path);
789
}
790
791
/**
792
 * Helper function to get cache path.
793
 *
794
 * @param string|null $path [optional] The path to append
795
 *
796
 * @return string
797
 */
798
function cachePath(string|null $path = null): string
799
{
800
    return Directory::cachePath($path);
801
}
802
803
/**
804
 * Helper function to get tests path.
805
 *
806
 * @param string|null $path [optional] The path to append
807
 *
808
 * @return string
809
 */
810
function testsPath(string|null $path = null): string
811
{
812
    return Directory::testsPath($path);
813
}
814
815
/**
816
 * Helper function to get vendor path.
817
 *
818
 * @param string|null $path [optional] The path to append
819
 *
820
 * @return string
821
 */
822
function vendorPath(string|null $path = null): string
823
{
824
    return Directory::vendorPath($path);
825
}
826