Skip to content

Commit

Permalink
modified: .github/workflows/Bump Version.yml
Browse files Browse the repository at this point in the history
	modified:   src/Application.php
	modified:   src/Axm.php
	modified:   src/BaseConfig.php
	modified:   src/Controller.php
	modified:   src/Database.php
	modified:   src/DbModel.php
	modified:   src/axm_helper.php
	modified:   src/bootstrap.php
  • Loading branch information
juancristobalgd1 committed Nov 22, 2023
1 parent 3c6df63 commit da4115d
Show file tree
Hide file tree
Showing 9 changed files with 1,029 additions and 1,075 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Bump Version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
run: |
if git describe --tags --abbrev=0 > /dev/null 2>&1; then
VERSION=$(git describe --tags --abbrev=0)
echo "current_version=$VERSION" >> $GITHUB_ENV
else
echo "No version tags found in the repository"
exit 1
VERSION="v1.0.0"
echo "Setting version to $VERSION as no tags were found."
fi
echo "current_version=$VERSION" >> $GITHUB_ENV
shell: bash

- name: Bump release version
Expand Down
73 changes: 42 additions & 31 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ abstract class Application
*/
private string $socketToken;


/**
* Constructor for the Application class.
* Initializes the application.
Expand All @@ -48,7 +47,6 @@ public function __construct($config = [])

/**
* Get the container instance.
*
* @return Container The container instance.
*/
public function getContainer(): Container
Expand Down Expand Up @@ -87,16 +85,18 @@ private function openDefaultSystemConfigurationFiles(): void
* @return mixed The configuration value for the specified key, or the entire
* configuration if no key is provided.
*/
public function config(string $key = '')
public function config(string $key = null, $rootBaseConfig = null)
{
$key = strtolower($key);
$config = $this->config;
if (is_null($key))
return $this->config;

return empty($key)
? $config
: (strpos($key, '/')
? $config->load($key)
: $config->get($key));
if (str_contains($key, '/')) {
$path = is_null($rootBaseConfig) ? $this->config::ROOT_PATH_CONFIG .
$key : $rootBaseConfig;
return $this->config->load($path);
}

return $this->config->get($key);
}

/**
Expand All @@ -114,25 +114,30 @@ public function openRoutesUser(): void
}

/**
* Generate security tokens, including CSRF tokens.
* Load a configuration file.
*
* @param string $path The path to the configuration file.
* @param string $root An optional root directory for the path.
*
* @return mixed The result of the configuration file load operation.
*/
private function generateTokens(): void
public function load(string $path, string $root = APP_PATH)
{
$this->generateCsrfToken();
$filePath = $root . DIRECTORY_SEPARATOR . str_replace(
'.',
DIRECTORY_SEPARATOR,
pathinfo($path, PATHINFO_FILENAME)
) . '.' . pathinfo($path, PATHINFO_EXTENSION);

return $this->container->load($filePath);
}

/**
* Load a configuration file into the container.
*
* @param string $path The path to the configuration file.
* @param string $root An optional root directory for the path.
* Generate security tokens, including CSRF tokens.
*/
public function load(string $path, string $root = APP_PATH): void
private function generateTokens(): void
{
$path = $root . $path;
$path = str_replace('.', DIRECTORY_SEPARATOR, $path);

$this->container->load($path);
$this->generateCsrfToken();
}

/**
Expand All @@ -146,7 +151,6 @@ public function isProduction(): bool

/**
* Check if the user is logged in.
*
* @return bool True if the user is logged in, false otherwise.
*/
public function isLogged(): bool
Expand All @@ -157,16 +161,25 @@ public function isLogged(): bool
/**
* Set the user from the session variable.
*/
private function setUser(): void
private function getUser(): void
{
$this->user = function () {
return $this->session->get('user', true);
};
}

/**
* Set the user from the session variable.
*/
private function setUser(): void
{
$this->user = function () {
return $this->session->set('user', true);
};
}

/**
* Log out the user.
*
* @param string $path The optional path to redirect after logout.
*/
public function logout(string $path = '/'): void
Expand All @@ -177,7 +190,6 @@ public function logout(string $path = '/'): void

/**
* Get the event handler intent.
*
* @return mixed The event handler intent.
*/
public function event()
Expand Down Expand Up @@ -257,8 +269,8 @@ public function getLocale()
/**
* Generate a CSRF token.
*
* This method generates a CSRF token and stores it in a cookie. If a token already exists in the cookie,
* it is reused.
* This method generates a CSRF token and stores it in a cookie.
* If a token already exists in the cookie, it is reused.
* @return string The generated CSRF token.
*/
public function generateCsrfToken(): string
Expand All @@ -279,7 +291,7 @@ public function generateCsrfToken(): string
*/
public function hasCsrfToken(string $token): bool
{
return ($_COOKIE['csrfToken'] === $token);
return $_COOKIE['csrfToken'] === $token;
}

/**
Expand Down Expand Up @@ -325,9 +337,8 @@ public function getHelpers(): array
*/
public function user(string $value = null)
{
if (is_null($value)) {
if (is_null($value))
return $this->container->get('user');
}

return $this->container
->get('user')
Expand Down
105 changes: 12 additions & 93 deletions src/Axm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,9 @@
*/
class Axm
{
public static $framework = 'Axm Framework';

public static $framework = 'Axm Framework';
private static $version;

/**
* @var array Class map used by the AXM autoloading mechanism.
* The array keys are the class names, and the values are the corresponding class file paths.
* @since 1.1.5
*/
public static $classMap = [];

/**
* Request path to use.
*
* @var string
*/
protected $path;

public static $_environment;
public static $_environment;
private static $_app;
private static $initialized = false;

Expand Down Expand Up @@ -88,56 +72,10 @@ protected static function initSystemHandlers()
});
}

\Axm\HandlerErrors::make(new \Whoops\Handler\PrettyPageHandler, new \Whoops\Run);
// if (env('AXM_ENABLE_EXCEPTION_HANDLER', true)) {
// set_exception_handler(fn ($e) => self::handleException($e));
// }

// if (env('AXM_ENABLE_ERROR_HANDLER', true)) {
// set_error_handler([self::class, 'handleError'], error_reporting());
// }
}

/**
* Manages and displays application features.
*/
private static function handleException(\Throwable $e)
{
// Disable error capturing to avoid recursive errors
restore_error_handler();
restore_exception_handler();

if (self::is_cli()) {
return AxmCLIException::handleCLIException($e);
}

return AxmException::handleException($e);
if (self::$_environment !== 'production')
\Axm\HandlerErrors::make(new \Whoops\Handler\PrettyPageHandler, new \Whoops\Run);
}

/**
* Handles and displays the captured PHP error.
*
* This method displays the error in HTML when there is
* no active error handler.
* @param int $code Error code.
* @param string $msg Error message (optional).
* @param string $file Error file.
* @param int $line Error line.
* @return \AxmException Custom error exception object.
*/
public static function handleError($code, $message, $file, $line)
{
if (self::is_cli()) {
// Create a custom error exception object.
$e = new \ErrorException($message, $code, 0, $file, $line);
return AxmCLIException::handleCLIException($e);
}

// Create a custom error exception object.
throw new \ErrorException($message, $code, 0, $file, $line);
}


/**
* Checks if the application is running in a CLI environment.
*/
Expand All @@ -156,14 +94,15 @@ public static function is_cli(): bool
private static function initializeEnvironment()
{
// Obtain the value of AXM_ENVIRONMENT or use a default value
static::$_environment = $environment = env('AXM_ENVIRONMENT', 'production');
static::$_environment = $env = env('AXM_ENVIRONMENT', 'production');

// Configuring environment-based error handling.
if ($environment === 'debug') {
if ($env === 'debug') {
error_reporting(E_ALL);
ini_set('display_errors', '1');
} else {
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED
& ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
ini_set('display_errors', '0');
}
}
Expand All @@ -173,9 +112,9 @@ private static function initializeEnvironment()
*
* @return mixed The application instance.
*/
public static function startApplication()
public static function startApplication(array $config = null)
{
return self::createApplication('Axm\\initApplication');
return self::createApplication('Axm\\initApplication', $config);
}

/**
Expand All @@ -184,7 +123,7 @@ public static function startApplication()
* @param string $class The application class name.
* @param mixed $config Application configuration.
*/
private static function createApplication(string $class, $config = null)
private static function createApplication(string $class, array $config = null)
{
self::ensureInitialized();
return new $class($config);
Expand Down Expand Up @@ -309,32 +248,12 @@ public static function getPerformanceStats(): array
public static function setApplication(Application $app): void
{
if (self::$_app !== null) {
throw new AxmException(self::t('axm', 'Axm application can only be created once.'));
throw new AxmException('Axm application can only be created once.');
}

self::$_app = $app;
}

/**
* Translation method that allows translating messages in the system.
*
* @param string $category The translation category.
* @param string $message The original message.
* @param array $params Parameters to replace in the original message.
* @param bool $isHtml Whether the message should be treated as HTML.
* @return string The translated message.
*/
public static function t($category, $message, $params = [], $language = null, bool $isHtml = true)
{
$source = ($category === 'axm') ? 'coreMessages' : 'messages';

$message = vsprintf($message, array_map(function ($value) use ($isHtml) {
return !$isHtml ? $value : '<b>' . $value . '</b>';
}, $params));

return $message;
}

/**
* Get the installed version of the Axm Framework from composer.json.
*
Expand Down
5 changes: 3 additions & 2 deletions src/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaseConfig
private array $config = [];
private array $cache = [];
private array $loadedFiles = [];

const ROOT_PATH_CONFIG = APP_PATH . DIRECTORY_SEPARATOR . 'Config';

private function __construct()
{
Expand Down Expand Up @@ -55,6 +55,8 @@ public function load(string $file, bool $merge = true)
return $this->config;
}

$file = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $file);

if (!file_exists($file)) {
throw new AxmException('Configuration file not found: ' . $file);
}
Expand Down Expand Up @@ -89,7 +91,6 @@ public function load(string $file, bool $merge = true)
return (array) $this->config;
}


/**
* Recursive load a configuration file.
*
Expand Down
10 changes: 7 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ public function getAction(): string
* @param string $view
* @param array $param
*/
public function renderView(string $view, ?array $params = [], bool $buffer = true, string $ext = '.php'): ?string
{
public function renderView(
string $view,
?array $params = [],
bool $buffer = true,
string $ext = '.php'
): ?string {

return $this->view::render($view, $params, $buffer, $ext);
}

/**
* Register a Middleware in the Controller
*
* @param BaseMiddleware $middleware
*/
public function registerMiddleware(BaseMiddleware $middleware): void
Expand Down
Loading

0 comments on commit da4115d

Please sign in to comment.