| | |
| | | |
| | | use Framework\Exceptions\PageNotFoundException; |
| | | use ReflectionMethod; |
| | | use UnexpectedValueException; |
| | | |
| | | class Dispatcher { |
| | | public function __construct(private Router $router, |
| | | private Container $container) { } |
| | | |
| | | public function handle(Request $request) { |
| | | $params = $this->router->match($path, $method); |
| | | |
| | | $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 '$method'"); |
| | | throw new PageNotFoundException("No route matched for '$path' with method '$request->method'"); |
| | | } |
| | | |
| | | $controller = "App\Controllers\\" . ucwords($params["controller"]); |
| | |
| | | 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; |
| | | } |
| | | } |