@@ -30,21 +30,21 @@ function config(string $key = null, mixed $value = null)
30
30
31
31
if (!function_exists ('app ' )) {
32
32
/**
33
- * Return the Axm instance
33
+ * Returns the Axm instance or a value from the instance by alias.
34
34
*/
35
- function app (? string $ alias = null , $ value = null ): object
35
+ function app (string $ alias = null , mixed $ value = null ): object
36
36
{
37
- $ instance = Axm::getApp ();
37
+ $ axmInstance = Axm::getApp ();
38
38
39
39
if (null === $ alias ) {
40
- return $ instance ;
40
+ return $ axmInstance ;
41
41
}
42
42
43
43
if (null !== $ value ) {
44
- return $ instance ->$ alias = $ value ;
44
+ $ axmInstance ->$ alias = $ value ;
45
45
}
46
46
47
- return $ instance ->$ alias ;
47
+ return $ axmInstance ->$ alias ;
48
48
}
49
49
}
50
50
@@ -87,9 +87,9 @@ function extend(string $layout)
87
87
function view (string $ view , string |array $ params = null , bool $ show = false , bool $ withLayout = false , string $ ext = '.php ' ): ?string
88
88
{
89
89
return app ('view ' , new View )
90
- ->render ($ view , $ ext )
91
90
->withData ($ params )
92
91
->withLayout ($ withLayout )
92
+ ->render ($ view , $ ext )
93
93
->get ();
94
94
}
95
95
}
@@ -392,13 +392,12 @@ function isLogged(): bool
392
392
if (!function_exists ('old ' )) {
393
393
394
394
/**
395
- * Used to show again if the data sent in
396
- * html elements (input, select, textarea, etc) sent by the POST method exist.
397
- * e.g.: old('name); **/
398
- function old (string $ value ): string
395
+ * Returns the previously submitted value of a form field with the given name, if it exists.
396
+ */
397
+ function old (string $ fieldName ): string
399
398
{
400
- $ input = app ()->request ->post ();
401
- return ( isset ( $ input [ $ value ]) && ! empty ( $ input [ $ value ])) ? $ input [ $ value ] : '' ;
399
+ $ submittedValues = app ()->request ->post ();
400
+ return $ submittedValues [ $ fieldName ] ?? '' ;
402
401
}
403
402
}
404
403
@@ -452,22 +451,15 @@ function now()
452
451
453
452
/**
454
453
* Create a new string helper instance or operate on a string.
455
- * @return Stringable|object Returns a Stringable instance if a string argument is provided.
456
454
*/
457
- function str (?string $ string = null )
455
+ function str (?string $ string = null ): Stringable
458
456
{
459
- if (is_null ($ string )) {
460
- // Return a new class instance for chaining string methods
461
- return new class {
462
- public function __call ($ method , $ params )
463
- {
464
- // Delegate method calls to the Str class
465
- return Str::$ method (...$ params );
466
- }
467
- };
468
- }
469
- // Return a Stringable instance for the provided string
470
- return Str::of ($ string );
457
+ return is_null ($ string ) ? new class {
458
+ public function __call (string $ method , array $ arguments )
459
+ {
460
+ return Str::$ method (...$ arguments );
461
+ }
462
+ } : Str::of ($ string );
471
463
}
472
464
}
473
465
@@ -498,64 +490,41 @@ function to_object(array &$array): stdClass
498
490
}
499
491
}
500
492
501
- /**
502
- * Load one or multiple helpers.
503
- */
504
- function helpers ($ helpers , ?string $ customPath = null , string $ separator = '_ ' ): bool
505
- {
506
- return (new class ($ helpers , $ customPath , $ separator ) {
507
- private $ helpers ;
508
- private $ customPath ;
509
- private $ separator ;
510
-
511
- public function __construct ($ helpers , ?string $ customPath , string $ separator )
512
- {
513
- $ this ->helpers = $ this ->normalizeHelpers ($ helpers );
514
- $ this ->customPath = $ customPath ? rtrim ($ customPath , DIRECTORY_SEPARATOR ) : null ;
515
- $ this ->separator = $ separator ;
516
- }
517
-
518
- private function normalizeHelpers ($ helpers ): array
519
- {
520
- if (is_string ($ helpers )) {
521
- return preg_split ('/[\s,\.]+/ ' , $ helpers );
522
- } elseif (!is_array ($ helpers )) {
523
- throw new InvalidArgumentException ('The $helpers variable must be an array. ' );
524
- }
525
-
526
- return $ helpers ;
527
- }
493
+ if (!function_exists ('helpers ' )) {
528
494
529
- public function __invoke (): bool
530
- {
531
- $ config = config ('paths ' );
532
- $ appPath = $ config ['helpersPath ' ];
533
- $ axmHelpersPath = $ config ['helpersAxmPath ' ];
495
+ /**
496
+ * Load one or multiple helpers.
497
+ */
498
+ function helpers ($ helpers , ?string $ customPath = null , string $ separator = '_ ' ): bool
499
+ {
500
+ $ helpers = is_string ($ helpers ) ? preg_split ('/[\s,\.]+/ ' , $ helpers ) : $ helpers ;
501
+ $ customPath = $ customPath ? rtrim ($ customPath , DIRECTORY_SEPARATOR ) : null ;
534
502
535
- foreach ($ this ->helpers as $ helper ) {
536
- $ helper = trim ($ helper ) . $ this ->separator . 'helper.php ' ;
503
+ $ config = config ('paths ' );
504
+ $ appHelpersPath = $ config ['helpersPath ' ];
505
+ $ axmHelpersPath = $ config ['helpersAxmPath ' ];
537
506
538
- if ($ this ->customPath && is_file ($ customHelperFile = $ this ->customPath . DIRECTORY_SEPARATOR . $ helper )) {
539
- require_once $ customHelperFile ;
540
- continue ;
541
- }
507
+ foreach ($ helpers as $ helper ) {
508
+ $ helperFile = "$ helper$ separator " . 'helper.php ' ;
542
509
543
- if (is_file ( $ appHelperFile = $ appPath . DIRECTORY_SEPARATOR . $ helper )) {
544
- require_once $ appHelperFile ;
545
- continue ;
546
- }
510
+ if ($ customPath && is_file ( $ customPath . DIRECTORY_SEPARATOR . $ helperFile )) {
511
+ require_once " $ customPath / $ helperFile " ;
512
+ continue ;
513
+ }
547
514
548
- if (is_file ($ axmHelperFile = $ axmHelpersPath . DIRECTORY_SEPARATOR . $ helper )) {
549
- require_once $ axmHelperFile ;
550
- continue ;
515
+ $ paths = [$ appHelpersPath , $ axmHelpersPath ];
516
+ foreach ($ paths as $ path ) {
517
+ if (is_file ("$ path/ $ helperFile " )) {
518
+ require_once "$ path/ $ helperFile " ;
519
+ continue 2 ;
551
520
}
552
-
553
- throw new Exception ("The helper ' $ axmHelperFile' does not exist in any of the specified paths. " );
554
521
}
555
522
556
- return true ;
523
+ throw new Exception ( " The helper ' $ helperFile ' does not exist in any of the specified paths. " ) ;
557
524
}
558
- })();
525
+
526
+ return true ;
527
+ }
559
528
}
560
529
561
530
if (!function_exists ('getRouteParams ' )) {
0 commit comments