commit | author | age
|
bb3f24
|
1 |
<?php |
F |
2 |
|
|
3 |
namespace Framework; |
|
4 |
|
9aeb12
|
5 |
use ReflectionMethod; |
F |
6 |
|
bb3f24
|
7 |
class Dispatcher { |
F |
8 |
public function __construct(private Router $router) { } |
f6df29
|
9 |
|
F |
10 |
public function handle(string $path) { |
|
11 |
$params = $this->router->match($path); |
|
12 |
|
|
13 |
if($params === false) { |
|
14 |
exit("No routes matched"); |
|
15 |
} |
|
16 |
|
|
17 |
$controller = "App\Controllers\\" . ucwords($params["controller"]); |
|
18 |
$action = $params["action"]; |
|
19 |
|
|
20 |
$controller_object = new $controller; |
|
21 |
|
1e5093
|
22 |
$args = $this->getActionArguments($controller, $action, $params); |
9aeb12
|
23 |
|
1e5093
|
24 |
$controller_object->$action(...$args); |
9aeb12
|
25 |
} |
F |
26 |
|
1e5093
|
27 |
private function getActionArguments(string $controller, string $action, array $params = []): array { |
F |
28 |
$args = []; |
9aeb12
|
29 |
$method = new ReflectionMethod($controller, $action); |
F |
30 |
foreach($method->getParameters() as $parameter) { |
1e5093
|
31 |
|
9aeb12
|
32 |
$name = $parameter->getName(); |
1e5093
|
33 |
$args[$name] = $params[$name]; |
9aeb12
|
34 |
} |
1e5093
|
35 |
|
F |
36 |
return($args); |
f6df29
|
37 |
} |
bb3f24
|
38 |
} |