corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-28 0b87390cf225ee1053f1d5986900784d39110568
src/Framework/Dispatcher.php
@@ -2,25 +2,32 @@
namespace Framework;
use Framework\Exceptions\PageNotFoundException;
use ReflectionMethod;
use UnexpectedValueException;
class Dispatcher {
    public function __construct(private Router $router) { }
    public function __construct(private Router $router,
                                private Container $container) { }
    public function handle(string $path) {
        $params = $this->router->match($path);
    public function handle(Request $request) {
        $path = $this->getPath($request->uri);
        $params = $this->router->match($path, $request->method);
        if($params === false) {
            exit("No routes matched");
            throw new PageNotFoundException("No route matched for '$path' with method '$request->method'");
        }
        $controller = "App\Controllers\\" . ucwords($params["controller"]);
        $action = $params["action"];
        $controller = $this->getControllerName($params);
        exit($controller);
        $controller_object = new $controller;
        $controller_object = $this->container->get($controller);
        $controller_object->setRequest($request);
        $controller_object->setViewer($this->container->get(PHPTemplateViewer::class));
        $args = $this->getActionArguments($controller, $action, $params);
@@ -43,7 +50,20 @@
        $controller = $params["controller"];
        $controller = str_replace("-", "", ucwords(strtolower($controller), "-"));
        $namespace = "App\Controllers";
        if(array_key_exists("namespace", $params)) {
            $namespace .= "\\" . $params["namespace"];
        }
        return $namespace . "\\" . $controller;
    }
    public function getPath(string $uri): string {
        $path = parse_url($uri, PHP_URL_PATH);
        if ($path === false) {
        throw new UnexpectedValueException("Malformed URL: {$uri}");
        }
        return $path;
    }
}