|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Middleware\Dispatcher; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
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\Middleware\Dispatcher\Event\AfterMiddleware; |
|
13
|
|
|
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware; |
|
14
|
|
|
|
|
15
|
|
|
final class MiddlewareStack implements MiddlewareStackInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Contains a stack of middleware wrapped in handlers. |
|
19
|
|
|
* Each handler points to the handler of middleware that will be processed next. |
|
20
|
|
|
* |
|
21
|
|
|
* @var RequestHandlerInterface|null stack of middleware |
|
22
|
|
|
*/ |
|
23
|
|
|
private ?RequestHandlerInterface $stack = null; |
|
24
|
|
|
|
|
25
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
26
|
|
|
|
|
27
|
7 |
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
|
28
|
|
|
{ |
|
29
|
7 |
|
$this->eventDispatcher = $eventDispatcher; |
|
30
|
7 |
|
} |
|
31
|
|
|
|
|
32
|
7 |
|
public function build(array $middlewares, RequestHandlerInterface $fallbackHandler): MiddlewareStackInterface |
|
33
|
|
|
{ |
|
34
|
7 |
|
$handler = $fallbackHandler; |
|
35
|
7 |
|
foreach ($middlewares as $middleware) { |
|
36
|
7 |
|
$handler = $this->wrap($middleware, $handler); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
$new = clone $this; |
|
40
|
7 |
|
$new->stack = $handler; |
|
41
|
|
|
|
|
42
|
7 |
|
return $new; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
7 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
46
|
|
|
{ |
|
47
|
7 |
|
if ($this->isEmpty()) { |
|
48
|
|
|
throw new \RuntimeException('Stack is empty.'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
7 |
|
return $this->stack->handle($request); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
7 |
|
public function reset(): void |
|
55
|
|
|
{ |
|
56
|
7 |
|
$this->stack = null; |
|
57
|
7 |
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
public function isEmpty(): bool |
|
60
|
|
|
{ |
|
61
|
7 |
|
return $this->stack === null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Wraps handler by middlewares |
|
66
|
|
|
*/ |
|
67
|
7 |
|
private function wrap(MiddlewareInterface $middleware, RequestHandlerInterface $handler): RequestHandlerInterface |
|
68
|
|
|
{ |
|
69
|
7 |
|
return new class($middleware, $handler, $this->eventDispatcher) implements RequestHandlerInterface { |
|
70
|
|
|
private MiddlewareInterface $middleware; |
|
71
|
|
|
private RequestHandlerInterface $handler; |
|
72
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
73
|
|
|
|
|
74
|
|
|
public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $handler, EventDispatcherInterface $eventDispatcher) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$this->middleware = $middleware; |
|
77
|
7 |
|
$this->handler = $handler; |
|
78
|
7 |
|
$this->eventDispatcher = $eventDispatcher; |
|
79
|
7 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
82
|
|
|
{ |
|
83
|
7 |
|
$this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request)); |
|
84
|
|
|
|
|
85
|
7 |
|
$response = null; |
|
|
|
|
|
|
86
|
|
|
try { |
|
87
|
7 |
|
return $response = $this->middleware->process($request, $this->handler); |
|
88
|
|
|
} finally { |
|
89
|
7 |
|
$this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
}; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|