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 $response;
protected $matches; protected $matches;
public function __construct($matches=null) { public function __construct($request, $matches=null) {
$this->request = $request;
if (is_null($matches)) { if (is_null($matches)) {
$matches = array(); $matches = array();
} }
$this->matches = $matches; $this->matches = $matches;
$this->request = Request::getRequest();
$this->response = new Response(); $this->response = new Response();
$this->buildResponse();
} }
public function respond() { protected function buildResponse() {
switch ($this->request->method) { switch ($this->request->method) {
case 'GET': case 'GET':
$this->get();
break;
case 'HEAD': case 'HEAD':
$this->head();
break;
case 'POST': case 'POST':
$this->post();
break;
case 'PUT': case 'PUT':
$this->put();
break;
case 'DELETE': case 'DELETE':
$this->delete();
break;
case 'PATCH': case 'PATCH':
$this->patch();
break;
case 'OPTIONS': 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,14 +34,23 @@ class Response {
* HTTP status code * HTTP status code
* @var int * @var int
*/ */
public $statusCode = 500; public $statusCode;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
public function __construct() { 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(); $this->headers = array();
} }
}
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// !Accessors // !Accessors

View File

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