Fix Router and finish base view system.
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http;
|
||||
use App\Domain\Controller;
|
||||
use App\Exceptions\InvalidRouteException;
|
||||
use App\Helpers\AutoLoader;
|
||||
use App\Kernel;
|
||||
use FilesystemIterator;
|
||||
|
||||
final class Router {
|
||||
@@ -21,6 +22,12 @@ final class Router {
|
||||
*/
|
||||
public private(set) static Route $clientRoute;
|
||||
|
||||
/**
|
||||
* Informations sur les arguments passés à la route.
|
||||
* @var array
|
||||
*/
|
||||
public private(set) static array $clientRouteArguments;
|
||||
|
||||
/**
|
||||
* Liste des controllers sorti du cache ou récupéré via fetchControllers.
|
||||
* @var array
|
||||
@@ -34,7 +41,7 @@ final class Router {
|
||||
private static array $routes;
|
||||
|
||||
/**
|
||||
* Fonction principale qui va router le contenu vers la bonne méthode du bon Controlleur.
|
||||
* Fonction principale qui va router le contenu vers la bonne méthode du bon Controller.
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidRouteException Si la route voulue n'existe pas ou n'est pas bien formaté.
|
||||
@@ -121,17 +128,14 @@ final class Router {
|
||||
*/
|
||||
private static function clientRouteExist(): Route|bool {
|
||||
|
||||
$clientRouteName = trim( self::$clientRouteString, '/' );
|
||||
foreach( self::$routes as $route ){
|
||||
/*
|
||||
if( preg_match( self::getRegexRoute( $route), self::$clientRouteString, $matches ) ){
|
||||
var_dump( $matches );
|
||||
}
|
||||
*/
|
||||
/*
|
||||
if( $route->routeUrl === self::$clientRouteString ){
|
||||
$routeName = self::getRegexRoute( $route );
|
||||
if( preg_match( $routeName, $clientRouteName, $matches ) ){
|
||||
array_shift( $matches );
|
||||
self::$clientRouteArguments = $matches;
|
||||
return $route;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -139,20 +143,22 @@ final class Router {
|
||||
}
|
||||
|
||||
private static function getRegexRoute( Route $route ): string {
|
||||
$routeUrl = $route->routeUrl;
|
||||
$routeUrl = str_replace( "{int}", "([0-9]+)", $routeUrl );
|
||||
return $routeUrl;
|
||||
$routeUrl = trim( $route->routeUrl, '/' );
|
||||
foreach ( Kernel::$configs['route_arguments'] as $key => $value ){
|
||||
$routeUrl = str_replace( $key, $value, $routeUrl );
|
||||
}
|
||||
return "#^{$routeUrl}$#";
|
||||
}
|
||||
|
||||
/**
|
||||
* Va permettre d'exécuter la méthode du Controller->action() en parsant les arguments si existants.
|
||||
* Va permettre d'exécuter la méthode du Controller→action() en parsant les arguments si existants.
|
||||
* @return void
|
||||
*/
|
||||
private static function executeRouteAction(): void {
|
||||
$controller = self::$clientRoute->routeController;
|
||||
$method = self::$clientRoute->routeAction;
|
||||
|
||||
new $controller()->$method();
|
||||
new $controller()->$method( ...self::$clientRouteArguments);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user