1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Resolver\InstanceCreator; |
8
|
|
|
|
9
|
|
|
use function is_object; |
10
|
|
|
|
11
|
|
|
final class Route |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param object|class-string $controller |
|
|
|
|
15
|
|
|
*/ |
16
|
72 |
|
public function __construct( |
17
|
|
|
private string $method, |
18
|
|
|
private string $path, |
19
|
|
|
private object|string $controller, |
20
|
|
|
private string $action = '__invoke', |
21
|
|
|
) { |
22
|
72 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @psalm-suppress MixedMethodCall |
26
|
|
|
*/ |
27
|
65 |
|
public function run(RoutingConfigurator $routingConfigurator): string |
28
|
|
|
{ |
29
|
65 |
|
$params = (new RouteParams($this))->asArray(); |
30
|
|
|
|
31
|
65 |
|
if (is_object($this->controller)) { |
32
|
15 |
|
return (string)$this->controller->{$this->action}(...$params); |
33
|
|
|
} |
34
|
|
|
|
35
|
50 |
|
$creator = new InstanceCreator($routingConfigurator->getMappingInterfaces()); |
36
|
50 |
|
$controller = $creator->createByClassName($this->controller); |
37
|
|
|
|
38
|
50 |
|
return (string)$controller->{$this->action}(...$params); |
39
|
|
|
} |
40
|
|
|
|
41
|
65 |
|
public function path(): string |
42
|
|
|
{ |
43
|
65 |
|
return $this->path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function method(): string |
47
|
|
|
{ |
48
|
|
|
return $this->method; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return object|class-string |
|
|
|
|
53
|
|
|
*/ |
54
|
65 |
|
public function controller(): object|string |
55
|
|
|
{ |
56
|
65 |
|
return $this->controller; |
57
|
|
|
} |
58
|
|
|
|
59
|
65 |
|
public function action(): string |
60
|
|
|
{ |
61
|
65 |
|
return $this->action; |
62
|
|
|
} |
63
|
|
|
|
64
|
66 |
|
public function getPathPattern(): string |
65
|
|
|
{ |
66
|
66 |
|
$pattern = preg_replace('#({.*})#U', '(.*)', $this->path); |
67
|
|
|
|
68
|
66 |
|
return '#^/' . $pattern . '$#'; |
69
|
|
|
} |
70
|
|
|
|
71
|
72 |
|
public function requestMatches(): bool |
72
|
|
|
{ |
73
|
72 |
|
if (!$this->methodMatches()) { |
74
|
24 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
66 |
|
if (!$this->pathMatches()) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
65 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
72 |
|
public function methodMatches(): bool |
85
|
|
|
{ |
86
|
72 |
|
return Request::instance()->isMethod($this->method); |
87
|
|
|
} |
88
|
|
|
|
89
|
66 |
|
private function pathMatches(): bool |
90
|
|
|
{ |
91
|
66 |
|
$path = Request::instance()->path(); |
92
|
|
|
|
93
|
66 |
|
return preg_match($this->getPathPattern(), $path) |
94
|
66 |
|
|| preg_match($this->getPathPatternWithoutOptionals(), $path); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
private function getPathPatternWithoutOptionals(): string |
98
|
|
|
{ |
99
|
3 |
|
$pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path); |
100
|
|
|
|
101
|
3 |
|
return '#^/' . $pattern . '$#'; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|