|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
9
|
|
|
use Spiral\Core\BinderInterface; |
|
10
|
|
|
use Spiral\Http\Pipeline; |
|
11
|
|
|
use Spiral\Router\GroupRegistry; |
|
12
|
|
|
use Spiral\Router\Loader\Configurator\RoutingConfigurator; |
|
13
|
|
|
use Spiral\Router\PipelineFactory; |
|
14
|
|
|
|
|
15
|
|
|
abstract class RoutesBootloader extends Bootloader |
|
16
|
|
|
{ |
|
17
|
266 |
|
public function init(HttpBootloader $http): void |
|
18
|
|
|
{ |
|
19
|
266 |
|
foreach ($this->globalMiddleware() as $middleware) { |
|
20
|
266 |
|
$http->addMiddleware($middleware); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
266 |
|
public function boot(RoutingConfigurator $routes, BinderInterface $binder, GroupRegistry $groups): void |
|
25
|
|
|
{ |
|
26
|
266 |
|
$middlewareGroups = $this->middlewareGroups(); |
|
27
|
|
|
|
|
28
|
266 |
|
$this->registerMiddlewareGroups($binder, $middlewareGroups); |
|
29
|
266 |
|
$this->registerMiddlewareForRouteGroups($groups, \array_keys($middlewareGroups)); |
|
30
|
|
|
|
|
31
|
266 |
|
$this->configureRouteGroups($groups); |
|
32
|
|
|
$this->defineRoutes($routes); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Override this method to configure application routes |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function defineRoutes(RoutingConfigurator $routes): void |
|
39
|
|
|
{ |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Override this method to configure route groups |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function configureRouteGroups(GroupRegistry $groups): void |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array<MiddlewareInterface|class-string<MiddlewareInterface>> |
|
|
|
|
|
|
51
|
266 |
|
*/ |
|
52
|
|
|
abstract protected function globalMiddleware(): array; |
|
53
|
266 |
|
|
|
54
|
266 |
|
/** |
|
55
|
|
|
* @return array<string,array<MiddlewareInterface|class-string<MiddlewareInterface>>> |
|
|
|
|
|
|
56
|
266 |
|
*/ |
|
57
|
266 |
|
abstract protected function middlewareGroups(): array; |
|
58
|
|
|
|
|
59
|
|
|
private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($groups as $group => $middleware) { |
|
62
|
|
|
$binder->bind( |
|
63
|
266 |
|
'middleware:' . $group, |
|
64
|
|
|
static function (PipelineFactory $factory) use ($middleware): Pipeline { |
|
65
|
266 |
|
return $factory->createWithMiddleware($middleware); |
|
66
|
266 |
|
} |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void |
|
72
|
|
|
{ |
|
73
|
|
|
foreach ($groups as $group) { |
|
74
|
|
|
$registry->getGroup($group)->addMiddleware('middleware:' . $group); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.