<?php
|
|
namespace Framework;
|
|
use ReflectionMethod;
|
|
class Dispatcher {
|
public function __construct(private Router $router) { }
|
|
public function handle(string $path) {
|
$params = $this->router->match($path);
|
|
if($params === false) {
|
exit("No routes matched");
|
}
|
|
$controller = "App\Controllers\\" . ucwords($params["controller"]);
|
$action = $params["action"];
|
|
$controller = $this->getControllerName($params);
|
|
$controller_object = new $controller;
|
|
$args = $this->getActionArguments($controller, $action, $params);
|
|
$controller_object->$action(...$args);
|
}
|
|
private function getActionArguments(string $controller, string $action, array $params = []): array {
|
$args = [];
|
$method = new ReflectionMethod($controller, $action);
|
foreach($method->getParameters() as $parameter) {
|
|
$name = $parameter->getName();
|
$args[$name] = $params[$name];
|
}
|
|
return($args);
|
}
|
|
private function getControllerName(array $params): string {
|
$controller = $params["controller"];
|
$controller = str_replace("-", "", ucwords(strtolower($controller), "-"));
|
$namespace = "App\Controllers";
|
|
if(array_key_exists("namespace", $params)) {
|
$namespace .= "\\" . $params["namespace"];
|
}
|
|
return $namespace . "\\" . $controller;
|
|
}
|
}
|