-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.php
191 lines (169 loc) · 6.82 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
<?php
/**
* The MIT License
*
* Copyright (c) 2010 - 2013 Tony R Quilkey <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @namespace Proem\Routing
*/
namespace Proem\Routing;
use Proem\Routing\RouteAbstract;
use Proem\Http\Request;
/**
* Proem's standard route.
*/
class Route extends RouteAbstract
{
/**
* Store default tokens.
*
* @var array
*/
protected $defaultTokens = [];
/**
* Store default filters.
*
* @var array
*/
protected $defaultFilters = [];
/**
* Instantiate this route
*
* $options = ['rule', 'targets', 'filters', 'method', 'callback'];
*
* @param array $options
*/
public function __construct(array $options)
{
parent::__construct($options);
$this->defaultFilters = [
':default' => '[a-zA-Z0-9_\+\-%]+',
':gobble' => '[a-zA-Z0-9_\+\-%\/]+',
':int' => '[0-9]+',
':alpha' => '[a-zA-Z]+',
':slug' => '[a-zA-Z0-9_-]+'
];
$this->defaultTokens = [
'module' => $this->defaultFilters[':default'],
'controller' => $this->defaultFilters[':default'],
'action' => $this->defaultFilters[':default'],
'params' => $this->defaultFilters[':gobble']
];
}
/**
* Process the supplied url.
*
* This route takes a simplified series of patterns such as :controller and
* replaces them with more complex regular expressions which are then used
* within a preg_match_callback to match against the given uri.
*
* If a 'filter' regex is set within the $options array that regex will be
* used within the preg_match_callback. Otherwise a default regex of
* ([a-zA-Z0-9_\+\-%]+) is used.
*
* If one of the 'simplified' patterns within the rule is :results, this is
* treated specially and uses a ([a-zA-Z0-9_\+\-%\/]+) regex which will match
* the same as the default as well as / .
*
* This causes the pattern to match entire sections of uri's. Allowing a
* simple pattern like the default /:controller/:action/:results to match
* uri's like /foo/bar/a/b/c/d/e/f/g/h and cause everything after /foo/bar
* to be added to the Payload object as results (which are in turn transformed
* into key => value pairs).
*
* TODO: A lot of this fluffing around could (and likely should) be moved into
* the __construct and other protected internal methods.
*
* @param Proem\Http\Request $request
*/
public function process(Request $request)
{
// All routes must at least specify a rule.
if (!isset($this->options['rule'])) {
return false;
}
// Setup.
$rule = str_replace('/', '/?', $this->options['rule']);
$targets = isset($this->options['targets']) ? $this->options['targets'] : [];
$customFilters = isset($this->options['filters']) ? $this->options['filters'] : [];
$defaultTokens = $this->defaultTokens;
$defaultFilters = $this->defaultFilters;
$url = $request->getRequestUri();
$tokens = [];
$values = [];
$results = [];
// Build the main regular expression.
$regex = '^' . preg_replace_callback(
'@:[\w]+@',
function ($matches) use ($customFilters, $defaultTokens, $defaultFilters) {
$key = str_replace(':', '', $matches[0]);
if (isset($customFilters[$key])) {
if (isset($defaultFilters[$customFilters[$key]])) {
return '(' . $defaultFilters[$customFilters[$key]] . ')';
} else {
if ($customFilters[$key]{0} == ':') {
throw new \RuntimeException(
"The custom filter named \"{$key}\" references a
non-existent builtin filter named \"{$customFilters[$key]}\"."
);
} else {
return '(' . $customFilters[$key] . ')';
}
}
} elseif (isset($defaultTokens[$key])) {
return '(' . $defaultTokens[$key] . ')';
} else {
return '(' . $defaultFilters[':default'] . ')';
}
},
$rule
) . '/?$';
// Find all tokens.
preg_match_all('@:([\w]+)@', $rule, $tokens, PREG_PATTERN_ORDER);
$tokens = $tokens[0];
// Test the main regular expression against the url.
if (preg_match('@^' . $regex . '$@', $url, $values)) {
// Discard *all* matches index.
array_shift($values);
// Match tokens to values
foreach ($tokens as $index => $value) {
if (isset($values[$index])) {
$results[substr($value, 1)] = urldecode($values[$index]);
}
}
// Replace any results with specific targets..
foreach ($targets as $key => $value) {
$results[$key] = $value;
}
// If the current $key is "params" & the string within $value looks
// like a / seperated string, parse it into an associative array.
if (isset($results['params']) && strpos($results['params'], '/') !== false) {
preg_match_all("/([^\/]+)\/([^\/]+)/", $results['params'], $m);
$results = array_merge($results, array_combine($m[1], $m[2]));
unset($results['params']);
}
// Route has matched, return results.
return $results;
}
return false;
}
}