Skip to content

Commit be7a574

Browse files
update file
1 parent c8cb402 commit be7a574

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

src/functions.php

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ function app(?string $alias = null, $value = null): object
5050

5151
if (!function_exists('env')) {
5252

53-
function env(string $params, string|bool $default = null)
53+
function env(string $params, string|bool $default = null): bool|null|string
5454
{
55-
$env = Env::get($params, $default);
56-
return $env ?? $default;
55+
return Env::get($params, $default);
5756
}
5857
}
5958

@@ -137,16 +136,16 @@ function partials(string $partial_name, array $data = [])
137136
function cleanInput(mixed $data): mixed
138137
{
139138
return match (true) {
140-
is_array($data) => array_map('cleanInput', $data),
139+
is_array($data) => array_map('cleanInput', $data),
141140
is_object($data) => cleanInput((array) $data),
142-
is_email($data) => filter_var($data, FILTER_SANITIZE_EMAIL),
143-
is_url($data) => filter_var($data, FILTER_SANITIZE_URL),
144-
is_ip($data) => filter_var($data, FILTER_VALIDATE_IP),
141+
is_email($data) => filter_var($data, FILTER_SANITIZE_EMAIL),
142+
is_url($data) => filter_var($data, FILTER_SANITIZE_URL),
143+
is_ip($data) => filter_var($data, FILTER_VALIDATE_IP),
145144
is_string($data) => preg_replace('/[\x00-\x1F\x7F]/u', '', filter_var(trim($data), FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES)),
146-
is_int($data) => filter_var($data, FILTER_SANITIZE_NUMBER_INT),
147-
is_float($data) => filter_var($data, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION),
148-
is_bool($data) => filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
149-
is_null($data) => settype($data, 'NULL'),
145+
is_int($data) => filter_var($data, FILTER_SANITIZE_NUMBER_INT),
146+
is_float($data) => filter_var($data, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION),
147+
is_bool($data) => filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
148+
is_null($data) => settype($data, 'NULL'),
150149

151150
default => filter_var($data, FILTER_SANITIZE_SPECIAL_CHARS),
152151
};
@@ -284,8 +283,7 @@ function baseUrl(string $path = '/'): string
284283
{
285284
$scheme = $_SERVER['REQUEST_SCHEME'] ?? 'http';
286285
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
287-
$scriptName = basename($_SERVER['SCRIPT_NAME']);
288-
$basePath = str_replace($scriptName, '', $_SERVER['SCRIPT_NAME']);
286+
$basePath = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
289287
$path = trim($basePath . '/' . $path, '/');
290288

291289
return "{$scheme}://{$host}/{$path}";
@@ -297,12 +295,12 @@ function baseUrl(string $path = '/'): string
297295
/**
298296
* Generate the URL for an asset.
299297
*/
300-
function asset(string $path, ?string $basePath = null): string
298+
function asset(string $path, string $basePath = 'app/resources/assets/'): string
301299
{
302-
$basePath = $basePath ?? 'app/resources/assets/';
303-
$base = rtrim($basePath, '/') . '/' . ltrim($path, '/');
300+
$basePath = rtrim($basePath, '/');
301+
$path = ltrim($path, '/');
304302

305-
return baseUrl($base);
303+
return baseUrl("{$basePath}/{$path}");
306304
}
307305
}
308306

@@ -343,7 +341,7 @@ function back()
343341
* Redirect one page to another,
344342
* e.g.: redirect('login');
345343
**/
346-
function redirect($url)
344+
function redirect(string $url)
347345
{
348346
return app()
349347
->response
@@ -433,8 +431,7 @@ function getInfoUser(string $user, string $value)
433431
if (!function_exists('getUser')) {
434432

435433
/**
436-
* Returns a specific info of the user who
437-
* has successfully logged in.
434+
* Returns a specific info of the user who has successfully logged in.
438435
*/
439436
function getUser(string $value = null)
440437
{
@@ -504,63 +501,64 @@ function to_object(array &$array): stdClass
504501
}
505502
}
506503

507-
if (!function_exists('helpers')) {
504+
/**
505+
* Load one or multiple helpers.
506+
*/
507+
function helpers($helpers, ?string $customPath = null, string $separator = '_'): bool
508+
{
509+
return (new class ($helpers, $customPath, $separator) {
510+
private $helpers;
511+
private $customPath;
512+
private $separator;
508513

509-
/**
510-
* Load one or multiple helpers.
511-
*/
512-
function helpers(string|array $helpers, ?string $customPath = null, string $separator = '_'): bool
513-
{
514-
// Convert $helpers to an array if it's a string and split by spaces, commas, or dots
515-
if (is_string($helpers)) {
516-
$helpers = preg_split('/[\s,\.]+/', $helpers);
517-
} elseif (!is_array($helpers)) {
518-
throw new InvalidArgumentException('The $helpers variable must be an array.');
514+
public function __construct($helpers, ?string $customPath, string $separator)
515+
{
516+
$this->helpers = $this->normalizeHelpers($helpers);
517+
$this->customPath = $customPath ? rtrim($customPath, DIRECTORY_SEPARATOR) : null;
518+
$this->separator = $separator;
519519
}
520520

521-
$config = config('paths');
522-
523-
// Define paths for helper files
524-
$appPath = $config['helpersPath']; // Default application path
525-
$axmHelpersPath = $config['helpersAxmPath']; // Axm system path
521+
private function normalizeHelpers($helpers): array
522+
{
523+
if (is_string($helpers)) {
524+
return preg_split('/[\s,\.]+/', $helpers);
525+
} elseif (!is_array($helpers)) {
526+
throw new InvalidArgumentException('The $helpers variable must be an array.');
527+
}
526528

527-
// Load custom helpers from the provided path
528-
if ($customPath) {
529-
$customPath = rtrim($customPath, '/'); // Ensure the path does not end with a slash
529+
return $helpers;
530530
}
531531

532-
foreach ($helpers as $helper) {
533-
$helper = trim($helper) . $separator . 'helper.php';
532+
public function __invoke(): bool
533+
{
534+
$config = config('paths');
535+
$appPath = $config['helpersPath'];
536+
$axmHelpersPath = $config['helpersAxmPath'];
537+
538+
foreach ($this->helpers as $helper) {
539+
$helper = trim($helper) . $this->separator . 'helper.php';
534540

535-
// Try to load the helper from the custom path first
536-
if ($customPath) {
537-
$customHelperFile = $customPath . DIRECTORY_SEPARATOR . $helper;
538-
if (is_file($customHelperFile)) {
539-
require_once ($customHelperFile);
541+
if ($this->customPath && is_file($customHelperFile = $this->customPath . DIRECTORY_SEPARATOR . $helper)) {
542+
require_once $customHelperFile;
540543
continue;
541544
}
542-
}
543545

544-
// Try to load the helper from the default application path
545-
$appHelperFile = $appPath . DIRECTORY_SEPARATOR . $helper;
546-
if (is_file($appHelperFile)) {
547-
require_once ($appHelperFile);
548-
continue;
549-
}
546+
if (is_file($appHelperFile = $appPath . DIRECTORY_SEPARATOR . $helper)) {
547+
require_once $appHelperFile;
548+
continue;
549+
}
550+
551+
if (is_file($axmHelperFile = $axmHelpersPath . DIRECTORY_SEPARATOR . $helper)) {
552+
require_once $axmHelperFile;
553+
continue;
554+
}
550555

551-
// Try to load the helper from the Axm system path
552-
$axmHelperFile = $axmHelpersPath . DIRECTORY_SEPARATOR . $helper;
553-
if (is_file($axmHelperFile)) {
554-
require_once ($axmHelperFile);
555-
continue;
556+
throw new Exception("The helper '$axmHelperFile' does not exist in any of the specified paths.");
556557
}
557558

558-
// If the helper is not found in any of the specified locations, throw an exception
559-
throw new Exception("The helper '$axmHelperFile' does not exist in any of the specified paths.");
559+
return true;
560560
}
561-
562-
return true;
563-
}
561+
})();
564562
}
565563

566564
if (!function_exists('getRouteParams')) {
@@ -599,9 +597,9 @@ function getUri(): string
599597
*/
600598
function class_basename($class): string
601599
{
602-
return is_object($class)
600+
return (string) is_object($class)
603601
? basename(str_replace('\\', '/', get_class($class)))
604-
: (string) basename(str_replace('\\', '/', $class));
602+
: basename(str_replace('\\', '/', $class));
605603
}
606604
}
607605

0 commit comments

Comments
 (0)