1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use Yiisoft\Yii\Web\Middleware\Callback; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* MiddlewareDispatcher |
15
|
|
|
*/ |
16
|
|
|
final class MiddlewareDispatcher implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var MiddlewareInterface[] |
20
|
|
|
*/ |
21
|
|
|
private array $middlewares = []; |
22
|
|
|
|
23
|
|
|
private RequestHandlerInterface $nextHandler; |
24
|
|
|
private ContainerInterface $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Contains a chain of middleware wrapped in handlers. |
28
|
|
|
* Each handler points to the handler of middleware that will be processed next. |
29
|
|
|
* @var RequestHandlerInterface|null stack of middleware |
30
|
|
|
*/ |
31
|
|
|
private ?RequestHandlerInterface $stack = null; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
3 |
|
ContainerInterface $container, |
35
|
|
|
RequestHandlerInterface $nextHandler = null |
36
|
|
|
) { |
37
|
|
|
$this->container = $container; |
38
|
|
|
|
39
|
3 |
|
$responseFactory = $container->get(ResponseFactoryInterface::class); |
40
|
1 |
|
|
41
|
|
|
$this->nextHandler = $nextHandler ?? new NotFoundHandler($responseFactory); |
42
|
|
|
} |
43
|
3 |
|
|
44
|
|
|
/** |
45
|
3 |
|
* @param callable|MiddlewareInterface $middleware |
46
|
3 |
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function addMiddleware($middleware): self |
49
|
3 |
|
{ |
50
|
|
|
if (is_callable($middleware)) { |
51
|
3 |
|
$middleware = new Callback($middleware, $this->container); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$middleware instanceof MiddlewareInterface) { |
55
|
|
|
throw new \InvalidArgumentException('Middleware should be either callable or MiddlewareInterface instance. ' . get_class($middleware) . ' given.'); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
array_unshift($this->middlewares, $middleware); |
59
|
3 |
|
|
60
|
|
|
return $this; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
3 |
|
public function dispatch(ServerRequestInterface $request): ResponseInterface |
64
|
3 |
|
{ |
65
|
|
|
return $this->process($request, $this->nextHandler); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
69
|
|
|
{ |
70
|
1 |
|
if ($this->stack === null) { |
71
|
|
|
for ($i = count($this->middlewares) - 1; $i >= 0; $i--) { |
72
|
1 |
|
$handler = $this->wrap($this->middlewares[$i], $handler); |
73
|
|
|
} |
74
|
|
|
$this->stack = $handler; |
75
|
|
|
} |
76
|
1 |
|
|
77
|
|
|
return $this->stack->handle($request); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Wraps handler by middlewares |
82
|
|
|
*/ |
83
|
|
|
private function wrap(MiddlewareInterface $middleware, RequestHandlerInterface $handler): RequestHandlerInterface |
84
|
|
|
{ |
85
|
|
|
return new class($middleware, $handler) implements RequestHandlerInterface { |
86
|
|
|
private MiddlewareInterface $middleware; |
87
|
|
|
private RequestHandlerInterface $handler; |
88
|
|
|
|
89
|
|
|
public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $handler) |
90
|
1 |
|
{ |
91
|
|
|
$this->middleware = $middleware; |
92
|
1 |
|
$this->handler = $handler; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
96
|
|
|
{ |
97
|
|
|
return $this->middleware->process($request, $this->handler); |
98
|
|
|
} |
99
|
|
|
}; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|