Change the way Handler goes about building the response. This now happen in the constructor, and Handler::getResponse() returns the Response instance instead of using Handler::respond to trigger the response.

This commit is contained in:
PJ Dietz 2012-09-08 16:56:54 -04:00
parent 6664c73605
commit 44b6e9b118
3 changed files with 59 additions and 12 deletions

View File

@ -11,43 +11,81 @@ class Handler {
protected $response;
protected $matches;
public function __construct($matches=null) {
public function __construct($request, $matches=null) {
$this->request = $request;
if (is_null($matches)) {
$matches = array();
}
$this->matches = $matches;
$this->request = Request::getRequest();
$this->response = new Response();
$this->buildResponse();
}
public function respond() {
protected function buildResponse() {
switch ($this->request->method) {
case 'GET':
$this->get();
break;
case 'HEAD':
$this->head();
break;
case 'POST':
$this->post();
break;
case 'PUT':
$this->put();
break;
case 'DELETE':
$this->delete();
break;
case 'PATCH':
$this->patch();
break;
case 'OPTIONS':
$this->options();
break;
}
$this->response->body = 'Do stuff' . "\n";
$this->response->body = print_r($this->matches, true);
$this->response->statusCode = 200;
$this->response->respond();
}
public function getResponse() {
return $this->response;
}
protected function get() {
$this->response->statusCode = 405;
}
protected function head() {
$this->response->statusCode = 405;
}
protected function post() {
$this->response->statusCode = 405;
}
protected function put() {
$this->response->statusCode = 405;
}
protected function patch() {
$this->response->statusCode = 405;
}
protected function options() {
$this->response->statusCode = 405;
}
}

View File

@ -34,12 +34,21 @@ class Response {
* HTTP status code
* @var int
*/
public $statusCode = 500;
public $statusCode;
// -------------------------------------------------------------------------
public function __construct() {
$this->headers = array();
public function __construct($statusCode=500, $body='', $headers=null) {
$this->statusCode = $statusCode;
$this->body = $body;
if (is_array($headers)) {
$this->headers = $headers;
} else {
$this->headers = array();
}
}
// -------------------------------------------------------------------------

View File

@ -55,7 +55,7 @@ class Router {
require_once($route->handlerPath);
}
return $handler = new $route->handler($matches);
return $handler = new $route->handler($request, $matches);
}