diff --git a/Handler.inc.php b/Handler.inc.php index 96471db..594f54d 100644 --- a/Handler.inc.php +++ b/Handler.inc.php @@ -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; } } diff --git a/Response.inc.php b/Response.inc.php index e3e5e56..160701b 100644 --- a/Response.inc.php +++ b/Response.inc.php @@ -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(); + } + } // ------------------------------------------------------------------------- diff --git a/Router.inc.php b/Router.inc.php index 78f8759..a041669 100644 --- a/Router.inc.php +++ b/Router.inc.php @@ -55,7 +55,7 @@ class Router { require_once($route->handlerPath); } - return $handler = new $route->handler($matches); + return $handler = new $route->handler($request, $matches); }