-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRoute.php
More file actions
executable file
·143 lines (119 loc) · 2.55 KB
/
Route.php
File metadata and controls
executable file
·143 lines (119 loc) · 2.55 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
<?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 = [];
/**
* Internal counter.
*/
protected static int $counter = 0;
/**
* Route order ID.
*/
protected int $order;
public function __construct(string $method, string $path)
{
parent::__construct();
$this->path($path);
$this->method = $method;
$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::addRouteAlias($path, $this);
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;
}
/**
* Set path param.
*/
public function setPathParam(string $key, int $index, string $path = ''): void
{
$this->pathParams[$path][$key] = $index;
}
/**
* Get path params.
*
* @return array<string, mixed>
*/
public function getPathValues(Request $request, string $path = ''): array
{
$pathValues = [];
$parts = explode('/', ltrim($request->getURI(), '/'));
if (empty($path)) {
$pathParams = $this->pathParams[$path] ?? array_values($this->pathParams)[0] ?? [];
} else {
$pathParams = $this->pathParams[$path] ?? [];
}
foreach ($pathParams as $key => $index) {
if (\array_key_exists($index, $parts)) {
$pathValues[$key] = $parts[$index];
}
}
return $pathValues;
}
}