10namespace ComusParty\App;
12use ComusParty\App\Exceptions\RouteNotFoundException;
13use ComusParty\App\Exceptions\UnauthorizedAccessException;
14use ComusParty\Controllers\ControllerFactory;
49 if (is_null(self::$instance)) {
50 self::$instance =
new Router();
52 return self::$instance;
62 public function get(
string $url, callable $target, ?
string $middleware =
null): void
64 $this->
addRoute(
'GET', $url, $target, $middleware);
75 private function addRoute(
string $method,
string $url, callable $target, ?
string $middleware =
null): void
77 if (is_null($middleware)) {
81 $this->routes[$method][$middleware][
BASE_URI . $url] = $target;
91 public function post(
string $url, callable $target, ?
string $middleware =
null): void
93 $this->
addRoute(
'POST', $url, $target, $middleware);
103 public function put(
string $url, callable $target, ?
string $middleware =
null): void
105 $this->
addRoute(
'PUT', $url, $target, $middleware);
115 public function delete(
string $url, callable $target, ?
string $middleware =
null): void
117 $this->
addRoute(
'DELETE', $url, $target, $middleware);
141 $method = $_SERVER[
'REQUEST_METHOD'];
142 $url = $_SERVER[
'REQUEST_URI'];
144 if (is_null($_SESSION[
'uuid'] ??
null)) {
148 $authenticated =
true;
151 if (isset($this->routes[$method])) {
152 if (!$authenticated) {
157 header(
'Location: /login?redirect=' . urlencode($url));
160 $role = $_SESSION[
'role'];
166 foreach ($this->routes[$method] as $target) {
167 foreach (array_keys($target) as $routeUrl) {
168 $pattern = preg_replace(
'/\/:([^\/]+)/',
'/(?P<$1>[^/]+)', $routeUrl);
169 if (preg_match(
'#^' . $pattern .
'(\?[^/]*=[^/]+)?$#', $url)) {
188 if (isset($this->routes[$method][$middleware])) {
189 foreach ($this->routes[$method][$middleware] as $routeUrl => $target) {
205 $pattern = preg_replace(
'/\/:([^\/]+)/',
'/(?P<$1>[^/]+)', $routeUrl);
206 if (preg_match(
'#^' . $pattern .
'(\?[^/]*=[^/]+)?$#', $url, $matches)) {
207 $params = array_filter($matches,
'is_string', ARRAY_FILTER_USE_KEY);
208 call_user_func_array($target, $params);
221 throw new Exception(
"Cannot unserialize a singleton.");
Classe RouteNotFoundException.
Classe UnauthorizedAccessException.
Classe Router permettant de gérer les routes.
__construct()
Constructeur de la classe Router.
__wakeup()
Empêche la désérialisation de l'instance.
addRoute(string $method, string $url, callable $target, ?string $middleware=null)
Permet d'ajouter une route au tableau de routes du Router.
callFunctionFromRoute(string $routeUrl, callable $target, string $url)
Permet d'appeler la fonction associée à la route.
post(string $url, callable $target, ?string $middleware=null)
Ajout d'une route POST.
matchRoute()
Permet d'accéder à la route demandée.
put(string $url, callable $target, ?string $middleware=null)
Ajout d'une route PUT.
checkRouteAndCall(string $middleware, string $method, string $url)
Permet de vérifier si une route existe.
static getInstance()
Permet de récupérer l'instance du Router.
__clone()
Empêche le clonage de l'instance.
static getController(string $controller, FilesystemLoader $loader, Environment $twig)
La méthode getController permet de récupérer un contrôleur.
const BASE_URI
URI de base de l'application.
$loader
Instance de FilesystemLoader.