diff --git a/Handler.inc.php b/Handler.inc.php index 594f54d..1ea819f 100644 --- a/Handler.inc.php +++ b/Handler.inc.php @@ -5,12 +5,42 @@ namespace wellrested; require_once(dirname(__FILE__) . '/Request.inc.php'); require_once(dirname(__FILE__) . '/Response.inc.php'); +/******************************************************************************* + * Handler + * + * @package WellRESTed + * + ******************************************************************************/ + +/** + * @property Response response The Response to the request + */ class Handler { + /** + * The HTTP request to respond to. + * @var Request + */ protected $request; + + /** + * The HTTP response to send based on the request. + * @var Response + */ protected $response; + + /** + * Matches array from the preg_match() call used to find this Handler. + * @var array + */ protected $matches; + /** + * Create a new Handler for a specific request. + * + * @param Request $request + * @param array $matches + */ public function __construct($request, $matches=null) { $this->request = $request; @@ -25,9 +55,33 @@ class Handler { } + // ------------------------------------------------------------------------- + // !Accessors + + public function __get($name) { + + switch ($name) { + case 'response': + return $this->getResponse(); + default: + throw new Exception('Property ' . $name . ' does not exist.'); + } + + } // __get() + + public function getResponse() { + return $this->response; + } + + /** + * Prepare the Response. Override this method if your subclass needs to + * repond to any non-standard HTTP methods. Otherwise, override the + * get, post, put, etc. methods. + */ protected function buildResponse() { switch ($this->request->method) { + case 'GET': $this->get(); break; @@ -60,35 +114,66 @@ class Handler { } - public function getResponse() { - return $this->response; - } + // ------------------------------------------------------------------------- + // !HTTP Methods + // Each of these methods corresponds to a standard HTTP method. Each method + // has no arguments and returns nothing, but should affect the instance's + // response member. + // + // By default, the methods will provide a 405 Method Not Allowed header. + + /** + * Method for handling HTTP GET requests. + */ protected function get() { $this->response->statusCode = 405; } + /** + * Method for handling HTTP HEAD requests. + */ protected function head() { - $this->response->statusCode = 405; + + // The default function calls the instance's get() method, then sets + // the resonse's body member to an empty string. + + $this->get(); + + if ($this->response->statusCode == 200) { + $this->response->setBody('', false); + } + } + /** + * Method for handling HTTP POST requests. + */ protected function post() { $this->response->statusCode = 405; } + /** + * Method for handling HTTP PUT requests. + */ protected function put() { $this->response->statusCode = 405; } + /** + * Method for handling HTTP PATCH requests. + */ protected function patch() { $this->response->statusCode = 405; } + /** + * Method for handling HTTP OPTION requests. + */ protected function options() { $this->response->statusCode = 405; } -} - +} // Handler ?> diff --git a/Request.inc.php b/Request.inc.php index 60e0ce2..9a6a26c 100644 --- a/Request.inc.php +++ b/Request.inc.php @@ -164,6 +164,6 @@ namespace wellrested; } // getRequest() -} +} // Request ?> diff --git a/Resource.inc.php b/Resource.inc.php deleted file mode 100644 index 2256c33..0000000 --- a/Resource.inc.php +++ /dev/null @@ -1,12 +0,0 @@ - diff --git a/Response.inc.php b/Response.inc.php index 160701b..d52964c 100644 --- a/Response.inc.php +++ b/Response.inc.php @@ -49,7 +49,7 @@ class Response { $this->headers = array(); } - } + } // __construct() // ------------------------------------------------------------------------- // !Accessors @@ -76,7 +76,7 @@ class Response { throw new Exception('Property ' . $name . ' does not exist or is read only.'); } - } // __get() + } // __set() public function getBody() { return $this->body; @@ -92,12 +92,17 @@ class Response { * of the new body string. * * @param string $value + * @param bool $setContentLenght Automatically add a Content-length header */ - public function setBody($value) { - $this->body = $value; - $this->setHeader('Content-Length', strlen($value)); - } + public function setBody($value, $setContentLength=true) { + $this->body = $value; + + if ($setContentLength === true) { + $this->setHeader('Content-Length', strlen($value)); + } + + } // setBody() /** * Add or update a header to a given value @@ -117,7 +122,6 @@ class Response { */ public function hasHeader($header) { return isset($this->headers[$header]); - } /** @@ -172,10 +176,8 @@ class Response { print $this->body; } - exit; + } // respond() - } - -} +} // Response ?> diff --git a/Route.inc.php b/Route.inc.php index 6371b6f..70dca99 100644 --- a/Route.inc.php +++ b/Route.inc.php @@ -2,6 +2,13 @@ namespace wellrested; +/******************************************************************************* + * Route + * + * @package WellRESTed + * + ******************************************************************************/ + class Route { const RE_SLUG = '[0-9a-zA-Z\-_]+'; @@ -14,28 +21,26 @@ class Route { public $pattern; public $handler; public $handlerPath; - - - + public $uriTemplate; public function __construct($pattern, $handler, $handlerPath=null) { + $this->pattern = $pattern; $this->handler = $handler; $this->handlerPath = $handlerPath; - } + + } // __construct static public function newFromUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) { $pattern = ''; if ($uriTemplate[0] === '/') { - $uriTemplate = substr($uriTemplate, 1); + $parts = explode('/', substr($uriTemplate, 1)); + } else { + $parts = explode('/', $uriTemplate); } - $parts = explode('/', $uriTemplate); - - // TODO: Look up what characters are legal in Level 1 template expressions. - $expressionPattern = '/{([a-zA-Z]+)}/'; foreach ($parts as $part) { @@ -69,10 +74,12 @@ class Route { $pattern = '/^' . $pattern . '$/'; $klass = __CLASS__; - return new $klass($pattern, $handler, $handlerPath); + $route = new $klass($pattern, $handler, $handlerPath); + $route->uriTemplate = $uriTemplate; + return $route; - } + } // newFromUriTemplate() -} +} // Route ?> diff --git a/Router.inc.php b/Router.inc.php index a041669..83b7fd8 100644 --- a/Router.inc.php +++ b/Router.inc.php @@ -5,6 +5,13 @@ namespace wellrested; require_once(dirname(__FILE__) . '/Request.inc.php'); require_once(dirname(__FILE__) . '/Route.inc.php'); +/******************************************************************************* + * Router + * + * @package WellRESTed + * + ******************************************************************************/ + class Router { protected $routes; @@ -27,7 +34,7 @@ class Router { $this->routes[] = new Route($pattern, $handler, $handlerPath); - } + } // addRoute() public function addUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) { @@ -37,7 +44,7 @@ class Router { $this->routes[] = Route::newFromUriTemplate($uriTemplate, $handler, $handlerPath, $variables); - } + } // addUriTemplate() public function getRequestHandler($request=null) { @@ -63,8 +70,8 @@ class Router { return false; - } + } // getRequestHandler() -} +} // Router ?>