Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Jun 21, 2024
1 parent 2680ee6 commit 609ed2e
Show file tree
Hide file tree
Showing 25 changed files with 251 additions and 712 deletions.
29 changes: 14 additions & 15 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,20 @@ private function loadEnv(): void
*/
private function configureEnvironment(): void
{
static $initialized = false;
if (!$initialized) {

$environment = Env::get('APP_ENVIRONMENT', 'production');
if ($environment === 'debug') {
ini_set('display_errors', 1);
error_reporting(E_ALL);
} else {
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED
& ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
ini_set('display_errors', 0);
}

$initialized = true;
}
static $isInitialized = false;

if ($isInitialized)
return;

$environment = Env::get('APP_ENVIRONMENT', 'production');
$isInDebugMode = $environment === 'debug';

error_reporting(
$isInDebugMode ? E_ALL : (E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED)
);
ini_set('display_errors', $isInDebugMode ? 1 : 0);

$isInitialized = true;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class Controller
public function __construct()
{
$app = app();
$this->request = $app->request;
$this->request = $app->request;
$this->response = $app->response;
$this->view = $app->view;
$this->view = $app->view;

$this->registerDefaultMiddleware();
}
Expand Down Expand Up @@ -122,9 +122,13 @@ public function getAction(): string
/**
* Render the view.
*/
public function renderView(string $view, string|array $params = null, bool $withLayout = true, string $ext = '.php'): ?string
public function renderView(string $viewName, $viewData = null, bool $withLayout = true, string $fileExtension = '.php'): ?string
{
return $this->view->render($view, $ext)->withData($params)->withLayout($withLayout)->get();
return $this->view
->withData($viewData)
->withLayout($withLayout)
->render($viewName, $fileExtension)
->get();
}

/**
Expand Down
24 changes: 11 additions & 13 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@

function axm_autoloader(string $class)
{
static $classMap;

$classMap ??= [

'Axm' => AXM_PATH . DIRECTORY_SEPARATOR . 'Axm.php',
'Container' => AXM_PATH . DIRECTORY_SEPARATOR . 'Container.php',
'App' => AXM_PATH . DIRECTORY_SEPARATOR . 'App.php',
'Config' => AXM_PATH . DIRECTORY_SEPARATOR . 'Config.php',
'Env' => AXM_PATH . DIRECTORY_SEPARATOR . 'Env.php',
'Facade' => AXM_PATH . DIRECTORY_SEPARATOR . 'Facade.php',
'Controller' => AXM_PATH . DIRECTORY_SEPARATOR . 'Controller.php',
'BaseModel' => AXM_PATH . DIRECTORY_SEPARATOR . 'BaseModel.php',
static $classMap = [

'Axm' => AXM_PATH . DIRECTORY_SEPARATOR . 'Axm.php',
'Container' => AXM_PATH . DIRECTORY_SEPARATOR . 'Container.php',
'App' => AXM_PATH . DIRECTORY_SEPARATOR . 'App.php',
'Config' => AXM_PATH . DIRECTORY_SEPARATOR . 'Config.php',
'Env' => AXM_PATH . DIRECTORY_SEPARATOR . 'Env.php',
'Facade' => AXM_PATH . DIRECTORY_SEPARATOR . 'Facade.php',
'Controller' => AXM_PATH . DIRECTORY_SEPARATOR . 'Controller.php',
'BaseModel' => AXM_PATH . DIRECTORY_SEPARATOR . 'BaseModel.php',
];

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

}

spl_autoload_register('axm_autoloader');
spl_autoload_register('axm_autoloader');
121 changes: 45 additions & 76 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ function config(string $key = null, mixed $value = null)

if (!function_exists('app')) {
/**
* Return the Axm instance
* Returns the Axm instance or a value from the instance by alias.
*/
function app(?string $alias = null, $value = null): object
function app(string $alias = null, mixed $value = null): object
{
$instance = Axm::getApp();
$axmInstance = Axm::getApp();

if (null === $alias) {
return $instance;
return $axmInstance;
}

if (null !== $value) {
return $instance->$alias = $value;
$axmInstance->$alias = $value;
}

return $instance->$alias;
return $axmInstance->$alias;
}
}

Expand Down Expand Up @@ -87,9 +87,9 @@ function extend(string $layout)
function view(string $view, string|array $params = null, bool $show = false, bool $withLayout = false, string $ext = '.php'): ?string
{
return app('view', new View)
->render($view, $ext)
->withData($params)
->withLayout($withLayout)
->render($view, $ext)
->get();
}
}
Expand Down Expand Up @@ -392,13 +392,12 @@ function isLogged(): bool
if (!function_exists('old')) {

/**
* Used to show again if the data sent in
* html elements (input, select, textarea, etc) sent by the POST method exist.
* e.g.: old('name); **/
function old(string $value): string
* Returns the previously submitted value of a form field with the given name, if it exists.
*/
function old(string $fieldName): string
{
$input = app()->request->post();
return (isset($input[$value]) && !empty($input[$value])) ? $input[$value] : '';
$submittedValues = app()->request->post();
return $submittedValues[$fieldName] ?? '';
}
}

Expand Down Expand Up @@ -452,22 +451,15 @@ function now()

/**
* Create a new string helper instance or operate on a string.
* @return Stringable|object Returns a Stringable instance if a string argument is provided.
*/
function str(?string $string = null)
function str(?string $string = null): Stringable
{
if (is_null($string)) {
// Return a new class instance for chaining string methods
return new class {
public function __call($method, $params)
{
// Delegate method calls to the Str class
return Str::$method(...$params);
}
};
}
// Return a Stringable instance for the provided string
return Str::of($string);
return is_null($string) ? new class {
public function __call(string $method, array $arguments)
{
return Str::$method(...$arguments);
}
} : Str::of($string);
}
}

Expand Down Expand Up @@ -498,64 +490,41 @@ function to_object(array &$array): stdClass
}
}

/**
* Load one or multiple helpers.
*/
function helpers($helpers, ?string $customPath = null, string $separator = '_'): bool
{
return (new class ($helpers, $customPath, $separator) {
private $helpers;
private $customPath;
private $separator;

public function __construct($helpers, ?string $customPath, string $separator)
{
$this->helpers = $this->normalizeHelpers($helpers);
$this->customPath = $customPath ? rtrim($customPath, DIRECTORY_SEPARATOR) : null;
$this->separator = $separator;
}

private function normalizeHelpers($helpers): array
{
if (is_string($helpers)) {
return preg_split('/[\s,\.]+/', $helpers);
} elseif (!is_array($helpers)) {
throw new InvalidArgumentException('The $helpers variable must be an array.');
}

return $helpers;
}
if (!function_exists('helpers')) {

public function __invoke(): bool
{
$config = config('paths');
$appPath = $config['helpersPath'];
$axmHelpersPath = $config['helpersAxmPath'];
/**
* Load one or multiple helpers.
*/
function helpers($helpers, ?string $customPath = null, string $separator = '_'): bool
{
$helpers = is_string($helpers) ? preg_split('/[\s,\.]+/', $helpers) : $helpers;
$customPath = $customPath ? rtrim($customPath, DIRECTORY_SEPARATOR) : null;

foreach ($this->helpers as $helper) {
$helper = trim($helper) . $this->separator . 'helper.php';
$config = config('paths');
$appHelpersPath = $config['helpersPath'];
$axmHelpersPath = $config['helpersAxmPath'];

if ($this->customPath && is_file($customHelperFile = $this->customPath . DIRECTORY_SEPARATOR . $helper)) {
require_once $customHelperFile;
continue;
}
foreach ($helpers as $helper) {
$helperFile = "$helper$separator" . 'helper.php';

if (is_file($appHelperFile = $appPath . DIRECTORY_SEPARATOR . $helper)) {
require_once $appHelperFile;
continue;
}
if ($customPath && is_file($customPath . DIRECTORY_SEPARATOR . $helperFile)) {
require_once "$customPath/$helperFile";
continue;
}

if (is_file($axmHelperFile = $axmHelpersPath . DIRECTORY_SEPARATOR . $helper)) {
require_once $axmHelperFile;
continue;
$paths = [$appHelpersPath, $axmHelpersPath];
foreach ($paths as $path) {
if (is_file("$path/$helperFile")) {
require_once "$path/$helperFile";
continue 2;
}

throw new Exception("The helper '$axmHelperFile' does not exist in any of the specified paths.");
}

return true;
throw new Exception("The helper '$helperFile' does not exist in any of the specified paths.");
}
})();

return true;
}
}

if (!function_exists('getRouteParams')) {
Expand Down
Loading

0 comments on commit 609ed2e

Please sign in to comment.