Start project PHP Back.
50% routes.
This commit is contained in:
93
src/Kernel.php
Normal file
93
src/Kernel.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
use App\Exceptions\ConfigFailedLoadingException;
|
||||
use App\Exceptions\InvalidRouteException;
|
||||
use App\Helpers\AutoLoader;
|
||||
use App\Helpers\ConfigFactory;
|
||||
use App\Http\Router;
|
||||
|
||||
/**
|
||||
* Classe primaire du site.
|
||||
* Permet de lancer l'application.
|
||||
*/
|
||||
final class Kernel {
|
||||
|
||||
/**
|
||||
* Liste des fichiers de configuration sous le format suivant :
|
||||
* [ 'nom_de_la_config' -> [ 'cle" → valeur ] ]
|
||||
*
|
||||
* @var array<string,array>
|
||||
*/
|
||||
public private(set) static array $configs = [];
|
||||
|
||||
/**
|
||||
* Instance actuelle de l'application.
|
||||
* @var Kernel|null
|
||||
*/
|
||||
private static ?self $instance = null;
|
||||
|
||||
/**
|
||||
* Méthode qui permet de démarrer le site.
|
||||
* @return self
|
||||
*/
|
||||
public static function start(): self {
|
||||
self::$instance = new self();
|
||||
self::$instance->init();
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet d'obtenir l'instance actuelle du site.
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance(): self {
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
*/
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de préparer le démarrage du site.
|
||||
* Lancé automatiquement par start().
|
||||
*
|
||||
* @return void
|
||||
* @see self::start()
|
||||
*/
|
||||
public function init(): void {
|
||||
$this->buildAutoloader();
|
||||
$this->loadConfig();
|
||||
|
||||
try {
|
||||
Router::routeTo();
|
||||
} catch ( InvalidRouteException $e ){
|
||||
die( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de mettre en place l'Autoloader.
|
||||
* @return void
|
||||
*/
|
||||
private function buildAutoloader(): void {
|
||||
require_once 'Helpers/AutoLoader.php';
|
||||
AutoLoader::register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de charger tous les fichiers de configuration du site.
|
||||
* @return void
|
||||
*/
|
||||
private function loadConfig(): void {
|
||||
try {
|
||||
self::$configs['general'] = ConfigFactory::loadConfigFile('general');
|
||||
} catch( ConfigFailedLoadingException $e ){
|
||||
die( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user