ufm

Форк
0
/
App.php 
72 строки · 2.1 Кб
1
<?php declare(strict_types=1);
2

3
namespace Upside\Ufm;
4

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;
19

20
class App
21
{
22
    private Container $container;
23

24
    public function __construct(ServiceProviderInterface ...$service_providers)
25
    {
26
        $this
27
            ->init_container(...$service_providers)
28
            ->init_routes();
29
    }
30

31
    private function init_container(ServiceProviderInterface ...$service_providers): self
32
    {
33
        $this->container = new Container();
34

35
        $this->container->register(new Configs());
36
        $this->container->register(new Services());
37

38
        foreach ($service_providers as $provider) {
39
            $this->container->register($provider);
40
        }
41

42
        return $this;
43
    }
44

45
    private function init_routes(): self
46
    {
47
        $provider = $this->container->get(RoutesProviderInterface::class);
48
        $collector = $this->container->get(RouteCollectorInterface::class);
49

50
        $provider->register($collector);
51

52
        return $this;
53
    }
54

55
    public function handle(Request $request): Response
56
    {
57
        $router = $this->container->get(RouterInterface::class);
58

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;
62

63
        $controller = $this->container->get($route->controller);
64
        $action = $route->action;
65

66
        if ($controller instanceof BaseController) {
67
            $controller->init($this->container->get(ViewInterface::class));
68
        }
69

70
        return $controller->$action($matched->params === [] ? $request : $request->with_params($matched->params));
71
    }
72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.