Skip to content

Commit 609ed2e

Browse files
update file
1 parent 2680ee6 commit 609ed2e

25 files changed

+251
-712
lines changed

src/App.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,20 @@ private function loadEnv(): void
7676
*/
7777
private function configureEnvironment(): void
7878
{
79-
static $initialized = false;
80-
if (!$initialized) {
81-
82-
$environment = Env::get('APP_ENVIRONMENT', 'production');
83-
if ($environment === 'debug') {
84-
ini_set('display_errors', 1);
85-
error_reporting(E_ALL);
86-
} else {
87-
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED
88-
& ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
89-
ini_set('display_errors', 0);
90-
}
91-
92-
$initialized = true;
93-
}
79+
static $isInitialized = false;
80+
81+
if ($isInitialized)
82+
return;
83+
84+
$environment = Env::get('APP_ENVIRONMENT', 'production');
85+
$isInDebugMode = $environment === 'debug';
86+
87+
error_reporting(
88+
$isInDebugMode ? E_ALL : (E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED)
89+
);
90+
ini_set('display_errors', $isInDebugMode ? 1 : 0);
91+
92+
$isInitialized = true;
9493
}
9594

9695
/**

src/Controller.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ class Controller
5959
public function __construct()
6060
{
6161
$app = app();
62-
$this->request = $app->request;
62+
$this->request = $app->request;
6363
$this->response = $app->response;
64-
$this->view = $app->view;
64+
$this->view = $app->view;
6565

6666
$this->registerDefaultMiddleware();
6767
}
@@ -122,9 +122,13 @@ public function getAction(): string
122122
/**
123123
* Render the view.
124124
*/
125-
public function renderView(string $view, string|array $params = null, bool $withLayout = true, string $ext = '.php'): ?string
125+
public function renderView(string $viewName, $viewData = null, bool $withLayout = true, string $fileExtension = '.php'): ?string
126126
{
127-
return $this->view->render($view, $ext)->withData($params)->withLayout($withLayout)->get();
127+
return $this->view
128+
->withData($viewData)
129+
->withLayout($withLayout)
130+
->render($viewName, $fileExtension)
131+
->get();
128132
}
129133

130134
/**

src/autoload.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212

1313
function axm_autoloader(string $class)
1414
{
15-
static $classMap;
16-
17-
$classMap ??= [
18-
19-
'Axm' => AXM_PATH . DIRECTORY_SEPARATOR . 'Axm.php',
20-
'Container' => AXM_PATH . DIRECTORY_SEPARATOR . 'Container.php',
21-
'App' => AXM_PATH . DIRECTORY_SEPARATOR . 'App.php',
22-
'Config' => AXM_PATH . DIRECTORY_SEPARATOR . 'Config.php',
23-
'Env' => AXM_PATH . DIRECTORY_SEPARATOR . 'Env.php',
24-
'Facade' => AXM_PATH . DIRECTORY_SEPARATOR . 'Facade.php',
25-
'Controller' => AXM_PATH . DIRECTORY_SEPARATOR . 'Controller.php',
26-
'BaseModel' => AXM_PATH . DIRECTORY_SEPARATOR . 'BaseModel.php',
15+
static $classMap = [
16+
17+
'Axm' => AXM_PATH . DIRECTORY_SEPARATOR . 'Axm.php',
18+
'Container' => AXM_PATH . DIRECTORY_SEPARATOR . 'Container.php',
19+
'App' => AXM_PATH . DIRECTORY_SEPARATOR . 'App.php',
20+
'Config' => AXM_PATH . DIRECTORY_SEPARATOR . 'Config.php',
21+
'Env' => AXM_PATH . DIRECTORY_SEPARATOR . 'Env.php',
22+
'Facade' => AXM_PATH . DIRECTORY_SEPARATOR . 'Facade.php',
23+
'Controller' => AXM_PATH . DIRECTORY_SEPARATOR . 'Controller.php',
24+
'BaseModel' => AXM_PATH . DIRECTORY_SEPARATOR . 'BaseModel.php',
2725
];
2826

2927
if (isset($classMap[$class])) {
@@ -43,4 +41,4 @@ function axm_autoloader(string $class)
4341

4442
}
4543

46-
spl_autoload_register('axm_autoloader');
44+
spl_autoload_register('axm_autoloader');

src/functions.php

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ function config(string $key = null, mixed $value = null)
3030

3131
if (!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
392392
if (!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

561530
if (!function_exists('getRouteParams')) {

0 commit comments

Comments
 (0)