-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRoute.php
More file actions
executable file
·181 lines (156 loc) · 3.08 KB
/
Route.php
File metadata and controls
executable file
·181 lines (156 loc) · 3.08 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;
class Route extends Hook
{
/**
* HTTP Method
*
* @var string
*/
protected string $method = '';
/**
* Whether to use hook
*
* @var bool
*/
protected bool $hook = true;
/**
* Path
*
* @var string
*/
protected string $path = '';
/**
* Path params.
*
* @var array<string,array<string, string>>
*/
protected array $pathParams = [];
/**
* Internal counter.
*
* @var int
*/
protected static int $counter = 0;
/**
* Route order ID.
*
* @var int
*/
protected int $order;
protected string $matchedPath = '';
public function __construct(string $method, string $path)
{
$this->path($path);
$this->method = $method;
$this->order = ++self::$counter;
$this->action = function (): void {
};
}
public function setMatchedPath(string $path): self
{
$this->matchedPath = $path;
return $this;
}
public function getMatchedPath(): string
{
return $this->matchedPath;
}
/**
* Get Route Order ID
*
* @return int
*/
public function getOrder(): int
{
return $this->order;
}
/**
* Add path
*
* @param string $path
* @return self
*/
public function path(string $path): self
{
$this->path = $path;
return $this;
}
/**
* Add alias
*
* @param string $path
* @return self
*/
public function alias(string $path): self
{
Router::addRouteAlias($path, $this);
return $this;
}
/**
* When set false, hooks for this route will be skipped.
*
* @param bool $hook
* @return self
*/
public function hook(bool $hook = true): self
{
$this->hook = $hook;
return $this;
}
/**
* Get HTTP Method
*
* @return string
*/
public function getMethod(): string
{
return $this->method;
}
/**
* Get path
*
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* Get hook status
*
* @return bool
*/
public function getHook(): bool
{
return $this->hook;
}
/**
* Set path param.
*
* @param string $key
* @param int $index
* @return void
*/
public function setPathParam(string $path, string $key, int $index): void
{
$this->pathParams[$path][$key] = $index;
}
/**
* Get path params.
*
* @param \Utopia\Request $request
* @return array
*/
public function getPathValues(Request $request, string $path): array
{
$pathValues = [];
$parts = explode('/', ltrim($request->getURI(), '/'));
foreach (($this->pathParams[$path] ?? []) as $key => $index) {
if (array_key_exists($index, $parts)) {
$pathValues[$key] = $parts[$index];
}
}
return $pathValues;
}
}