pluf2

pluf2 Commit Details


Date:2008-12-19 02:55:28 (16 years 3 days ago)
Author:Loic d'Anterroches
Branch:master
Commit:b519e074b6d00dbf4b247bf22a95868a946c110d
Parents: ba7d7d844ae31710499c9a3b0109327c6086b9e5
Message:Restructured the dispatcher to catch exceptions.

The dispatcher now correctly catches exceptions thrown by the
middleware stack.
Changes:

File differences

src/Pluf/Dispatcher.php
3030
3131
3232
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
33
34
35
36
37
38
5539
56
40
5741
58
59
60
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
6176
6277
6378
......
7792
7893
7994
80
81
82
83
95
96
97
98
8499
85100
86101
......
104119
105120
106121
107
108
109
110
111
112
113
114
115
116122
117123
124
125
126
118127
119128
120129
*/
public static function dispatch($query='')
{
$query = preg_replace('#^(/)+#', '/', '/'.$query);
$req = new Pluf_HTTP_Request($query);
$middleware = array();
foreach (Pluf::f('middleware_classes', array()) as $mw) {
$middleware[] = new $mw();
}
$skip = false;
foreach ($middleware as $mw) {
if (method_exists($mw, 'process_request')) {
$response = $mw->process_request($req);
if ($response !== false) {
// $response is a response
$response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
$skip = true;
break;
}
}
}
if ($skip === false) {
$response = self::match($req);
if (!empty($req->response_vary_on)) {
$response->headers['Vary'] = $req->response_vary_on;
try {
$query = preg_replace('#^(/)+#', '/', '/'.$query);
$req = new Pluf_HTTP_Request($query);
$middleware = array();
foreach (Pluf::f('middleware_classes', array()) as $mw) {
$middleware[] = new $mw();
}
$middleware = array_reverse($middleware);
$skip = false;
foreach ($middleware as $mw) {
if (method_exists($mw, 'process_response')) {
$response = $mw->process_response($req, $response);
}
if (method_exists($mw, 'process_request')) {
$response = $mw->process_request($req);
if ($response !== false) {
// $response is a response
if (Pluf::f('pluf_runtime_header', false)) {
$response->headers['X-Perf-Runtime'] = sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime']));
}
$response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
$skip = true;
break;
}
}
}
if ($skip === false) {
$response = self::match($req);
if (!empty($req->response_vary_on)) {
$response->headers['Vary'] = $req->response_vary_on;
}
$middleware = array_reverse($middleware);
foreach ($middleware as $mw) {
if (method_exists($mw, 'process_response')) {
$response = $mw->process_response($req, $response);
}
}
if (Pluf::f('pluf_runtime_header', false)) {
$response->headers['X-Perf-Runtime'] = sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime']));
}
$response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
}
} catch (Exception $e) {
if (Pluf::f('debug', false) == true) {
$response = new Pluf_HTTP_Response_ServerErrorDebug($e);
} else {
$response = new Pluf_HTTP_Response_ServerError($e);
}
$response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
}
$priority[$key] = $control['priority'];
}
array_multisort($priority, SORT_ASC, $GLOBALS['_PX_views']);
foreach ($GLOBALS['_PX_views'] as $key => $ctl) {
$match = array();
if (preg_match($ctl['regex'], $req->query, $match)) {
try {
try {
foreach ($GLOBALS['_PX_views'] as $key => $ctl) {
$match = array();
if (preg_match($ctl['regex'], $req->query, $match)) {
$req->view = $ctl;
$m = new $ctl['model']();
if (isset($m->{$ctl['method'].'_precond'})) {
} else {
return $m->$ctl['method']($req, $match, $ctl['params']);
}
} catch (Pluf_HTTP_Error404 $e) {
// Need to add a 404 error handler
// something like Pluf::f('404_handler', 'class::method')
} catch (Exception $e) {
if (Pluf::f('debug', false) == true) {
return new Pluf_HTTP_Response_ServerErrorDebug($e);
} else {
return new Pluf_HTTP_Response_ServerError($e);
}
}
}
} catch (Pluf_HTTP_Error404 $e) {
// Need to add a 404 error handler
// something like Pluf::f('404_handler', 'class::method')
}
return new Pluf_HTTP_Response_NotFound($req);
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.06123s using 13 queries.