Code cleanup and refactoring

This commit is contained in:
PJ Dietz 2013-05-19 12:59:57 -04:00
parent 20fb124cd5
commit 47ec8f5f96
5 changed files with 32 additions and 26 deletions

View File

@ -15,7 +15,7 @@ namespace pjdietz\WellRESTed;
* *
* @property-read Response response The Response to the request * @property-read Response response The Response to the request
*/ */
class Handler abstract class Handler
{ {
/** /**
* The HTTP request to respond to. * The HTTP request to respond to.
@ -72,6 +72,7 @@ class Handler
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->{$method}(); return $this->{$method}();
} }
return null;
} }
/** /**
@ -140,7 +141,7 @@ class Handler
*/ */
protected function get() protected function get()
{ {
$this->response->statusCode = 405; $this->respondWithMethodNotAllowed();
} }
/** /**
@ -167,7 +168,7 @@ class Handler
*/ */
protected function post() protected function post()
{ {
$this->response->statusCode = 405; $this->respondWithMethodNotAllowed();
} }
/** /**
@ -177,7 +178,7 @@ class Handler
*/ */
protected function put() protected function put()
{ {
$this->response->statusCode = 405; $this->respondWithMethodNotAllowed();
} }
/** /**
@ -187,7 +188,7 @@ class Handler
*/ */
protected function delete() protected function delete()
{ {
$this->response->statusCode = 405; $this->respondWithMethodNotAllowed();
} }
/** /**
@ -197,7 +198,7 @@ class Handler
*/ */
protected function patch() protected function patch()
{ {
$this->response->statusCode = 405; $this->respondWithMethodNotAllowed();
} }
/** /**
@ -206,6 +207,14 @@ class Handler
* This method should modify the instance's response member. * This method should modify the instance's response member.
*/ */
protected function options() protected function options()
{
$this->respondWithMethodNotAllowed();
}
/**
* Provide a default response for unsupported methods.
*/
protected function respondWithMethodNotAllowed()
{ {
$this->response->statusCode = 405; $this->response->statusCode = 405;
} }

View File

@ -81,6 +81,7 @@ abstract class Message
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->{$method}(); return $this->{$method}();
} }
return null;
} }
/** /**
@ -101,7 +102,7 @@ abstract class Message
* Magic accessor method * Magic accessor method
* *
* @param string $propertyName * @param string $propertyName
* @return mixed * @return boolean
*/ */
public function __isset($propertyName) public function __isset($propertyName)
{ {
@ -109,6 +110,7 @@ abstract class Message
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->{$method}(); return $this->{$method}();
} }
return false;
} }
/** /**

View File

@ -36,35 +36,35 @@ class Request extends Message
* *
* @var string * @var string
*/ */
protected $hostname; private $hostname;
/** /**
* HTTP method or verb for the request * HTTP method or verb for the request
* *
* @var string * @var string
*/ */
protected $method = 'GET'; private $method = 'GET';
/** /**
* Path component of the URI for the request * Path component of the URI for the request
* *
* @var string * @var string
*/ */
protected $path = '/'; private $path = '/';
/** /**
* Array of fragments of the path, delimited by slashes * Array of fragments of the path, delimited by slashes
* *
* @var array * @var array
*/ */
protected $pathParts; private $pathParts;
/** /**
* Associative array of query parameters * Associative array of query parameters
* *
* @var array * @var array
*/ */
protected $query; private $query;
/** /**
* Singleton instance derived from reading info from Apache. * Singleton instance derived from reading info from Apache.
@ -363,13 +363,9 @@ class Request extends Message
public static function getRequest() public static function getRequest()
{ {
if (!isset(self::$theRequest)) { if (!isset(self::$theRequest)) {
$request = new Request();
$klass = __CLASS__;
$request = new $klass();
$request->readHttpRequest(); $request->readHttpRequest();
self::$theRequest = $request; self::$theRequest = $request;
} }
return self::$theRequest; return self::$theRequest;

View File

@ -30,14 +30,14 @@ class Response extends Message
* *
* @var string * @var string
*/ */
protected $reasonPhrase; private $reasonPhrase;
/** /**
* HTTP status code * HTTP status code
* *
* @var int * @var int
*/ */
protected $statusCode; private $statusCode;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -58,15 +58,14 @@ class Router
$path = $request->path; $path = $request->path;
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
if (preg_match($route->pattern, $path, $matches)) { if (preg_match($route->pattern, $path, $matches)) {
if (is_subclass_of($route->handler, '\pjdietz\WellRESTed\Handler')) {
$klass = $route->handler; $handler = new $route->handler($request, $matches);
$handler = new $klass($request, $matches); return $handler->response;
return $handler->response; } else {
return $this->getNoRouteResponse($request);
}
} }
} }
return $this->getNoRouteResponse($request); return $this->getNoRouteResponse($request);