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