Properly implement the router with config and interfaces

Expand the container with aliases and argument autowiring
This commit is contained in:
Tim
2023-08-07 17:50:34 +02:00
parent e9a636554f
commit a55d1c3c2e
11 changed files with 406 additions and 48 deletions

View File

@ -3,27 +3,42 @@
namespace Ardent\Undercurrent\Kernel;
use App\Controller\BaseController;
use Ardent\Undercurrent\Container\ContainerInterface;
use Ardent\Undercurrent\Container\GenericContainer;
use Ardent\Undercurrent\Http\GenericRequest;
use Ardent\Undercurrent\Http\GenericRouter;
use Ardent\Undercurrent\Http\ResponseInterface;
use Ardent\Undercurrent\Http\MethodEnum;
use Ardent\Undercurrent\Http\RouterConfig;
use Ardent\Undercurrent\Http\RouterInterface;
class BaseKernel
{
public function __invoke(): void
{
$container = new GenericContainer();
$container->add(GenericRouter::class);
$container->add(BaseController::class);
$container = (new GenericContainer());
$container
->alias(RouterInterface::class, GenericRouter::class)
->alias(ContainerInterface::class, GenericContainer::class)
->add(GenericContainer::class, fn($container) => $container)
->add(GenericRouter::class)
->add(BaseController::class);
$container->add(RouterConfig::class, fn() => new RouterConfig([
BaseController::class,
]));
$this->render($container);
}
private function render(GenericContainer $container): void
{
$router = $container->get(GenericRouter::class);
$route = $router->getRoute($_SERVER['REQUEST_URI']);
$controller = $container->get($route['controller']);
$method = $route['method'];
echo $controller->$method()->getBody();
$request = new GenericRequest(
MethodEnum::from($_SERVER['REQUEST_METHOD']),
$_SERVER['REQUEST_URI'],
$_REQUEST,
);
$router = $container->get(RouterInterface::class);
echo $router->dispatch($request)->getBody();
}
}