Skip to content

Commit 08cf79c

Browse files
committed
Fixing up additional verbs and adding appropriate body parsing
1 parent b3edf1a commit 08cf79c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/GuahanWeb/Http/request.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public function __construct() {
2323
$this->params = array();
2424
}
2525

26+
/**
27+
* Let's try to make some variables more accessible for consumption
28+
*
29+
* @public
30+
* @param {string} $k The key being requested
31+
* @return {mixed}
32+
*/
2633
public function __get($k) {
2734
static $body;
2835

@@ -35,6 +42,8 @@ public function __get($k) {
3542
break;
3643

3744
case 'body':
45+
// We will only parse the body if it is requested, and then only
46+
// once. We want to keep the processing as light as possible
3847
if (is_null($body)) {
3948
$body = $this->parseBody();
4049
}
@@ -61,15 +70,21 @@ protected function getAllHeaders() {
6170
return $headers;
6271
}
6372

73+
/**
74+
* Attempt to parse the body of the request (considering all HTTP verbs and limitations).
75+
*
76+
* @protected
77+
* @return {string|object|null}
78+
*/
6479
protected function parseBody() {
6580
// no body allowed for GET or DELETE requests
6681
if ($this->method == 'GET' || $this->method == 'DELETE') {
67-
return '';
82+
return null;
6883
}
6984

70-
// Process POST and PUT appropriately
7185
$data = null;
72-
if (isset($_POST)) {
86+
if ($this->method == 'POST' && isset($this->headers['Content-Type']) && strpos($this->headers['Content-Type'], 'multipart/form-data') === 0) {
87+
// POST requests may have multipart/form-data already parsed
7388
$data = $_POST;
7489
} else {
7590
// handle raw body, and parse if content-type if application/json
@@ -78,6 +93,7 @@ protected function parseBody() {
7893
$data = json_decode(trim($data));
7994
}
8095
}
96+
return $data;
8197
}
8298
}
8399

src/GuahanWeb/Http/router.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ class RouterException extends \Exception {}
66
class Router {
77
protected $routes;
88
protected $supported_methods;
9+
protected $default_handlers;
910

1011
protected function __construct() {
1112
$this->supported_methods = array('GET', 'POST', 'PUT', 'DELETE');
1213
$this->routes = array();
1314
foreach ($this->supported_methods as $method) {
1415
$this->routes[$method] = array();
16+
$this->default_handlers[$method] = function ($req, $res) {
17+
// All verbs will return a 404 by default
18+
$res->send('Not found', 404);
19+
};
1520
}
1621
}
1722

23+
/**
24+
* Retrieves the shared router instance
25+
*
26+
* @public
27+
* @return {Router}
28+
*/
1829
static public function instance() {
1930
static $instance;
2031

@@ -25,6 +36,15 @@ static public function instance() {
2536
return $instance;
2637
}
2738

39+
/**
40+
* Looks for a matching registered route for the provided method and uri combination.
41+
*
42+
* @protected
43+
* @param {string} $method The HTTP verb of the request
44+
* @param {string} $uri The URI of the request
45+
* @param {array} $params Optional reference for extracted parameters
46+
* @return {function|false}
47+
*/
2848
protected function match($method, $uri, &$params = null) {
2949
if (isset($this->routes[$method])) {
3050
foreach ($this->routes[$method] as $route => $handler) {
@@ -140,7 +160,7 @@ public function process() {
140160

141161
if (false === ($handler = $this->match($request->method, $request->uri, $params))) {
142162
// Error case
143-
$response->send('Not found', 404);
163+
$this->default_handlers[$request->method]($request, $response);
144164
}
145165

146166
// route params applied to the request object

0 commit comments

Comments
 (0)