@@ -30,21 +30,21 @@ function config(string $key = null, mixed $value = null)
3030
3131if (!function_exists ('app ' )) {
3232 /**
33- * Return the Axm instance
33+ * Returns the Axm instance or a value from the instance by alias.
3434 */
35- function app (? string $ alias = null , $ value = null ): object
35+ function app (string $ alias = null , mixed $ value = null ): object
3636 {
37- $ instance = Axm::getApp ();
37+ $ axmInstance = Axm::getApp ();
3838
3939 if (null === $ alias ) {
40- return $ instance ;
40+ return $ axmInstance ;
4141 }
4242
4343 if (null !== $ value ) {
44- return $ instance ->$ alias = $ value ;
44+ $ axmInstance ->$ alias = $ value ;
4545 }
4646
47- return $ instance ->$ alias ;
47+ return $ axmInstance ->$ alias ;
4848 }
4949}
5050
@@ -87,9 +87,9 @@ function extend(string $layout)
8787 function view (string $ view , string |array $ params = null , bool $ show = false , bool $ withLayout = false , string $ ext = '.php ' ): ?string
8888 {
8989 return app ('view ' , new View )
90- ->render ($ view , $ ext )
9190 ->withData ($ params )
9291 ->withLayout ($ withLayout )
92+ ->render ($ view , $ ext )
9393 ->get ();
9494 }
9595}
@@ -392,13 +392,12 @@ function isLogged(): bool
392392if (!function_exists ('old ' )) {
393393
394394 /**
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
399398 {
400- $ input = app ()->request ->post ();
401- return ( isset ( $ input [ $ value ]) && ! empty ( $ input [ $ value ])) ? $ input [ $ value ] : '' ;
399+ $ submittedValues = app ()->request ->post ();
400+ return $ submittedValues [ $ fieldName ] ?? '' ;
402401 }
403402}
404403
@@ -452,22 +451,15 @@ function now()
452451
453452 /**
454453 * 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.
456454 */
457- function str (?string $ string = null )
455+ function str (?string $ string = null ): Stringable
458456 {
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 );
471463 }
472464}
473465
@@ -498,64 +490,41 @@ function to_object(array &$array): stdClass
498490 }
499491}
500492
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 ' )) {
528494
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 ;
534502
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 ' ];
537506
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 ' ;
542509
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+ }
547514
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 ;
551520 }
552-
553- throw new Exception ("The helper ' $ axmHelperFile' does not exist in any of the specified paths. " );
554521 }
555522
556- return true ;
523+ throw new Exception ( " The helper ' $ helperFile ' does not exist in any of the specified paths. " ) ;
557524 }
558- })();
525+
526+ return true ;
527+ }
559528}
560529
561530if (!function_exists ('getRouteParams ' )) {
0 commit comments