1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Yii\Web\Middleware\Callback; |
13
|
|
|
use Yiisoft\Yii\Web\RequestHandler\MiddlewareHandler; |
14
|
|
|
use Yiisoft\Yii\Web\RequestHandler\NotFoundHandler; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* MiddlewareDispatcher |
18
|
|
|
*/ |
19
|
|
|
final class MiddlewareDispatcher implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MiddlewareInterface[] |
23
|
|
|
*/ |
24
|
|
|
private array $middlewares = []; |
25
|
|
|
|
26
|
|
|
private RequestHandlerInterface $nextHandler; |
27
|
|
|
private ContainerInterface $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Contains a chain of middleware wrapped in handlers. |
31
|
|
|
* Each handler points to the handler of middleware that will be processed next. |
32
|
|
|
* @var RequestHandlerInterface|null stack of middleware |
33
|
|
|
*/ |
34
|
3 |
|
private ?RequestHandlerInterface $stack = null; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
ContainerInterface $container, |
38
|
|
|
RequestHandlerInterface $nextHandler = null |
39
|
3 |
|
) { |
40
|
1 |
|
$this->container = $container; |
41
|
|
|
$this->nextHandler = $nextHandler ?? new NotFoundHandler($container->get(ResponseFactoryInterface::class)); |
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
|
|
|
$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
|
|
|
$dispatcher = $this->container->get(EventDispatcherInterface::class); |
72
|
1 |
|
|
73
|
|
|
foreach ($this->middlewares as $middleware) { |
74
|
|
|
$handler = new MiddlewareHandler($middleware, $handler, $dispatcher); |
75
|
|
|
} |
76
|
1 |
|
$this->stack = $handler; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->stack->handle($request); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|