/
path_manager.php
317 lines (279 loc) · 10.8 KB
/
path_manager.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
class PathManager {
protected static $paths = array();
// @deprecated - $prefix should not be used, use $defaultPrefix
protected static $prefix = "";
protected static $defaultCacheTtl = array();
protected static $defaultPrefix = array();
public static function loadPath($pattern = NULL, $action = NULL, $controller = NULL, $location = NULL, $requestMethods = NULL, $cacheTtl = NULL, $name = NULL) {
if (!isset($pattern)) {
throw new CoreException("No pattern passed");
}
if (!isset($action)) {
$action = "templateForPattern";
}
if (!isset($location)) {
$location = self::getLocationFromTrace();
}
if (!isset($controller)) {
$controller = Utils::toCamelCase($location);
}
if (isset(self::$defaultPrefix[$location])) {
$prefix = self::$defaultPrefix[$location];
} else {
// @deprecated
$prefix = self::$prefix;
}
$pattern = $prefix.$pattern;
$path = new JaossPath();
$path->setPattern($pattern);
$path->setApp($location);
$path->setController($controller);
$path->setAction($action);
if ($name === null) {
$name = $location.":".$controller.":".$action;
}
$path->setName($name);
if ($requestMethods !== null) {
if (!is_array($requestMethods)) {
$requestMethods = array($requestMethods);
}
$path->setRequestMethods($requestMethods);
}
if ($cacheTtl === NULL && isset(self::$defaultCacheTtl[$location])) {
$cacheTtl = self::$defaultCacheTtl[$location];
Log::verbose("using defaultCacheTtl value for path [".$cacheTtl."]");
}
if ($cacheTtl > 0) {
$path->setCacheable(true);
$path->setCacheTtl($cacheTtl);
}
if (isset(self::$paths[$name])) {
throw new CoreException("Path name already exists", CoreException::DUPLICATE_PATH_NAME, array("path" => $path));
}
self::$paths[$name] = $path;
Log::verbose("Loading path: pattern [".$path->getPattern()."] location [apps/".$path->getApp()."] controller [".$path->getController()."] action [".$path->getAction()."] cacheTtl [".$cacheTtl."]");
}
public static function loadPaths() {
$args = func_get_args();
foreach ($args as $path) {
if (!is_array($path)) {
throw new CoreException("loadPaths called without array");
}
if (count($path) == 0) {
throw new CoreException("loadPaths called with empty array");
}
// this is this quickest way of determining if the array is
// associative or not, and is NOT reliable. but for our purposes, it'll
// do since we don't allow mixing associative / indexed arrays when declaring paths
if (isset($path[0])) {
// array is faster than range()
$keys = array(0,1,2,3,4,5,6);
} else {
Log::verbose("Loading path with associative array");
$keys = array("pattern", "action", "controller", "location", "method", "cacheTtl", "name");
}
$pattern = isset($path[$keys[0]]) ? $path[$keys[0]] : NULL;
$action = isset($path[$keys[1]]) ? $path[$keys[1]] : NULL;
$controller = isset($path[$keys[2]]) ? $path[$keys[2]] : NULL;
if (!isset($path[$keys[3]])) {
$location = self::getLocationFromTrace();
} else {
$location = $path[$keys[3]];
}
$methods = isset($path[$keys[4]]) ? $path[$keys[4]] : NULL;
$cacheTtl = isset($path[$keys[5]]) ? $path[$keys[5]] : NULL;
$name = isset($path[$keys[6]]) ? $path[$keys[6]] : NULL;
self::loadPath($pattern, $action, $controller, $location, $methods, $cacheTtl, $name);
}
}
public static function reset() {
self::$paths = array();
self::$prefix = "";
}
public static function matchUrl($url) {
if (empty(self::$paths)) {
throw new CoreException(
"No paths loaded",
CoreException::NO_PATHS_LOADED,
array(
"apps" => AppManager::getInstalledApps(),
)
);
}
// we want to be request-aware, since paths can be filtered to certain request methods
$request = JaossRequest::getInstance();
Log::verbose("Looking for match against URL [".$url."] and request method [".$request->getMethod()."]");
foreach (self::$paths as $path) {
if ($path->isDiscarded()) {
Log::verbose("Path already discarded, ignoring pattern [".$path->getPattern()."]");
continue;
}
// checking the request method first is cheaper than a preg_match()
if (!$path->supportsMethod($request->getMethod())) {
continue;
}
// check for simple(r) routes
$pattern = $path->getPattern();
if (substr($pattern, 0, 1) != "^" && substr($pattern, -1) != "$") {
$path->setPattern("^{$pattern}$");
$pattern = $path->getPattern();
}
if (preg_match("@{$pattern}@", $url, $matches)) {
Log::debug($request->getMethod()." [".$url."] matches [".$pattern."] => [apps/".$path->getApp()."/controllers/".$path->getController()."Controller::".$path->getAction()."]");
$matches = self::cleanMatches($matches);
$path->setMatches($matches);
return $path;
}
$path->setDiscarded(true);
Log::verbose("Discarding path pattern [".$pattern."]");
}
// no match :(
throw new CoreException(
"No matching path for URL ".$url,
CoreException::URL_NOT_FOUND,
array(
"paths" => self::$paths,
"url" => $url,
)
);
}
public static function getPaths() {
return self::$paths;
}
public static function setPaths($paths) {
self::$paths = $paths;
}
public static function getPathsToArray() {
$final = array();
foreach (self::getPaths() as $name => $path) {
$final[$name] = $path->toArray();
}
return $final;
}
public static function setPathsFromArray($data) {
foreach ($data as $path) {
self::loadPath(
$path['pattern'],
$path['action'],
$path['controller'],
$path['app'],
$path['requestMethods'],
$path['cacheTtl'],
$path['name']
);
}
}
private static function getLocationFromTrace() {
$trace = debug_backtrace();
if (!isset($trace[1]["file"])) {
throw new CoreException("file location not available");
}
$dir = dirname($trace[1]["file"]);
return substr($dir, strrpos($dir, "/")+1);
}
public static function setPrefix($prefix) {
Log::warn("PathManager::setPrefix() is deprecated. Use setAppPrefix instead");
self::$prefix = $prefix;
}
public static function clearPrefix() {
self::$prefix = "";
}
public static function getUrlForOptions($options) {
if (isset($options['name'])) {
Log::debug("Looking for path by name [".$options["name"]."]");
$path = self::getPathForName($options['name']);
} else {
$path = self::getPathForOptions($options);
}
$pattern = $path->getPattern();
$args = array_diff_key($options, array("app" => "", "controller" => "", "action" => "", "name" => ""));
if (count($args)) {
// got dynamic args. try and sort them out
foreach ($args as $key => $val) {
$result = preg_replace("@(.*)\(\?P\<".$key."\>.*?\)(.*)@", "$1__VAL__$2", $pattern);
if ($result === null || $result === $pattern) {
throw new CoreException("No matching argument found");
}
$result = preg_replace("@__VAL__@", $val, $result);
$pattern = $result;
}
}
if (substr($pattern, 0, 1) == "^" && substr($pattern, -1) == "$") {
$url = substr($pattern, 1, -1);
} else {
$url = $pattern;
}
return $url;
}
public static function getPathForName($name) {
if ($name === null || strlen($name) == 0) {
throw new CoreException(
"Path Name not valid",
CoreException::PATH_NAME_NOT_VALID,
array(
"name" => $name,
)
);
}
if (isset(self::$paths[$name])) {
return self::$paths[$name];
}
$parts = explode(":", $name);
if (count($parts) === 2) {
$controller = ucwords(strtolower($parts[0]));
$name = $parts[0].":".$controller.":".$parts[1];
if (isset(self::$paths[$name])) {
return self::$paths[$name];
}
}
throw new CoreException(
"No Path found for name",
CoreException::PATH_NAME_NOT_FOUND,
array(
'name' => $name,
'paths' => self::$paths,
)
);
}
public static function getPathForOptions($options) {
foreach (self::$paths as $path) {
if ($path->getApp() == $options["app"] &&
$path->getController() == $options["controller"] &&
$path->getAction() == $options["action"]) {
return $path;
}
}
throw new CoreException(
"No Path found for options",
CoreException::NO_PATH_FOUND_FOR_OPTIONS,
array(
'options' => $options,
'paths' => self::$paths,
)
);
}
public static function reloadPaths() {
foreach (self::$paths as $path) {
$path->setDiscarded(false);
}
}
public static function setAppCacheTtl($ttl) {
self::$defaultCacheTtl[self::getLocationFromTrace()] = $ttl;
}
public static function setAppPrefix($prefix) {
self::$defaultPrefix[self::getLocationFromTrace()] = $prefix;
}
/**
* ensure that any matches from the route are associative keys
*/
protected static function cleanMatches($matches) {
$clean = array();
foreach ($matches as $key => $val) {
if (!is_numeric($key)) {
$clean[$key] = $val;
}
}
return $clean;
}
}