diff --git a/src/pjdietz/WellRESTed/Handler.php b/src/pjdietz/WellRESTed/Handler.php index db73217..0079cbc 100644 --- a/src/pjdietz/WellRESTed/Handler.php +++ b/src/pjdietz/WellRESTed/Handler.php @@ -15,7 +15,7 @@ namespace pjdietz\WellRESTed; * * @property-read Response response The Response to the request */ -class Handler +abstract class Handler { /** * The HTTP request to respond to. @@ -72,6 +72,7 @@ class Handler if (method_exists($this, $method)) { return $this->{$method}(); } + return null; } /** @@ -140,7 +141,7 @@ class Handler */ protected function get() { - $this->response->statusCode = 405; + $this->respondWithMethodNotAllowed(); } /** @@ -167,7 +168,7 @@ class Handler */ protected function post() { - $this->response->statusCode = 405; + $this->respondWithMethodNotAllowed(); } /** @@ -177,7 +178,7 @@ class Handler */ protected function put() { - $this->response->statusCode = 405; + $this->respondWithMethodNotAllowed(); } /** @@ -187,7 +188,7 @@ class Handler */ protected function delete() { - $this->response->statusCode = 405; + $this->respondWithMethodNotAllowed(); } /** @@ -197,7 +198,7 @@ class Handler */ protected function patch() { - $this->response->statusCode = 405; + $this->respondWithMethodNotAllowed(); } /** @@ -206,6 +207,14 @@ class Handler * This method should modify the instance's response member. */ protected function options() + { + $this->respondWithMethodNotAllowed(); + } + + /** + * Provide a default response for unsupported methods. + */ + protected function respondWithMethodNotAllowed() { $this->response->statusCode = 405; } diff --git a/src/pjdietz/WellRESTed/Message.php b/src/pjdietz/WellRESTed/Message.php index 789c15f..37e80c6 100644 --- a/src/pjdietz/WellRESTed/Message.php +++ b/src/pjdietz/WellRESTed/Message.php @@ -81,6 +81,7 @@ abstract class Message if (method_exists($this, $method)) { return $this->{$method}(); } + return null; } /** @@ -101,7 +102,7 @@ abstract class Message * Magic accessor method * * @param string $propertyName - * @return mixed + * @return boolean */ public function __isset($propertyName) { @@ -109,6 +110,7 @@ abstract class Message if (method_exists($this, $method)) { return $this->{$method}(); } + return false; } /** diff --git a/src/pjdietz/WellRESTed/Request.php b/src/pjdietz/WellRESTed/Request.php index c665bc3..850bc0f 100644 --- a/src/pjdietz/WellRESTed/Request.php +++ b/src/pjdietz/WellRESTed/Request.php @@ -36,35 +36,35 @@ class Request extends Message * * @var string */ - protected $hostname; + private $hostname; /** * HTTP method or verb for the request * * @var string */ - protected $method = 'GET'; + private $method = 'GET'; /** * Path component of the URI for the request * * @var string */ - protected $path = '/'; + private $path = '/'; /** * Array of fragments of the path, delimited by slashes * * @var array */ - protected $pathParts; + private $pathParts; /** * Associative array of query parameters * * @var array */ - protected $query; + private $query; /** * Singleton instance derived from reading info from Apache. @@ -363,13 +363,9 @@ class Request extends Message public static function getRequest() { if (!isset(self::$theRequest)) { - - $klass = __CLASS__; - $request = new $klass(); + $request = new Request(); $request->readHttpRequest(); - self::$theRequest = $request; - } return self::$theRequest; diff --git a/src/pjdietz/WellRESTed/Response.php b/src/pjdietz/WellRESTed/Response.php index b6a9e57..d580469 100644 --- a/src/pjdietz/WellRESTed/Response.php +++ b/src/pjdietz/WellRESTed/Response.php @@ -30,14 +30,14 @@ class Response extends Message * * @var string */ - protected $reasonPhrase; + private $reasonPhrase; /** * HTTP status code * * @var int */ - protected $statusCode; + private $statusCode; // ------------------------------------------------------------------------- diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index ea12efe..9feecda 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -58,15 +58,14 @@ class Router $path = $request->path; foreach ($this->routes as $route) { - if (preg_match($route->pattern, $path, $matches)) { - - $klass = $route->handler; - $handler = new $klass($request, $matches); - return $handler->response; - + if (is_subclass_of($route->handler, '\pjdietz\WellRESTed\Handler')) { + $handler = new $route->handler($request, $matches); + return $handler->response; + } else { + return $this->getNoRouteResponse($request); + } } - } return $this->getNoRouteResponse($request);