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
|
|
|
use Yiisoft\Yii\Web\RequestHandler\MiddlewareHandler; |
13
|
|
|
use Yiisoft\Yii\Web\RequestHandler\NotFoundHandler; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MiddlewareDispatcher |
17
|
|
|
*/ |
18
|
|
|
final class MiddlewareDispatcher implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var MiddlewareInterface[] |
22
|
|
|
*/ |
23
|
|
|
private array $middlewares = []; |
24
|
|
|
|
25
|
|
|
private RequestHandlerInterface $nextHandler; |
26
|
|
|
private ContainerInterface $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Contains a chain of middleware wrapped in handlers. |
30
|
|
|
* Each handler points to the handler of middleware that will be processed next. |
31
|
|
|
* @var RequestHandlerInterface|null stack of middleware |
32
|
|
|
*/ |
33
|
|
|
private ?RequestHandlerInterface $stack = null; |
34
|
3 |
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ContainerInterface $container, |
37
|
|
|
RequestHandlerInterface $nextHandler = null |
38
|
|
|
) { |
39
|
3 |
|
$this->container = $container; |
40
|
1 |
|
$this->nextHandler = $nextHandler ?? new NotFoundHandler($container->get(ResponseFactoryInterface::class)); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
/** |
44
|
|
|
* @param callable|MiddlewareInterface $middleware |
45
|
3 |
|
* @return self |
46
|
3 |
|
*/ |
47
|
|
|
public function addMiddleware($middleware): self |
48
|
|
|
{ |
49
|
3 |
|
if (is_callable($middleware)) { |
50
|
|
|
$middleware = new Callback($middleware, $this->container); |
|
|
|
|
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
if (!$middleware instanceof MiddlewareInterface) { |
54
|
|
|
throw new \InvalidArgumentException('Middleware should be either callable or MiddlewareInterface instance. ' . get_class($middleware) . ' given.'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->middlewares[] = $middleware; |
58
|
|
|
|
59
|
3 |
|
return $this; |
60
|
|
|
} |
61
|
3 |
|
|
62
|
|
|
public function dispatch(ServerRequestInterface $request): ResponseInterface |
63
|
3 |
|
{ |
64
|
3 |
|
return $this->process($request, $this->nextHandler); |
65
|
|
|
} |
66
|
1 |
|
|
67
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
68
|
|
|
{ |
69
|
|
|
if ($this->stack === null) { |
70
|
1 |
|
foreach ($this->middlewares as $middleware) { |
71
|
|
|
$handler = new MiddlewareHandler($middleware, $handler); |
72
|
1 |
|
} |
73
|
|
|
$this->stack = $handler; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $this->stack->handle($request); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|