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