Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Mar 29, 2024
1 parent 5a8e38c commit 43c5785
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 351 deletions.
56 changes: 34 additions & 22 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,48 @@ final class App extends Container
*/
public function __construct()
{
$this->setApp();
$this->handlerErrors();
$this->loadEnv();
$this->configureEnvironment();
$this->internalFunctions();
$this->registerComponents();
$this->includeAutoloadVendor();
$this->bootServices();
}

/**
* Singleton App
*/
public static function getInstance(): self
private function setApp(): void
{
if (self::$instance === null) {
self::$instance = new self();
}
Axm::setApplication($this);
}

/**
* Include the functions file from the AXM directory.
*/
private function internalFunctions()
{
require (AXM_PATH . DIRECTORY_SEPARATOR . 'functions.php');
}

return self::$instance;
/**
* Include the error handler file from the AXM directory.
* This is a private function that serves to include the necessary error handler.
*/
private function handlerErrors()
{
require (AXM_PATH . DIRECTORY_SEPARATOR . 'HandlerErrors.php');
}

/**
* Include the Composer autoload file from the vendor directory.
* This is a private function that serves to include the Composer autoload used in the project.
*/
private function includeAutoloadVendor()
{
require VENDOR_PATH . DIRECTORY_SEPARATOR . 'autoload.php';
}

/**
Expand All @@ -52,8 +78,8 @@ private function configureEnvironment(): void
{
static $initialized = false;
if (!$initialized) {
$environment = env('APP_ENVIRONMENT', 'production');

$environment = Env::get('APP_ENVIRONMENT', 'production');
if ($environment === 'debug') {
ini_set('display_errors', 1);
error_reporting(E_ALL);
Expand Down Expand Up @@ -243,8 +269,7 @@ public function hasCsrfToken(string $token): bool
*/
public function version(string $libraryName = 'axm/framework'): ?string
{
$v = \Composer\InstalledVersions::getVersion($libraryName);
return $v;
return Axm::version($libraryName);
}

/**
Expand All @@ -271,22 +296,9 @@ public function removeService(string $alias): void
*/
public function __get(string $name): mixed
{
if ($name == 'config') {
return config($name);
}

return $this->get($name);
}

/**
* Returns a new instance of the ConsoleApplication class
*/
public function createConsoleApplication()
{
static $console;
return $console ??= new \Console\ConsoleApplication;
}

/**
* Run the application.
**/
Expand Down
74 changes: 74 additions & 0 deletions src/Axm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Axm Framework PHP.
*
* The Axm class serves as the entry point for the AXM Framework. It provides methods for
* initializing the application.
*
* @author Juan Cristobal <[email protected]>
* @link http://www.axm.com/
* @license http://www.axm.com/license/
* @package Framework
*/
abstract class Axm
{
private static ?App $_app = null;

/**
* Initializes the application.
*/
public static function makeApplication()
{
return new App;
}

/**
* Sets the application instance.
*
* 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.
*/
public static function setApplication(App $app): void
{
if (self::$_app !== null) {
throw new Exception('Axm application can only be created once.');
}

self::$_app = $app;
}

/**
* Gets instance of the application.
*/
public static function getApp(): ?App
{
return self::$_app;
}

/**
* Returns a new instance of the ConsoleApplication class
*/
public static function makeConsoleApplication()
{
static $console;
return $console ??= new \Console\ConsoleApplication;
}

/**
* Check if the application is in production mode.
*/
public static function isProduction(): bool
{
return env('APP_ENVIRONMENT') === 'production';
}

/**
* Get the version of a specified library.
*/
public static function version(string $libraryName = 'axm/framework'): ?string
{
$v = \Composer\InstalledVersions::getVersion($libraryName);
return $v;
}
}
132 changes: 0 additions & 132 deletions src/Router(luego revisar).php

This file was deleted.

1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function axm_autoloader(string $class)

$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',
Expand Down
5 changes: 0 additions & 5 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,3 @@
FILES FOR INITIALIZATION
-------------------------------------------------------------------------------- */
require (AXM_PATH . DIRECTORY_SEPARATOR . 'autoload.php');
require (AXM_PATH . DIRECTORY_SEPARATOR . 'HandlerErrors.php');
require (AXM_PATH . DIRECTORY_SEPARATOR . 'functions.php');

// Add Composer autoload to load external dependencies
require VENDOR_PATH . DIRECTORY_SEPARATOR . 'autoload.php';
11 changes: 8 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function config(string $key = null, mixed $value = null)
*/
function app(?string $alias = null, $value = null): object
{
static $instance;
$instance ??= new App;
$instance = Axm::getApp();

if (null === $alias) {
return $instance;
Expand Down Expand Up @@ -280,7 +279,13 @@ function generateUrl(string $dir = ''): string
**/
function baseUrl(string $path = '/'): string
{
return app()->router->url($path);
$scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$scriptPath = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
$baseUrl = "{$scheme}://{$_SERVER['HTTP_HOST']}{$scriptPath}";
$path = trim(rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/' . ltrim($path, '/'), '/');

return "{$baseUrl}/{$path}";

}
}

Expand Down
Loading

0 comments on commit 43c5785

Please sign in to comment.