Skip to content

Commit cc47bc9

Browse files
committed
Adding some code documentation and adding in body parsing for the request object
1 parent cf1e9f6 commit cc47bc9

File tree

3 files changed

+140
-8
lines changed

3 files changed

+140
-8
lines changed

src/GuahanWeb/Http/request.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function __construct() {
2424
}
2525

2626
public function __get($k) {
27+
static $body;
28+
2729
switch ($k) {
2830
case 'method':
2931
case 'headers':
@@ -32,6 +34,13 @@ public function __get($k) {
3234
return $this->$k;
3335
break;
3436

37+
case 'body':
38+
if (is_null($body)) {
39+
$body = $this->parseBody();
40+
}
41+
return $body;
42+
break;
43+
3544
default:
3645
return isset($this->params[$k]) ? $this->params[$k] : null;
3746
}
@@ -51,5 +60,24 @@ protected function getAllHeaders() {
5160
}
5261
return $headers;
5362
}
63+
64+
protected function parseBody() {
65+
// no body allowed for GET or DELETE requests
66+
if ($this->method == 'GET' || $this->method == 'DELETE') {
67+
return '';
68+
}
69+
70+
// Process POST and PUT appropriately
71+
$data = null;
72+
if (isset($this->headers['Content-Type']) && $this->headers['Content-Type'] == 'multipart/form-data') {
73+
$data = $_POST;
74+
} else {
75+
// handle raw body, and parse if content-type if application/json
76+
$data = file_get_contents('php://input');
77+
if (isset($this->headers['Content-Type']) && $this->headers['Content-Type'] == 'application/json') {
78+
$data = json_decode(trim($data));
79+
}
80+
}
81+
}
5482
}
5583

src/GuahanWeb/Http/response.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,53 @@
44
class Response {
55
protected $headers;
66

7+
/**
8+
* Initialize a new response object, defaulting to HTML type
9+
*
10+
* @constructor
11+
*/
712
public function __construct() {
8-
$this->headers = array();
13+
$this->headers = array(
14+
'content-type' => 'text/html'
15+
);
916
}
1017

18+
/**
19+
* Set a header to be sent with the response
20+
*
21+
* @param {string} The header name
22+
* @param {string} The header value
23+
* @return void
24+
*/
1125
public function setHeader($k, $v) {
1226
$this->headers[$k] = $v;
1327
}
1428

29+
/**
30+
* Set multiple headers to be sent with the response
31+
*
32+
* @param {array} A list of headers to be applied
33+
* @return void
34+
*/
1535
public function setHeaders($headers) {
1636
foreach ($headers as $k => $v) {
1737
$this->setHeader($k, $v);
1838
}
1939
}
2040

41+
/**
42+
* Send the response to the consumer
43+
*
44+
* @param {*} $body The content to send. If the content is an array or object, it will be treated as a JSON payload
45+
* @return void
46+
*/
2147
public function send($body, $code = 200) {
48+
// Handle JSON payloads
49+
if (is_array($body) || is_object($body)) {
50+
$this->setHeader('content-type', 'application/json');
51+
$body = json_encode($body);
52+
}
53+
2254
foreach ($this->headers as $k => $v) {
2355
header($k . ': ' . $v);
2456
}

src/GuahanWeb/Http/router.php

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
namespace GuahanWeb\Http;
33

4+
class RouterException extends \Exception {}
5+
46
class Router {
57
protected $routes;
8+
protected $supported_methods;
69

710
protected function __construct() {
8-
$this->routes = array(
9-
'GET' => array(),
10-
'POST' => array(),
11-
'PUT' => array(),
12-
'DELETE' => array()
13-
);
11+
$this->supported_methods = array('GET', 'POST', 'PUT', 'DELETE');
12+
$this->routes = array();
13+
foreach ($this->supported_methods as $method) {
14+
$this->routes[$method] = array();
15+
}
1416
}
1517

1618
static public function instance() {
@@ -58,10 +60,80 @@ protected function match($method, $uri, &$params = null) {
5860
return false;
5961
}
6062

63+
/**
64+
* Registers a new route
65+
*
66+
* @param {string|array} $method The HTTP method(s) this route supports
67+
* @param {string} $route The route pattern to register
68+
* @param {function} $handler The handler to execute when route and method matches
69+
* @return void
70+
*/
71+
public function route($method, $route, $handler) {
72+
if (is_array($method)) {
73+
foreach ($method as $m) {
74+
$this->route($m, $route, $handler);
75+
}
76+
} elseif ($method == '*') {
77+
foreach ($this->supported_methods as $m) {
78+
$this->route($m, $route, $handler);
79+
}
80+
} elseif (in_array($method, strtoupper($this->supported_methods))) {
81+
$this->routes[strtoupper($method)][$route] = $handler;
82+
} else {
83+
throw new RouterException(sprintf('Unsupported HTTP verb provided for route: %s', $method));
84+
}
85+
}
86+
87+
/**
88+
* Shorthand to register GET handlers
89+
*
90+
* @param {string} $route The route pattern to register
91+
* @param {function} $handler The handler to execute when route matches
92+
* @return void
93+
*/
6194
public function get($route, $handler) {
62-
$this->routes['GET'][$route] = $handler;
95+
$this->route('GET', $route, $handler);
96+
}
97+
98+
/**
99+
* Shorthand to register POST handlers
100+
*
101+
* @param {string} $route The route pattern to register
102+
* @param {function} $handler The handler to execute when route matches
103+
* @return void
104+
*/
105+
public function post($route, $handler) {
106+
$this->route('POST', $route, $handler);
107+
}
108+
109+
/**
110+
* Shorthand to register PUT handlers
111+
*
112+
* @param {string} $route The route pattern to register
113+
* @param {function} $handler The handler to execute when route matches
114+
* @return void
115+
*/
116+
public function put($route, $handler) {
117+
$this->route('PUT', $route, $handler);
118+
}
119+
120+
/**
121+
* Shorthand to register DELETE handlers
122+
*
123+
* @param {string} $route The route pattern to register
124+
* @param {function} $handler The handler to execute when route matches
125+
* @return void
126+
*/
127+
public function delete($route, $handler) {
128+
$this->route('DELETE', $route, $handler);
63129
}
64130

131+
/**
132+
* Activates the router to begin handling the HTTP request. If no matching routes are found,
133+
* we will automatically send a 404.
134+
*
135+
* @return void
136+
*/
65137
public function process() {
66138
$request = new Request();
67139
$response = new Response();

0 commit comments

Comments
 (0)