1
<?php declare(strict_types=1);
5
use Upside\Di\Container;
6
use Upside\Di\ServiceProviderInterface;
7
use Upside\Http\Request;
8
use Upside\Http\Response;
9
use Upside\Router\Matched;
10
use Upside\Router\Route;
11
use Upside\Router\RouteCollectorInterface;
12
use Upside\Router\RouterInterface;
13
use Upside\Router\RoutesProviderInterface;
14
use Upside\Ufm\Controller\BaseController;
15
use Upside\Ufm\Controller\ErrorController;
16
use Upside\Ufm\Provider\Configs;
17
use Upside\Ufm\Provider\Services;
18
use Upside\View\ViewInterface;
22
private Container $container;
24
public function __construct(ServiceProviderInterface ...$service_providers)
27
->init_container(...$service_providers)
31
private function init_container(ServiceProviderInterface ...$service_providers): self
33
$this->container = new Container();
35
$this->container->register(new Configs());
36
$this->container->register(new Services());
38
foreach ($service_providers as $provider) {
39
$this->container->register($provider);
45
private function init_routes(): self
47
$provider = $this->container->get(RoutesProviderInterface::class);
48
$collector = $this->container->get(RouteCollectorInterface::class);
50
$provider->register($collector);
55
public function handle(Request $request): Response
57
$router = $this->container->get(RouterInterface::class);
59
$match = $router->match($request->get_path());
60
$matched = $match->unwrap_or_else(fn() => new Matched(new Route($request->get_path(), ErrorController::class, 'error_404', 'ANY', 'error.404')));
61
$route = $matched->route;
63
$controller = $this->container->get($route->controller);
64
$action = $route->action;
66
if ($controller instanceof BaseController) {
67
$controller->init($this->container->get(ViewInterface::class));
70
return $controller->$action($matched->params === [] ? $request : $request->with_params($matched->params));