| | |
| | | |
| | | 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); |
| | | |
| | |
| | | $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; |
| | | } |
| | | } |