/
dispatcher.php
262 lines (215 loc) · 7.24 KB
/
dispatcher.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
<?php
/**
* The Dispatcher is used to map an incoming HTTP request to a Controller
* producing a response which is then rendered. To initialize an instance of
* class Trails_Dispatcher you have to give three configuration settings:
*
* trails_root - the absolute file path to a directory containing the
* applications controllers, views etc.
* trails_uri - the URI to which routes to mapped Controller/Actions
* are appended
* default_controller - the route to a controller, that is used if no
* controller is given, that is the route is equal to '/'
*
* After instantiation of a dispatcher you have to call method #dispatch with
* the request uri to be mapped to a controller/action pair.
*
* @package trails
*
* @author mlunzena
* @copyright (c) Authors
* @version $Id: trails.php 7001 2008-04-04 11:20:27Z mlunzena $
*/
class Trails_Dispatcher {
# TODO (mlunzena) Konfiguration muss anders geschehen
/**
* This is the absolute file path to the trails application directory.
*
* @access public
* @var string
*/
public $trails_root;
/**
* This is the URI to which routes to controller/actions are appended.
*
* @access public
* @var string
*/
public $trails_uri;
/**
* This variable contains the route to the default controller.
*
* @access public
* @var string
*/
public $default_controller;
/**
* Constructor.
*
* @param string absolute file path to a directory containing the
* applications controllers, views etc.
* @param string the URI to which routes to mapped Controller/Actions
* are appended
* @param string the route to a controller, that is used if no
* controller is given, that is the route is equal to '/'
*
* @return void
*/
function __construct($trails_root,
$trails_uri,
$default_controller) {
$this->trails_root = $trails_root;
$this->trails_uri = $trails_uri;
$this->default_controller = $default_controller;
}
/**
* Maps a string to a response which is then rendered.
*
* @param string The requested URI.
*
* @return void
*/
function dispatch($uri) {
# E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE|E_RECOVERABLE_ERROR = 5888
$old_handler = set_error_handler(array($this, 'error_handler'), 5888);
ob_start();
$level = ob_get_level();
$this->map_uri_to_response($this->clean_request_uri((string) $uri))->output();
while (ob_get_level() >= $level) {
ob_end_flush();
}
if (isset($old_handler)) {
set_error_handler($old_handler);
}
}
/**
* Maps an URI to a response by figuring out first what controller to
* instantiate, then delegating the unconsumed part of the URI to the
* controller who returns an appropriate response object or throws a
* Trails_Exception.
*
* @param string the URI string
*
* @return mixed a response object
*/
function map_uri_to_response($uri) {
try {
if ('' === $uri) {
if (!$this->file_exists($this->default_controller . '.php')) {
throw new Trails_MissingFile(
"Default controller '{$this->default_controller}' not found'");
}
$controller_path = $this->default_controller;
$unconsumed = $uri;
}
else {
list($controller_path, $unconsumed) = $this->parse($uri);
}
$controller = $this->load_controller($controller_path);
$response = $controller->perform($unconsumed);
} catch (Exception $e) {
$response = isset($controller) ? $controller->rescue($e)
: $this->trails_error($e);
}
return $response;
}
function trails_error($exception) {
ob_clean();
# show details for local requests
$detailed = @$_SERVER['REMOTE_ADDR'] === '127.0.0.1';
$body = sprintf('<html><head><title>Trails Error</title></head>'.
'<body><h1>%s</h1><pre>%s</pre></body></html>',
htmlentities($exception->__toString()),
$detailed
? htmlentities($exception->getTraceAsString())
: '');
if ($exception instanceof Trails_Exception) {
$response = new Trails_Response($body,
$exception->headers,
$exception->getCode(),
$exception->getMessage());
}
else {
$response = new Trails_Response($body, array(), 500,
$exception->getMessage());
}
return $response;
}
/**
* Clean up URI string by removing the query part and leading slashes.
*
* @param string an URI string
*
* @return string the cleaned string
*/
function clean_request_uri($uri) {
if (FALSE !== ($pos = strpos($uri, '?'))) {
$uri = substr($uri, 0, $pos);
}
return ltrim($uri, '/');
}
/**
* <MethodDescription>
*
* @param type <description>
* @param type <description>
*
* @return type <description>
*/
function parse($unconsumed, $controller = NULL) {
list($head, $tail) = $this->split_on_first_slash($unconsumed);
if (!preg_match('/^\w+$/', $head)) {
throw new Trails_RoutingError("No route matches '$head'");
}
$controller = (isset($controller) ? $controller . '/' : '') . $head;
if ($this->file_exists($controller . '.php')) {
return array($controller, $tail);
}
else if ($this->file_exists($controller)) {
return $this->parse($tail, $controller);
}
throw new Trails_RoutingError("No route matches '$head'");
}
function split_on_first_slash($str) {
preg_match(":([^/]*)(/+)?(.*):", $str, $matches);
return array($matches[1], $matches[3]);
}
function file_exists($path) {
return file_exists("{$this->trails_root}/controllers/$path");
}
/**
* Loads the controller file for a given controller path and return an
* instance of that controller. If an error occures, an exception will be
* thrown.
*
* @param string the relative controller path
*
* @return TrailsController an instance of that controller
*/
function load_controller($controller) {
require_once "{$this->trails_root}/controllers/{$controller}.php";
$class = Trails_Inflector::camelize($controller) . 'Controller';
if (!class_exists($class)) {
throw new Trails_UnknownController("Controller missing: '$class'");
}
return new $class($this);
}
/**
* This method transforms E_USER_* and E_RECOVERABLE_ERROR to
* Trails_Exceptions.
*
* @param integer the level of the error raised
* @param string the error message
* @param string the filename that the error was raised in
* @param integer the line number the error was raised at
* @param array an array of every variable that existed in the scope the
* error was triggered in
*
* @throws Trails_Exception
*
* @return void
*/
function error_handler($errno, $string, $file, $line, $context) {
throw new Trails_Exception(500, $string);
}
}