|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Route; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
trait RouteConditionHandlerTrait |
|
11
|
|
|
{ |
|
12
|
|
|
protected ?string $host = null; |
|
13
|
|
|
protected ?string $name = null; |
|
14
|
|
|
protected ?int $port = null; |
|
15
|
|
|
protected ?string $scheme = null; |
|
16
|
|
|
|
|
17
|
57 |
|
public function getHost(): ?string |
|
18
|
|
|
{ |
|
19
|
57 |
|
return $this->host; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
57 |
|
public function getName(): ?string |
|
23
|
|
|
{ |
|
24
|
57 |
|
return $this->name; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
54 |
|
public function getPort(): ?int |
|
28
|
|
|
{ |
|
29
|
54 |
|
return $this->port; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
60 |
|
public function getScheme(): ?string |
|
33
|
|
|
{ |
|
34
|
60 |
|
return $this->scheme; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
15 |
|
public function setHost(string $host): RouteConditionHandlerInterface |
|
38
|
|
|
{ |
|
39
|
15 |
|
$this->host = $host; |
|
40
|
15 |
|
return $this->checkAndReturnSelf(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
public function setName(string $name): RouteConditionHandlerInterface |
|
44
|
|
|
{ |
|
45
|
9 |
|
$this->name = $name; |
|
46
|
9 |
|
return $this->checkAndReturnSelf(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
public function setPort(int $port): RouteConditionHandlerInterface |
|
50
|
|
|
{ |
|
51
|
9 |
|
$this->port = $port; |
|
52
|
9 |
|
return $this->checkAndReturnSelf(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
public function setScheme(string $scheme): RouteConditionHandlerInterface |
|
56
|
|
|
{ |
|
57
|
9 |
|
$this->scheme = $scheme; |
|
58
|
9 |
|
return $this->checkAndReturnSelf(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
27 |
|
private function checkAndReturnSelf(): RouteConditionHandlerInterface |
|
62
|
|
|
{ |
|
63
|
27 |
|
if ($this instanceof RouteConditionHandlerInterface) { |
|
64
|
24 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
throw new RuntimeException(sprintf( |
|
68
|
3 |
|
'Trait (%s) must be consumed by an instance of (%s)', |
|
69
|
3 |
|
__TRAIT__, |
|
70
|
3 |
|
RouteConditionHandlerInterface::class |
|
71
|
3 |
|
)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
48 |
|
protected function isExtraConditionMatch(Route $route, ServerRequestInterface $request): bool |
|
75
|
|
|
{ |
|
76
|
|
|
// check for scheme condition |
|
77
|
48 |
|
$scheme = $route->getScheme(); |
|
78
|
48 |
|
if ($scheme !== null && $scheme !== $request->getUri()->getScheme()) { |
|
79
|
3 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// check for domain condition |
|
83
|
45 |
|
$host = $route->getHost(); |
|
84
|
45 |
|
if ($host !== null && $host !== $request->getUri()->getHost()) { |
|
85
|
6 |
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// check for port condition |
|
89
|
42 |
|
$port = $route->getPort(); |
|
90
|
42 |
|
return !($port !== null && $port !== $request->getUri()->getPort()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|