|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
10
|
|
|
use Yiisoft\Http\Method; |
|
11
|
|
|
|
|
12
|
|
|
final class MatchingResult implements MiddlewareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private bool $success; |
|
15
|
|
|
private Route $route; |
|
16
|
|
|
private array $parameters = []; |
|
17
|
|
|
private array $methods = []; |
|
18
|
|
|
private ?ContainerInterface $container = null; |
|
19
|
|
|
|
|
20
|
8 |
|
private function __construct() |
|
21
|
|
|
{ |
|
22
|
8 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function withContainer(ContainerInterface $container): self |
|
25
|
|
|
{ |
|
26
|
1 |
|
$new = clone $this; |
|
27
|
1 |
|
$new->container = $container; |
|
28
|
1 |
|
return $new; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public static function fromSuccess(Route $route, array $parameters): self |
|
32
|
|
|
{ |
|
33
|
3 |
|
$new = new self(); |
|
34
|
3 |
|
$new->success = true; |
|
35
|
3 |
|
$new->route = $route; |
|
36
|
3 |
|
$new->parameters = $parameters; |
|
37
|
3 |
|
return $new; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public static function fromFailure(array $methods): self |
|
41
|
|
|
{ |
|
42
|
5 |
|
$new = new self(); |
|
43
|
5 |
|
$new->methods = $methods; |
|
44
|
5 |
|
$new->success = false; |
|
45
|
5 |
|
return $new; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
public function isSuccess(): bool |
|
49
|
|
|
{ |
|
50
|
5 |
|
return $this->success; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
public function isMethodFailure(): bool |
|
54
|
|
|
{ |
|
55
|
5 |
|
return !$this->success && $this->methods !== Method::ANY; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function parameters(): array |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->parameters; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function methods(): array |
|
64
|
|
|
{ |
|
65
|
2 |
|
return $this->methods; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
69
|
|
|
{ |
|
70
|
3 |
|
if ($this->success === false) { |
|
71
|
1 |
|
return $handler->handle($request); |
|
72
|
|
|
} |
|
73
|
2 |
|
$route = $this->route; |
|
74
|
2 |
|
if ($this->container !== null && !$route->hasContainer()) { |
|
75
|
1 |
|
$route = $route->withContainer($this->container); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $route->process($request, $handler); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|