forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAphrontRoutingMap.php
159 lines (125 loc) · 3.49 KB
/
AphrontRoutingMap.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
<?php
/**
* Collection of routes on a site for an application.
*
* @task info Map Information
* @task routing Routing
*/
final class AphrontRoutingMap extends Phobject {
private $site;
private $application;
private $routes = array();
/* -( Map Info )----------------------------------------------------------- */
public function setSite(AphrontSite $site) {
$this->site = $site;
return $this;
}
public function getSite() {
return $this->site;
}
public function setApplication(PhabricatorApplication $application) {
$this->application = $application;
return $this;
}
public function getApplication() {
return $this->application;
}
public function setRoutes(array $routes) {
$this->routes = $routes;
return $this;
}
public function getRoutes() {
return $this->routes;
}
/* -( Routing )------------------------------------------------------------ */
/**
* Find the route matching a path, if one exists.
*
* @param string Path to route.
* @return AphrontRoutingResult|null Routing result, if path matches map.
* @task routing
*/
public function routePath($path) {
$map = $this->getRoutes();
foreach ($map as $route => $value) {
$match = $this->tryRoute($route, $value, $path);
if (!$match) {
continue;
}
$result = $this->newRoutingResult();
$application = $result->getApplication();
$controller_class = $match['class'];
$controller = newv($controller_class, array());
$controller->setCurrentApplication($application);
$result
->setController($controller)
->setURIData($match['data']);
return $result;
}
return null;
}
/**
* Test a sub-map to see if any routes match a path.
*
* @param string Path to route.
* @param string Pattern from the map.
* @param string Value from the map.
* @return dict<string, wild>|null Match details, if path matches sub-map.
* @task routing
*/
private function tryRoute($route, $value, $path) {
$has_submap = is_array($value);
if (!$has_submap) {
// If the value is a controller rather than a sub-map, any matching
// route must completely consume the path.
$pattern = '(^'.$route.'\z)';
} else {
$pattern = '(^'.$route.')';
}
$data = null;
$ok = preg_match($pattern, $path, $data);
if ($ok === false) {
throw new Exception(
pht(
'Routing fragment "%s" is not a valid regular expression.',
$route));
}
if (!$ok) {
return null;
}
$path_match = $data[0];
// Clean up the data. We only want to retain named capturing groups, not
// the duplicated numeric captures.
foreach ($data as $k => $v) {
if (is_numeric($k)) {
unset($data[$k]);
}
}
if (!$has_submap) {
return array(
'class' => $value,
'data' => $data,
);
}
$sub_path = substr($path, strlen($path_match));
foreach ($value as $sub_route => $sub_value) {
$result = $this->tryRoute($sub_route, $sub_value, $sub_path);
if ($result) {
$result['data'] += $data;
return $result;
}
}
return null;
}
/**
* Build a new routing result for this map.
*
* @return AphrontRoutingResult New, empty routing result.
* @task routing
*/
private function newRoutingResult() {
return id(new AphrontRoutingResult())
->setSite($this->getSite())
->setApplication($this->getApplication());
}
}