|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Router; |
|
6
|
|
|
|
|
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 RuntimeException; |
|
12
|
|
|
use Yiisoft\Http\Method; |
|
13
|
|
|
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; |
|
14
|
|
|
|
|
15
|
|
|
final class MatchingResult implements MiddlewareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string[] |
|
19
|
|
|
* @psalm-var array<string,string> |
|
20
|
|
|
*/ |
|
21
|
|
|
private array $arguments = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private array $methods = []; |
|
27
|
|
|
|
|
28
|
|
|
private ?MiddlewareDispatcher $dispatcher = null; |
|
29
|
|
|
|
|
30
|
16 |
|
private function __construct(private ?Route $route) |
|
31
|
|
|
{ |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
6 |
|
public function withDispatcher(MiddlewareDispatcher $dispatcher): self |
|
35
|
|
|
{ |
|
36
|
6 |
|
$new = clone $this; |
|
37
|
6 |
|
$new->dispatcher = $dispatcher; |
|
38
|
6 |
|
return $new; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string[] $arguments |
|
43
|
|
|
* @psalm-param array<string,string> $arguments |
|
44
|
|
|
*/ |
|
45
|
8 |
|
public static function fromSuccess(Route $route, array $arguments): self |
|
46
|
|
|
{ |
|
47
|
8 |
|
$new = new self($route); |
|
48
|
8 |
|
$new->arguments = $arguments; |
|
49
|
8 |
|
return $new; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string[] $methods |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public static function fromFailure(array $methods): self |
|
56
|
|
|
{ |
|
57
|
8 |
|
$new = new self(null); |
|
58
|
8 |
|
$new->methods = $methods; |
|
59
|
8 |
|
return $new; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @psalm-assert-if-true !null $this->route |
|
64
|
|
|
*/ |
|
65
|
12 |
|
public function isSuccess(): bool |
|
66
|
|
|
{ |
|
67
|
12 |
|
return $this->route !== null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
12 |
|
public function isMethodFailure(): bool |
|
71
|
|
|
{ |
|
72
|
12 |
|
return $this->route === null && $this->methods !== Method::ALL; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string[] |
|
77
|
|
|
* @psalm-return array<string,string> |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function arguments(): array |
|
80
|
|
|
{ |
|
81
|
7 |
|
return $this->arguments; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function methods(): array |
|
88
|
|
|
{ |
|
89
|
4 |
|
return $this->methods; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
7 |
|
public function route(): Route |
|
93
|
|
|
{ |
|
94
|
7 |
|
if ($this->route === null) { |
|
95
|
1 |
|
throw new RuntimeException('There is no route in the matching result.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
return $this->route; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
8 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
102
|
|
|
{ |
|
103
|
8 |
|
if (!$this->isSuccess()) { |
|
104
|
1 |
|
return $handler->handle($request); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Inject dispatcher only if we have not previously injected. |
|
108
|
|
|
// This improves performance in event-loop applications. |
|
109
|
7 |
|
if ($this->dispatcher !== null && !$this->route->getData('hasDispatcher')) { |
|
110
|
6 |
|
$this->route->injectDispatcher($this->dispatcher); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
7 |
|
return $this->route |
|
114
|
7 |
|
->getData('dispatcherWithMiddlewares') |
|
115
|
7 |
|
->dispatch($request, $handler); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|