progetto fatto precedentemente adattato al framework creato con il corso
filippo.bertilotti
2024-06-12 5dd12c2939637d0af09bb385f12f24868bfc17c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
 
namespace Framework;
 
use Framework\Exceptions\PageNotFoundException;
use ReflectionMethod;
use UnexpectedValueException;
 
class Dispatcher {
    public function __construct(private Router $router,
                                private Container $container,
                                private array $middleware_classes) { }
 
    public function handle(Request $request): Response {
 
        $path = $this->getPath($request->uri);
        $params = $this->router->match($path, $request->method);
 
        if($params === false) {
            throw new PageNotFoundException("No route matched for '$path' with method '$request->method'");
        }
 
        $controller = "App\Controllers\\" . ucwords($params["controller"]);
        
        $action = $this->getActionName($params);
        $controller = $this->getControllerName($params);
 
        $controller_object = $this->container->get($controller);
 
        $controller_object->setViewer($this->container->get(TemplateViewerInterface::class));
        $controller_object->setResponse($this->container->get(Response::class));
 
        $args = $this->getActionArguments($controller, $action, $params);
        $controller_handler = new ControllerRequestHandler($controller_object, $action, $args);
 
        $middleware = $this->getMiddleware($params);
 
        $middleware_handler = new MiddlewareRequestHandler($middleware, 
                                                           $controller_handler);
 
        return $middleware_handler->handle($request);
    }
 
 
    private function getMiddleware(array $params): array {
        if( !array_key_exists("middleware", $params)) {
            return [];
        }
 
        $middleware = explode("|" ,$params["middleware"]);
 
        array_walk($middleware, function(&$value) {
            if(!array_key_exists($value, $this->middleware_classes)) {
                throw new UnexpectedValueException("Middleware '$value' not found in config settings");
            }
 
            $value = $this->container->get($this->middleware_classes[$value]);
        });
 
        return $middleware;
    }
 
    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;
 
    }
 
 
    private function getActionName(array $params): string
    {
        $action = $params["action"];
 
        $action = lcfirst(str_replace("-", "", ucwords(strtolower($action), "-")));
 
        return $action;
    }
 
    public function getPath(string $uri): string {
        $path = parse_url($uri, PHP_URL_PATH);
        if ($path === false) {
        throw new UnexpectedValueException("Malformed URL: {$uri}");
        }
        return $path;
    }
}