|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Spiral\Boot\AbstractKernel; |
|
12
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
13
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
14
|
|
|
use Spiral\Core\Attribute\Proxy; |
|
15
|
|
|
use Spiral\Core\BinderInterface; |
|
16
|
|
|
use Spiral\Core\Core; |
|
17
|
|
|
use Spiral\Core\CoreInterface; |
|
18
|
|
|
use Spiral\Core\Exception\ScopeException; |
|
19
|
|
|
use Spiral\Framework\Kernel; |
|
20
|
|
|
use Spiral\Framework\Spiral; |
|
21
|
|
|
use Spiral\Http\Config\HttpConfig; |
|
22
|
|
|
use Spiral\Interceptors\HandlerInterface; |
|
23
|
|
|
use Spiral\Router\GroupRegistry; |
|
24
|
|
|
use Spiral\Router\Loader\Configurator\RoutingConfigurator; |
|
25
|
|
|
use Spiral\Router\Loader\DelegatingLoader; |
|
26
|
|
|
use Spiral\Router\Loader\LoaderInterface; |
|
27
|
|
|
use Spiral\Router\Loader\LoaderRegistry; |
|
28
|
|
|
use Spiral\Router\Loader\LoaderRegistryInterface; |
|
29
|
|
|
use Spiral\Router\Loader\PhpFileLoader; |
|
30
|
|
|
use Spiral\Router\Registry\DefaultPatternRegistry; |
|
31
|
|
|
use Spiral\Router\Registry\RoutePatternRegistryInterface; |
|
32
|
|
|
use Spiral\Router\RouteInterface; |
|
33
|
|
|
use Spiral\Router\Router; |
|
34
|
|
|
use Spiral\Router\RouterInterface; |
|
35
|
|
|
use Spiral\Router\UriHandler; |
|
36
|
|
|
use Spiral\Telemetry\Bootloader\TelemetryBootloader; |
|
37
|
|
|
use Spiral\Telemetry\TracerInterface; |
|
38
|
|
|
|
|
39
|
|
|
final class RouterBootloader extends Bootloader |
|
40
|
|
|
{ |
|
41
|
373 |
|
public function __construct( |
|
42
|
|
|
private readonly ConfiguratorInterface $config, |
|
43
|
|
|
private readonly BinderInterface $binder, |
|
44
|
|
|
) { |
|
45
|
373 |
|
} |
|
46
|
|
|
|
|
47
|
373 |
|
public function defineDependencies(): array |
|
48
|
|
|
{ |
|
49
|
373 |
|
return [ |
|
50
|
373 |
|
HttpBootloader::class, |
|
51
|
373 |
|
TelemetryBootloader::class, |
|
52
|
373 |
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
373 |
|
public function defineSingletons(): array |
|
56
|
|
|
{ |
|
57
|
373 |
|
$this->binder |
|
58
|
373 |
|
->getBinder(Spiral::HttpRequest) |
|
|
|
|
|
|
59
|
373 |
|
->bindSingleton(RouteInterface::class, [self::class, 'route']); |
|
60
|
|
|
|
|
61
|
373 |
|
return [ |
|
62
|
373 |
|
HandlerInterface::class => [self::class, 'handler'], |
|
63
|
373 |
|
CoreInterface::class => Core::class, |
|
64
|
373 |
|
RouterInterface::class => [self::class, 'router'], |
|
65
|
373 |
|
RequestHandlerInterface::class => RouterInterface::class, |
|
66
|
373 |
|
LoaderInterface::class => DelegatingLoader::class, |
|
67
|
373 |
|
LoaderRegistryInterface::class => [self::class, 'initRegistry'], |
|
68
|
373 |
|
GroupRegistry::class => GroupRegistry::class, |
|
69
|
373 |
|
RoutingConfigurator::class => RoutingConfigurator::class, |
|
70
|
373 |
|
RoutePatternRegistryInterface::class => DefaultPatternRegistry::class, |
|
71
|
373 |
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
373 |
|
public function boot(AbstractKernel $kernel): void |
|
75
|
|
|
{ |
|
76
|
373 |
|
$configuratorCallback = static function (RouterInterface $router, RoutingConfigurator $routes): void { |
|
77
|
373 |
|
$router->import($routes); |
|
78
|
373 |
|
}; |
|
79
|
373 |
|
$groupsCallback = static function (RouterInterface $router, GroupRegistry $groups): void { |
|
80
|
373 |
|
$groups->registerRoutes($router); |
|
81
|
373 |
|
}; |
|
82
|
|
|
|
|
83
|
373 |
|
if ($kernel instanceof Kernel) { |
|
84
|
356 |
|
$kernel->appBooted($configuratorCallback); |
|
85
|
356 |
|
$kernel->appBooted($groupsCallback); |
|
86
|
|
|
} else { |
|
87
|
17 |
|
$kernel->booted($configuratorCallback); |
|
88
|
17 |
|
$kernel->booted($groupsCallback); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @noRector RemoveUnusedPrivateMethodRector |
|
94
|
|
|
*/ |
|
95
|
373 |
|
private function router( |
|
96
|
|
|
UriHandler $uriHandler, |
|
97
|
|
|
#[Proxy] ContainerInterface $container, |
|
98
|
|
|
TracerInterface $tracer, |
|
99
|
|
|
?EventDispatcherInterface $dispatcher = null |
|
100
|
|
|
): RouterInterface { |
|
101
|
373 |
|
return new Router( |
|
102
|
373 |
|
$this->config->getConfig(HttpConfig::CONFIG)['basePath'], |
|
103
|
373 |
|
$uriHandler, |
|
104
|
373 |
|
$container, |
|
105
|
373 |
|
$dispatcher, |
|
106
|
373 |
|
$tracer, |
|
107
|
373 |
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
14 |
|
private function handler(?CoreInterface $core, ContainerInterface $container): HandlerInterface |
|
111
|
|
|
{ |
|
112
|
14 |
|
return $core instanceof HandlerInterface |
|
113
|
14 |
|
? $core |
|
114
|
14 |
|
: $container->get(Core::class); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @noRector RemoveUnusedPrivateMethodRector |
|
119
|
|
|
*/ |
|
120
|
3 |
|
private function route(ServerRequestInterface $request): RouteInterface |
|
121
|
|
|
{ |
|
122
|
3 |
|
$route = $request->getAttribute(Router::ROUTE_ATTRIBUTE, null); |
|
123
|
3 |
|
if ($route === null) { |
|
124
|
1 |
|
throw new ScopeException('Unable to resolve Route, invalid request scope'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
return $route; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @noRector RemoveUnusedPrivateMethodRector |
|
132
|
|
|
*/ |
|
133
|
373 |
|
private function initRegistry(ContainerInterface $container): LoaderRegistryInterface |
|
134
|
|
|
{ |
|
135
|
373 |
|
return new LoaderRegistry([ |
|
136
|
373 |
|
$container->get(PhpFileLoader::class), |
|
137
|
373 |
|
]); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|