2021-02-21 16:37:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2022-06-07 19:04:15 +00:00
|
|
|
class Router {
|
2021-02-21 16:37:27 +00:00
|
|
|
private $routes;
|
|
|
|
private $errRoute;
|
|
|
|
|
|
|
|
public function addRoute($pattern, $function) {
|
|
|
|
$this->routes['{'.$pattern.'}'] = $function;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addErrorRoute($function) {
|
|
|
|
$this->errRoute = $function;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
foreach ($this->routes as $pattern => $function) {
|
|
|
|
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $params)) {
|
|
|
|
array_shift($params);
|
|
|
|
array_unshift($params, $_SERVER['REQUEST_URI']);
|
2022-06-07 19:04:15 +00:00
|
|
|
|
2021-02-21 16:37:27 +00:00
|
|
|
return call_user_func_array($function, array_values($params));
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 19:04:15 +00:00
|
|
|
|
2021-02-21 16:37:27 +00:00
|
|
|
return call_user_func($this->errRoute);
|
|
|
|
}
|
|
|
|
}
|