From e0592f6eeff422fb609d809dd7c7e5d4d94788c2 Mon Sep 17 00:00:00 2001 From: Juan Cristobal <65052633+juancristobalgd1@users.noreply.github.com> Date: Sun, 24 Dec 2023 07:13:59 +0100 Subject: [PATCH] updates files --- src/Application.php | 49 ++----- src/Axm.php | 193 +++++++++----------------- src/BaseConfig.php | 129 ++++++++++------- src/Controller.php | 114 ++++++++++----- src/HandlerErrors.php | 53 +++++-- src/bootstrap.php | 20 +-- src/{axm_helper.php => functions.php} | 16 ++- 7 files changed, 306 insertions(+), 268 deletions(-) rename src/{axm_helper.php => functions.php} (98%) diff --git a/src/Application.php b/src/Application.php index 3e340c7..0d6afec 100644 --- a/src/Application.php +++ b/src/Application.php @@ -63,20 +63,9 @@ private function init(): void $this->getContainer() ->loadFromDirectory(APP_PATH . DIRECTORY_SEPARATOR . 'Providers'); - $this->openDefaultSystemConfigurationFiles(); $this->openRoutesUser(); } - /** - * Open the default system configuration files. - */ - private function openDefaultSystemConfigurationFiles(): void - { - $path = APP_PATH . DIRECTORY_SEPARATOR . 'Config'; - $this->config->load($path . DIRECTORY_SEPARATOR . 'App.php'); - $this->config->load($path . DIRECTORY_SEPARATOR . 'Paths.php'); - } - /** * Get the application configuration. * @@ -86,17 +75,8 @@ private function openDefaultSystemConfigurationFiles(): void */ public function config(string $key = null, $rootBaseConfig = null) { - $config = Axm\BaseConfig::make(); - if (is_null($key)) - return $config; - - if (str_contains($key, '/')) { - $path = is_null($rootBaseConfig) ? $config::ROOT_PATH_CONFIG . - $key : $rootBaseConfig; - return $config->load($path); - } - - return $config->get($key); + // return $config->get($key); + return config($key, $rootBaseConfig); } /** @@ -108,9 +88,7 @@ public function openRoutesUser(): void $files = glob(ROOT_PATH . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . "*$ext"); - foreach ($files as $file) { - include_once($file); - } + foreach ($files as $file) include_once($file); } /** @@ -204,9 +182,10 @@ public function simpleKeyLogin(array $data): bool } /** - * @param array $keys - * @param array $values - * + * multipleKeyLogin + * + * @param mixed $keys + * @param mixed $values * @return bool */ public function multipleKeyLogin(array $keys, array $values): bool @@ -266,7 +245,7 @@ public function login(array $fields, array $values = []): bool return $check; } catch (\Throwable $th) { // Handle errors and throw a more descriptive exception - throw new \Exception("Error during login: " . $th->getMessage()); + throw new Exception(sprintf('Error during login: %s', $th->getMessage())); } } @@ -464,8 +443,7 @@ public function getHelpers(): array */ public function user(string $value = null) { - if (is_null($value)) - return $this->container->get('user'); + if (is_null($value)) return $this->container->get('user'); return $this->container ->get('user') @@ -554,11 +532,10 @@ public function removeService(string $alias) */ public function __get($name) { - if ($name === 'user') { - $this->setUser(); - } - - return $this->container->get($name); + return match ($name) { + ($name === 'user') => $this->setUser(), + default => $this->container->get($name), + }; } /** diff --git a/src/Axm.php b/src/Axm.php index 4438616..f80fbbd 100644 --- a/src/Axm.php +++ b/src/Axm.php @@ -21,32 +21,12 @@ class Axm private static $version; public static $_environment; private static $_app; - private static $initialized; - - - /** - * Ensures that the class is initialized. - * - * This method checks if the class has already been initialized. If not, - * it sets the initialization flag to true and calls the initialize method. - * This helps to ensure that the necessary setup or initialization is performed - * only once. - * @return void - */ - private static function ensureInitialized() - { - if (!self::$initialized) { - self::$initialized = true; - self::initialize(); - } - } /** * Initializes the application. * * This method performs the initial setup for the application. It includes * bootstrapping, initializing system handlers, and detecting the environment. - * It is typically called after ensuring that the class is initialized. * @return void */ private static function initialize() @@ -83,7 +63,6 @@ public static function boot(string $bootstrapFileName = 'bootstrap.php'): void * This method is responsible for setting up system-specific error handlers, * such as those defined in the `Axm\HandlerErrors` class. It ensures that the * necessary error handling configurations are applied. - * * @return void */ protected static function initSystemHandlers() @@ -101,9 +80,7 @@ protected static function initSystemHandlers() */ public static function is_cli(): bool { - if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) { - return true; - } + if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) return true; return !isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['REQUEST_METHOD']); } @@ -116,126 +93,101 @@ public static function is_cli(): bool * according to the specified environment. * @return void */ - private static function initializeEnvironment() + private static function initializeEnvironment(string $appEnvironment = null) { // Obtain the value of APP_ENVIRONMENT or use a default value - static::$_environment = $env = env('APP_ENVIRONMENT', 'production'); + $environment = $appEnvironment ?? env('APP_ENVIRONMENT', 'production'); - if ($env === '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); - } + // Set error reporting based on the environment + ($environment === 'debug') + ? self::configureForDebug() + : self::configureForProduction(); + } + + /** + * Configure error reporting and display settings for debugging. + * @return void + */ + private static function configureForDebug() + { + ini_set('display_errors', 1); + error_reporting(E_ALL); + } + + /** + * Configure error reporting and display settings for production. + * @return void + */ + private static function configureForProduction() + { + error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED + & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); + ini_set('display_errors', 0); } /** - * Starts the application. + * Start the application using the specified configuration. * - * This method initiates the application by calling the specified initialization - * function and passing the provided configuration. It returns the initialized - * application instance. - * @param array|null $config An optional array of configuration parameters. - * @return object The initialized application instance. + * @param array|null $config An optional configuration array. + * @return Application The initialized application instance. */ - public static function startApplication(array $config = null) + public static function startApplication(array|null $config = null) { return self::buildApplication('Axm\\initApplication', $config); } /** - * Initializes the application. + * Build and initialize an application instance of the specified class with optional configuration. * - * @param string $class The application class name. - * @param mixed $config Application configuration. + * @param string $class The fully qualified class name of the application. + * @param array|null $config An optional configuration array. + * @return Application The initialized application instance. */ private static function buildApplication(string $class, array $config = null) { - self::ensureInitialized(); + self::initialize(); return new $class($config); } /** - * Creates a console application instance. + * Create and initialize a console application instance with optional configuration. * - * @param mixed $config application configuration. - * If a string, it is treated as the path of the file that contains the configuration; - * If an array, it is the actual configuration information. - * @return Console + * @param array|null $config An optional configuration array. + * @return ConsoleApplication The initialized console application instance. */ - public static function createConsoleApplication($config = null) + public static function createConsoleApplication(array|null $config = null) { return self::buildApplication('Axm\\Console\\ConsoleApplication', $config); } /** - * Gets or registers a service in the application container. + * Get or register a service in the application. * - * This method allows getting or registering a service in the application container. - * If no arguments are provided, it returns the singleton instance of the application. - * If an alias is provided, it checks if the service is registered and returns it. - * If a callback is provided, it registers the service with the specified alias and callback. - * @param string|object|null $alias The alias or object of the service. - * @param Closure|null $callback The callback function to create the service. - * @param bool $shared Whether the service should be shared (singleton) or not. - * @return object The registered or retrieved service instance. - * @throws InvalidArgumentException If the provided alias or callback is not valid. - * @throws RuntimeException If the requested service is not found. + * @param string|object|null $alias The alias or object representing the service. + * @param Closure|null $callback A callback to create the service if not registered. + * @param bool $shared Indicates if the service should be shared. + * @return mixed The registered service or the result of the callback. + * @throws RuntimeException If the service is not found. */ - public static function app($alias = null, Closure $callback = null, bool $shared = false) + public static function app(string|object|null $alias = null, Closure|null $callback = null, bool $shared = false) { if ($alias === null && $callback === null) { return self::getSingleton(); } - if ($alias !== null && !is_string($alias) && !is_object($alias)) { - throw new InvalidArgumentException('Alias must be a string, an object, or null.'); - } - - if ($callback !== null && !($callback instanceof Closure)) { - throw new InvalidArgumentException('Callback must be a Closure or null.'); - } - - $class = null; - if (is_object($alias)) { - $class = $alias; - $alias = get_class($alias); - } - - $serviceRegistred = self::$_app->hasService($alias); - if ($alias !== null && $callback === null) { - - if (is_object($alias) && !$serviceRegistred) { - return self::$_app->addService($alias, $class, $shared); - } + $alias = is_object($alias) ? get_class($alias) : $alias; - if (class_exists($alias) && !$serviceRegistred) { - return self::$_app->addService($alias, fn () => new $alias, $shared); - } - - $service = self::$_app->getService($alias); - if ($service === null) { - throw new RuntimeException("Service '{$alias}' not found."); - } - - if (class_exists($alias) && $serviceRegistred) { - return $service; - } - - return $service; - } - - if (!$serviceRegistred) { + if (!self::$_app->hasService($alias)) { + $callback = $callback ?? fn () => new $alias; return self::$_app->addService($alias, $callback, $shared); } + + return self::$_app->getService($alias) + ?? throw new RuntimeException(sprintf('Service %s not found.', $alias)); } /** - * Gets the singleton instance of the application. - * - * This method returns the singleton instance of the application container. + * Get the singleton instance of the application. * @return Application|null The singleton instance of the application, or null if not set. */ private static function getSingleton(): ?Application @@ -244,11 +196,8 @@ private static function getSingleton(): ?Application } /** - * Gets the current application environment. - * - * This method returns the current application environment, which is typically - * determined based on the value of the `APP_ENVIRONMENT` variable or a default value. - * @return string The current application environment. + * Get the current environment of the application. + * @return string The current environment, such as 'production', 'debug', etc. */ public static function getEnvironment() { @@ -256,25 +205,20 @@ public static function getEnvironment() } /** - * Checks if the application is in production environment. - * - * This method returns true if the current application environment is set to - * production, as determined by the value of the `APP_ENVIRONMENT` variable. - * @return bool True if the application is in production environment, false otherwise. + * Check if the application is in production environment. + * @return bool True if the environment is 'production', false otherwise. */ public static function isProduction(): bool { - return static::$_environment === ENV_PRODUCTION; + return static::$_environment === 'production'; } /** - * Gets performance statistics for the script execution. + * Get performance statistics of the application. * - * This method returns an array containing performance statistics for the script execution. - * It includes the start time and the total time elapsed since the script began executing. * @return array An associative array containing performance statistics. - * - 'startTime': The timestamp when the script started executing. - * - 'totalTime': The total time elapsed since the script began executing. + * - 'startTime': The start time of the application. + * - 'totalTime': The total execution time of the application. */ public static function getPerformanceStats(): array { @@ -285,13 +229,10 @@ public static function getPerformanceStats(): array } /** - * Sets the application instance. + * Set the singleton instance of the application. * - * This method sets the application instance, ensuring that it can only be set once. - * If the application instance is already set, an exception is thrown. - * @param Application $app The instance of the application to set. - * @return void - * @throws Exception If the application instance is already set. + * @param Application $app The application instance to set. + * @throws Exception If the application instance has already been set. */ public static function setApplication(Application $app): void { @@ -306,7 +247,7 @@ public static function setApplication(Application $app): void * Get the installed version of the Axm Framework from composer.json. * @return string|null The version of the Axm Framework or null if not found. */ - public static function version() + public static function version(): ?string { if (isset($version)) return self::$version; diff --git a/src/BaseConfig.php b/src/BaseConfig.php index 6f3d09c..86ac44b 100644 --- a/src/BaseConfig.php +++ b/src/BaseConfig.php @@ -1,5 +1,7 @@ openFileConfig($file, $merge) + : $this->recursiveLoadFiles($file, $merge); + + return self::$instance; + } + + /** + * Opens and loads a configuration file. + * + * @param string $file Configuration file name. + * @param bool $merge Whether to merge the loaded configuration. + * @param string|null $pathConfig Optional path to the configuration directory. + * @return array Loaded configuration. + * @throws \RuntimeException When the file is not found or has an invalid format. + */ + private function openFileConfig(string $file, bool $merge = true, ?string $pathConfig = null): array { // Check if the file has already been previously uploaded - if (in_array($file, $this->loadedFiles)) { - return $this->config; - } + if (in_array($file, $this->loadedFiles)) return $this->config; - $file = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $file); + $filePath = $this->resolverDirPath($file, $pathConfig); - if (!file_exists($file)) { - throw new RuntimeException('Configuration file not found: ' . $file); - } + if (!is_file($filePath)) + throw new RuntimeException(sprintf('Configuration file not found: %s', $filePath)); - $ext = pathinfo($file, PATHINFO_EXTENSION); - switch ($ext) { - case 'php': - $data = require($file); - break; - case 'json': - $data = json_decode(file_get_contents($file), true); - break; - case 'ini': - $data = parse_ini_file($file, true); - break; - default: - throw new RuntimeException('Invalid configuration file format: ' . $ext); - } + $ext = pathinfo($filePath, PATHINFO_EXTENSION); + $data = $this->getData($filePath, $ext); - if (!is_array($data)) { - throw new RuntimeException('Invalid configuration file: ' . $file); - } + if (!is_array($data)) + throw new RuntimeException(sprintf('Invalid configuration file: %s', $filePath)); - if ($merge) { - $this->config = array_merge($this->config, $data); - } else { - $this->config = $data; - } + $this->config = ($merge) ? array_merge($this->config, $data) : $data; // Register the file as uploaded $this->loadedFiles[] = $file; - return (array) $this->config; + return $this->config; + } + + /** + * Resolves the full path of a configuration file. + * + * @param string $file Configuration file name. + * @param string|null $pathConfig Optional path to the configuration directory. + * @return string Full path of the configuration file. + */ + private function resolverDirPath(string $file, ?string $pathConfig = null) + { + $file = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, trim($file, '\/')); + $basePath = realpath($pathConfig ?? self::ROOT_PATH_CONFIG) . DIRECTORY_SEPARATOR . $file; + + return $basePath; + } + + /** + * Get parsed data from a configuration file. + * + * @param string $file The file path. + * @param string $ext The format ('php', 'json', 'ini'). + * @return mixed Parsed data. + * @throws RuntimeException If the format is invalid. + */ + private function getData(string $file, string $ext) + { + return match ($ext) { + 'php' => require $file, + 'json' => json_decode(file_get_contents($file), true), + 'ini' => parse_ini_file($file, true), + default => throw new RuntimeException(sprintf('Invalid format: %s', $ext)), + }; } /** @@ -99,11 +130,11 @@ public function load(string $file, bool $merge = true) * @return array * @throws RuntimeException */ - public function recursiveLoadFiles(array $files, bool $merge = true): array + private function recursiveLoadFiles(array $files, bool $merge = true): array { $config = []; foreach ($files as $file) { - $data = $this->load($file, $merge); + $data = $this->openFileConfig($file, $merge); $config = array_merge($config, $data); } @@ -111,14 +142,14 @@ public function recursiveLoadFiles(array $files, bool $merge = true): array } /** - * Get the view cache. + * Get the file cache. * - * @param string $view + * @param string $file * @return bool */ - protected function getCache(string $view) + protected function getCache(string $file) { - $cache = Cache::driver()->get($view); + $cache = Cache::driver()->get($file); return $cache; } @@ -142,16 +173,20 @@ protected function saveCache(string $file, $data) * @param mixed $default * @return mixed */ - public function get($key, $default = null) + public function get(string|null $key = null, $default = null) { + if (is_null($key)) return (array) $this->config; + $value = $this->config; foreach (explode('.', $key) as $segment) { if (!is_array($value) || !array_key_exists($segment, $value)) { $this->cache[$key] = $default; return $default; } + $value = $value[$segment]; } + $this->cache[$key] = $value; return $value; } @@ -169,7 +204,6 @@ public function has(string $name): bool /** * Get all configuration settings. - * * @return array */ public function all() @@ -179,7 +213,6 @@ public function all() /** * Set default configuration values. - * * @param array $defaults */ public function setDefaults(array $defaults) @@ -195,6 +228,7 @@ public function clearCache() $this->cache = []; } + /** * Magic method to retrieve configuration values. * @@ -203,6 +237,7 @@ public function clearCache() */ public function __get($key) { - return $this->get($key); + $config = ((object) $this->config[$key]) ?? null; + return $config; } } diff --git a/src/Controller.php b/src/Controller.php index 26b8781..8ec578b 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -1,5 +1,7 @@ * @link http://www.axm.com/ * @license http://www.axm.com/license/ @@ -22,40 +22,73 @@ */ abstract class Controller { + /** + * @var object|null The current user. + */ protected ?object $user = null; + + /** + * @var string|null The layout to be used. + */ protected ?string $layout = null; + + /** + * @var View|null The View instance. + */ protected ?View $view = null; + + /** + * @var string The current action. + */ protected string $action = ''; + + /** + * @var Request|null The Request instance. + */ protected ?Request $request = null; + + /** + * @var Response|null The Response instance. + */ protected ?Response $response = null; /** - * @var BaseMiddleware[] + * @var BaseMiddleware[]|null The array of middlewares. */ protected ?array $middlewares = []; - + /** + * Controller constructor. + */ public function __construct() { $app = Axm::app(); - $this->request = $app->request; + $this->request = $app->request; $this->response = $app->response; - $this->view = View::make(); + $this->view = View::make(); - $this->init(); + $this->executeMiddleware(); } /** - * + * Execute the registered middlewares. + * @return void */ - public function init() + private function executeMiddleware() { - $middleware = new MaintenanceMiddleware; - $this->registerMiddleware($middleware); + $middlewares = BaseMiddleware::$httpMiddlewares; + foreach ($middlewares as $key => $middleware) { + if ($middleware instanceof BaseMiddleware) { + $this->registerMiddleware(new $middleware); + } + } } /** - * Modifies the current layout + * Set the layout for the current controller. + * + * @param string $layout + * @return void */ public function setLayout(string $layout): void { @@ -63,7 +96,8 @@ public function setLayout(string $layout): void } /** - * Gets the current layout + * Get the current layout. + * @return string */ public function getLayout(): string { @@ -71,16 +105,19 @@ public function getLayout(): string } /** - * Specifies that the current view should extend an existing layout. + * Set the path for the current view. + * + * @param string $path + * @return void */ public function setPathView(string $path) { - return $this->view::$viewPath = $path; + $this->view::$viewPath = $path; } /** - * Adds an action to the controller - * + * Add an action to the controller. + * * @param string|null $action * @return void */ @@ -90,8 +127,7 @@ public function addAction(?string $action): void } /** - * Gets the current controller action. - * + * Get the current controller action. * @return string */ public function getAction(): string @@ -100,10 +136,13 @@ public function getAction(): string } /** - * Render the view - * + * Render the view. + * * @param string $view - * @param array $param + * @param array|null $params + * @param bool $buffer + * @param string $ext + * @return string|null */ public function renderView( string $view, @@ -111,13 +150,14 @@ public function renderView( bool $buffer = true, string $ext = '.php' ): ?string { - return $this->view::render($view, $params, $buffer, $ext); } /** - * Register a Middleware in the Controller + * Register a middleware in the controller. + * * @param BaseMiddleware $middleware + * @return void */ public function registerMiddleware(BaseMiddleware $middleware): void { @@ -125,6 +165,7 @@ public function registerMiddleware(BaseMiddleware $middleware): void } /** + * Get the registered middlewares. * @return Middlewares\BaseMiddleware[] */ public function getMiddlewares(): array @@ -133,13 +174,12 @@ public function getMiddlewares(): array } /** - * Registers and executes the AuthMiddleware middleware in the controller - * - * for access control to the specified actions - * @param array $actions Actions requiring authorization - * @param bool $allowedAction Indicates whether access to other - * actions than those specified is allowed. - **/ + * Register and execute the AuthMiddleware for access control to the specified actions. + * + * @param array $actions Actions requiring authorization. + * @param bool $allowedAction Indicates whether access to other actions than those specified is allowed. + * @return void + */ public function accessControl(array $actions, bool $allowedAction = false) { $middleware = new AuthMiddleware($actions, $allowedAction); @@ -147,15 +187,15 @@ public function accessControl(array $actions, bool $allowedAction = false) } /** - * Called when there is no method + * Handle calls to methods that do not exist. * - * @param string $name - * @param array $arguments + * @param string $name + * @param array $arguments * @throws RuntimeException * @return void */ public function __call($name, $arguments) { - throw new RuntimeException("Method [ $name ] does not exist"); + throw new RuntimeException(sprintf('Method [ %s ] does not exist', $name)); } } diff --git a/src/HandlerErrors.php b/src/HandlerErrors.php index db5077e..6883209 100644 --- a/src/HandlerErrors.php +++ b/src/HandlerErrors.php @@ -1,5 +1,7 @@ getCLIHandler(); - } + + if (Axm::is_cli()) return $this->getCLIHandler(); return $this->getHandler(); } @@ -103,6 +104,11 @@ public function exceptionHandler(\Throwable $e) error_log((string) $this->theme($e), 3, $this->getDirectory()); } + if (env('APP_ENVIRONMENT') == 'production') { + header(sprintf("Location: %s", $this->renderErrorView($e->getCode()))); + return; + } + CLIException::handleCLIException($e); exit(1); } @@ -147,6 +153,21 @@ public function getDirectory() return $dir; } + /** + * Renders the error view based on the specified HTTP error code. + * @return mixed The result of rendering the error view. + */ + private function renderErrorView($code) + { + $config = require(APP_PATH . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'Paths.php'); + $file = $config['paths']['viewsErrorsPath'] . DIRECTORY_SEPARATOR . $code . '.php'; + + if (!is_file($file)) throw new Exception(sprintf('The view %s file does not exist', $file)); + + // Calling the render() method on the controller instance + return $file; + } + /** * Custom error handler method. * @@ -159,12 +180,16 @@ public function getDirectory() public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null) { // Check if the error should be reported based on error reporting settings. - if (!(error_reporting() & $severity)) { + if (!(error_reporting() & $severity)) return; + + // Throw an ErrorException for the specified error. + $e = new ErrorException($message, 0, $severity, $file, $line); + if (env('APP_ENVIRONMENT') == 'production') { + header(sprintf("Location: %s", $this->renderErrorView($e->getCode()))); return; } - // Throw an ErrorException for the specified error. - throw new ErrorException($message, 0, $severity, $file, $line); + throw $e; } /** @@ -181,12 +206,22 @@ public function shutdownHandler() if ($error === null) return; ['type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error; - - if (in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) { + if ($this->isFatal($type)) { $this->exceptionHandler(new ErrorException($message, $type, 0, $file, $line)); } } + /** + * Determines if the type of error is fatal + * + * @param int $type + * @return bool + */ + protected function isFatal(int $type): bool + { + return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]); + } + /** * Builds the Whoops handler. * diff --git a/src/bootstrap.php b/src/bootstrap.php index c37c2c9..b17e4ca 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -6,9 +6,9 @@ * @license http://www.axm.com/license/ * @package Axm * ---------------------------------------------------------- - SETUP OUR PATH CONSTANTS ---------------------------------------------------------- */ +------------------------------------------------------------------------- + SETUP OUR PATH CONSTANTS +-------------------------------------------------------------------------*/ // Define the root path defined('AXM_BEGIN_TIME') or define('AXM_BEGIN_TIME', time()); @@ -19,7 +19,6 @@ defined('ROOT_PATH') or define('ROOT_PATH', getcwd()); // Define the public path -// const PUBLIC_PATH = '/'; const PUBLIC_PATH = ROOT_PATH . DIRECTORY_SEPARATOR . 'public'; // Defines the path of the dependencies @@ -37,14 +36,15 @@ // Defines the clean path of the request URI defined('CLEAN_URI_PATH') or define('CLEAN_URI_PATH', substr($_SERVER['SCRIPT_NAME'], 0, -9)); -// Defines the development environment -const ENV_PRODUCTION = 'production'; -const ENV_DEBUG = 'debug'; +/** +------------------------------------------------------------------------- + FILES FOR INITIALIZATION +------------------------------------------------------------------------- */ -const APP_NAMESPACE = 'App\\'; -const AXM_NAMESPACE = 'Axm\\'; +require_once('functions.php'); -require_once('axm_helper.php'); +$path = APP_PATH . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR; +config()->load(['App.php', 'Paths.php']); require_once(VENDOR_PATH . DIRECTORY_SEPARATOR . 'vlucas' . DIRECTORY_SEPARATOR . 'phpdotenv' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Dotenv.php'); diff --git a/src/axm_helper.php b/src/functions.php similarity index 98% rename from src/axm_helper.php rename to src/functions.php index 69965a6..18b7c1d 100644 --- a/src/axm_helper.php +++ b/src/functions.php @@ -1,5 +1,7 @@ config($key, $rootBaseConfig); + $config = Axm\BaseConfig::make(); + + if (is_null($key)) return $config; + + if (str_contains($key, '/')) { + $path = is_null($rootBaseConfig) ? $config::ROOT_PATH_CONFIG . $key : $rootBaseConfig; + return $config->load($path); + } + + return $config->get($key); } }