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 |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var MiddlewareInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $middlewares = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var RequestHandlerInterface|null |
25
|
|
|
*/ |
26
|
|
|
private $nextHandler; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ContainerInterface |
30
|
|
|
*/ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
3 |
|
* Contains a chain of middleware wrapped in handlers. |
35
|
|
|
* Each handler points to the handler of middleware that will be processed next. |
36
|
|
|
* @var RequestHandlerInterface|null stack of middleware |
37
|
|
|
*/ |
38
|
|
|
private ?RequestHandlerInterface $stack = null; |
39
|
3 |
|
|
40
|
1 |
|
public function __construct( |
41
|
|
|
array $middlewares, |
42
|
|
|
ContainerInterface $container, |
43
|
3 |
|
RequestHandlerInterface $nextHandler = null |
44
|
|
|
) { |
45
|
3 |
|
if ($middlewares === []) { |
46
|
3 |
|
throw new \InvalidArgumentException('Middlewares should be defined.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
$this->container = $container; |
50
|
|
|
|
51
|
3 |
|
for ($i = count($middlewares) - 1; $i >= 0; $i--) { |
52
|
|
|
$this->addMiddleware($middlewares[$i]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$responseFactory = $container->get(ResponseFactoryInterface::class); |
56
|
|
|
|
57
|
|
|
$this->nextHandler = $nextHandler ?? new NotFoundHandler($responseFactory); |
58
|
|
|
} |
59
|
3 |
|
|
60
|
|
|
private function addCallable(callable $callback): void |
61
|
3 |
|
{ |
62
|
|
|
array_unshift($this->middlewares, new Callback($callback, $this->container)); |
63
|
3 |
|
} |
64
|
3 |
|
|
65
|
|
|
public function addMiddleware($middleware): self |
66
|
1 |
|
{ |
67
|
|
|
if (is_callable($middleware)) { |
68
|
|
|
$this->addCallable($middleware); |
69
|
|
|
} elseif ($middleware instanceof MiddlewareInterface) { |
70
|
1 |
|
array_unshift($this->middlewares, $middleware); |
71
|
|
|
} else { |
72
|
1 |
|
throw new \InvalidArgumentException('Middleware should be either callable or MiddlewareInterface instance. ' . get_class($middleware) . ' given.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
public function dispatch(ServerRequestInterface $request): ResponseInterface |
79
|
|
|
{ |
80
|
|
|
return $this->process($request, $this->nextHandler); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
84
|
|
|
{ |
85
|
|
|
if ($this->stack === null) { |
86
|
|
|
for ($i = count($this->middlewares) - 1; $i >= 0; $i--) { |
87
|
|
|
$handler = $this->wrap($this->middlewares[$i], $handler); |
88
|
|
|
} |
89
|
|
|
$this->stack = $handler; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->stack->handle($request); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Wraps handler by middlewares |
97
|
|
|
*/ |
98
|
|
|
private function wrap(MiddlewareInterface $middleware, RequestHandlerInterface $handler): RequestHandlerInterface |
99
|
|
|
{ |
100
|
|
|
return new class($middleware, $handler) implements RequestHandlerInterface { |
101
|
|
|
private MiddlewareInterface $middleware; |
102
|
|
|
private RequestHandlerInterface $handler; |
103
|
|
|
|
104
|
|
|
public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $handler) |
105
|
|
|
{ |
106
|
|
|
$this->middleware = $middleware; |
107
|
|
|
$this->handler = $handler; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
111
|
|
|
{ |
112
|
|
|
return $this->middleware->process($request, $this->handler); |
113
|
|
|
} |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|