| | |
| | | |
| | | namespace Framework; |
| | | |
| | | use Framework\Exceptions\PageNotFoundException; |
| | | use ReflectionMethod; |
| | | use ReflectionClass; |
| | | |
| | | 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(string $path, $method) { |
| | | $params = $this->router->match($path, $method); |
| | | |
| | | if($params === false) { |
| | | exit("No routes matched"); |
| | | throw new PageNotFoundException("No route matched for '$path' with method '$method'"); |
| | | } |
| | | |
| | | $controller = "App\Controllers\\" . ucwords($params["controller"]); |
| | |
| | | |
| | | $controller = $this->getControllerName($params); |
| | | |
| | | $controller_object = $this->getObject($controller); |
| | | $controller_object = $this->container->get($controller); |
| | | |
| | | $args = $this->getActionArguments($controller, $action, $params); |
| | | |
| | |
| | | |
| | | return $namespace . "\\" . $controller; |
| | | |
| | | } |
| | | |
| | | private function getObject(string $class_name):object { |
| | | $reflector = new ReflectionClass($class_name); |
| | | $contructor = $reflector->getConstructor(); |
| | | |
| | | $dependencies = []; |
| | | |
| | | if($contructor === null) { |
| | | return new $class_name(); |
| | | } |
| | | |
| | | foreach($contructor->getParameters() as $param) { |
| | | $type = (string) $param->getType(); |
| | | $dependencies[] = $this->getObject($type); |
| | | } |
| | | |
| | | return new $class_name(...$dependencies); |
| | | } |
| | | } |