-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRoute.php
More file actions
executable file
·181 lines (151 loc) · 3.59 KB
/
Copy pathRoute.php
File metadata and controls
executable file
·181 lines (151 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Utopia\Http;
use Utopia\Servers\Hook;
class Route extends Hook
{
/**
* HTTP Method
*/
protected string $method = '';
/**
* Whether to use hook
*/
protected bool $hook = true;
/**
* Path
*/
protected string $path = '';
/**
* Path params.
*
* @var array<string, array<string, int>>
*/
protected array $pathParams = [];
/**
* Alias paths this route is also registered under.
*
* @var array<string>
*/
protected array $aliasPaths = [];
/**
* Additional HTTP methods this route is also registered under.
*
* @var array<string>
*/
protected array $additionalMethods = [];
/**
* Internal counter.
*/
protected static int $counter = 0;
/**
* Route order ID.
*/
protected int $order;
/**
* @param array<int, string> $additionalMethods
*/
public function __construct(string $method, string $path, array $additionalMethods = [])
{
parent::__construct();
$this->path($path);
$this->method = $method;
$this->additionalMethods = $additionalMethods;
$this->order = ++self::$counter;
}
/**
* Get Route Order ID
*/
public function getOrder(): int
{
return $this->order;
}
/**
* Add path
*/
public function path(string $path): self
{
$this->path = $path;
return $this;
}
/**
* Add alias
*/
public function alias(string $path): self
{
Router::validateRouteAlias($path, [$this->method, ...$this->additionalMethods]);
Router::addRouteAlias($path, $this);
foreach ($this->additionalMethods as $method) {
Router::addRouteAlias($path, $this, $method);
}
if (!\in_array($path, $this->aliasPaths, true)) {
$this->aliasPaths[] = $path;
}
return $this;
}
/**
* When set false, hooks for this route will be skipped.
*/
public function hook(bool $hook = true): self
{
$this->hook = $hook;
return $this;
}
/**
* Get HTTP Method
*/
public function getMethod(): string
{
return $this->method;
}
/**
* Get path
*/
public function getPath(): string
{
return $this->path;
}
/**
* Get hook status
*/
public function getHook(): bool
{
return $this->hook;
}
/**
* Get additional methods this route is registered under.
*
* @return array<string>
*/
public function getAdditionalMethods(): array
{
return $this->additionalMethods;
}
/**
* Set path param.
*/
public function setPathParam(string $key, int $index, string $path = ''): void
{
$this->pathParams[$path][$key] = $index;
}
/**
* Extract this route's path params from a request URL.
*
* @return array<string, string>
*/
public function resolveParams(string $url, string $matchedTemplate = ''): array
{
$pathValues = [];
$parts = explode('/', ltrim($url, '/'));
if (empty($matchedTemplate)) {
$pathParams = $this->pathParams[$matchedTemplate] ?? array_values($this->pathParams)[0] ?? [];
} else {
$pathParams = $this->pathParams[$matchedTemplate] ?? [];
}
foreach ($pathParams as $key => $index) {
if (\array_key_exists($index, $parts)) {
$pathValues[$key] = $parts[$index];
}
}
return $pathValues;
}
}