-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoute.php
272 lines (239 loc) · 5.65 KB
/
Route.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace Darya\Routing;
/**
* Representation of a route in Darya's routing system.
*
* @property string $namespace Matched namespace
* @property string $controller Matched controller
* @property string|callable $action Matched action (callable or controller method)
*
* @author Chris Andrew <chris@hexus.io>
*/
class Route
{
/**
* Reserved route parameter keys.
*
* @var array
*/
protected $reserved = array('namespace', 'controller', 'action');
/**
* URI path that matches the route - e.g. "/:controller/:action/:params"
*
* @var string
*/
protected $path;
/**
* Default path parameters.
*
* @var array
*/
protected $defaults = array();
/**
* Matched path parameters.
*
* @var array
*/
protected $matches = array();
/**
* Matched path parameters prepared as controller arguments.
*
* @var array
*/
protected $parameters = array();
/**
* The router that matched this route.
*
* @var \Darya\Routing\Router
*/
public $router;
/**
* Prepare the given matches.
*
* Removes all non-numeric properties of the given matches.
*
* @param array $matches
* @return array
*/
public static function prepareMatches($matches)
{
$prepared = array();
foreach ($matches as $key => $value) {
if (!is_numeric($key)) {
$prepared[$key] = $value;
}
}
return $prepared;
}
/**
* Prepare the given matches as parameters
*
* Splits the matched "params" property by forward slashes and appends these
* to the parent array.
*
* @param array $matches Set of matches to prepare
* @return array Set of route parameters to pass to a matched action
*/
public static function prepareParameters($matches)
{
$parameters = array();
foreach ($matches as $key => $value) {
if (!is_numeric($key)) {
switch ($key) {
case 'params':
$pathParameters = explode('/', $value);
foreach ($pathParameters as $pathParameter) {
$parameters[] = $pathParameter;
}
break;
default:
$parameters[$key] = $value;
}
}
}
return $parameters;
}
/**
* Instantiate a new route.
*
* @param string $path Path that matches the route
* @param mixed $defaults Default route parameters
*/
public function __construct($path, $defaults = array())
{
$this->path = $path;
$this->defaults($defaults);
}
/**
* Magic method that determines whether an existing route parameter is set.
*
* @return bool
*/
public function __isset($property)
{
return isset($this->parameters[$property]) || isset($this->defaults[$property]);
}
/**
* Setter magic method that sets route parameters if the attempted property
* does not exist.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$this->parameters[$property] = $value;
}
/**
* Getter magic method for retrieving route parameters.
*
* Tries defaults if the parameter is not set.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
if (isset($this->parameters[$property])) {
return $this->parameters[$property];
}
if (isset($this->defaults[$property])) {
return $this->defaults[$property];
}
}
/**
* Get the currently set parameters, excluding those with reserved keys,
* for use as action arguments.
*
* @return array
*/
public function arguments()
{
return array_diff_key($this->parameters(), array_flip($this->reserved));
}
/**
* Set default route parameters using the given array.
*
* If a callable is given it becomes the route's default action.
*
* If a string or object is given it becomes the route's default controller.
*
* @param mixed $parameters
* @return array The route's default parameters
*/
public function defaults($parameters = array())
{
if (is_array($parameters)) {
$this->defaults = array_merge($this->defaults, $parameters);
}
if (is_callable($parameters)) {
$this->defaults['action'] = $parameters;
}
if (is_string($parameters) || is_object($parameters)) {
$this->defaults['controller'] = $parameters;
}
return $this->defaults;
}
/**
* Determine whether the route has been matched by a router.
*
* @return bool
*/
public function matched()
{
return !!$this->matches;
}
/**
* Set route matches and parameters using the given matches array.
*
* This completely replaces any existing matches and parameters.
*
* Returns the currently set matches.
*
* @param array $matches
* @return array
*/
public function matches(array $matches = array())
{
$this->matches = static::prepareMatches($matches);
$this->parameters = static::prepareParameters($matches);
return $this->matches;
}
/**
* Merge route parameters using the given array.
*
* Returns the currently set parameters merged into defaults.
*
* @param array $parameters [optional]
* @return array Route parameters
*/
public function parameters(array $parameters = array())
{
$this->parameters = array_merge($this->parameters, $parameters);
return array_merge($this->defaults, $this->parameters);
}
/**
* Retrieve the path that matches the route.
*
* @return string
*/
public function path()
{
return $this->path;
}
/**
* Retrieve the URL that the route was matched by.
*
* Optionally accepts parameters to merge into matched parameters.
*
* @param array $parameters [optional]
* @return string
*/
public function url(array $parameters = array())
{
if ($this->router) {
$parameters = array_merge($this->matches, $parameters);
return $this->router->url($this->path, $parameters);
}
}
}