Commenting and tidying.

This commit is contained in:
PJ Dietz 2012-09-08 19:46:11 -04:00
parent 44b6e9b118
commit 37b75d69d1
6 changed files with 135 additions and 46 deletions

View File

@ -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
?>

View File

@ -164,6 +164,6 @@ namespace wellrested;
} // getRequest()
}
} // Request
?>

View File

@ -1,12 +0,0 @@
<?php
namespace wellrested;
require_once(dirname(__FILE__) . '/Request.inc.php');
require_once(dirname(__FILE__) . '/Response.inc.php');
class Resource {
}
?>

View File

@ -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) {
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
?>

View File

@ -2,6 +2,13 @@
namespace wellrested;
/*******************************************************************************
* Route
*
* @package WellRESTed
*
******************************************************************************/
class Route {
const RE_SLUG = '[0-9a-zA-Z\-_]+';
@ -14,27 +21,25 @@ 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);
// TODO: Look up what characters are legal in Level 1 template expressions.
}
$expressionPattern = '/{([a-zA-Z]+)}/';
@ -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
?>

View File

@ -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
?>