Code cleanup and refactoring
This commit is contained in:
parent
20fb124cd5
commit
47ec8f5f96
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -30,14 +30,14 @@ class Response extends Message
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $reasonPhrase;
|
||||
private $reasonPhrase;
|
||||
|
||||
/**
|
||||
* HTTP status code
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $statusCode;
|
||||
private $statusCode;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue