Commenting and tidying.
This commit is contained in:
parent
44b6e9b118
commit
37b75d69d1
|
|
@ -5,12 +5,42 @@ namespace wellrested;
|
||||||
require_once(dirname(__FILE__) . '/Request.inc.php');
|
require_once(dirname(__FILE__) . '/Request.inc.php');
|
||||||
require_once(dirname(__FILE__) . '/Response.inc.php');
|
require_once(dirname(__FILE__) . '/Response.inc.php');
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Handler
|
||||||
|
*
|
||||||
|
* @package WellRESTed
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property Response response The Response to the request
|
||||||
|
*/
|
||||||
class Handler {
|
class Handler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HTTP request to respond to.
|
||||||
|
* @var Request
|
||||||
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HTTP response to send based on the request.
|
||||||
|
* @var Response
|
||||||
|
*/
|
||||||
protected $response;
|
protected $response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches array from the preg_match() call used to find this Handler.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
protected $matches;
|
protected $matches;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Handler for a specific request.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param array $matches
|
||||||
|
*/
|
||||||
public function __construct($request, $matches=null) {
|
public function __construct($request, $matches=null) {
|
||||||
|
|
||||||
$this->request = $request;
|
$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() {
|
protected function buildResponse() {
|
||||||
|
|
||||||
switch ($this->request->method) {
|
switch ($this->request->method) {
|
||||||
|
|
||||||
case 'GET':
|
case 'GET':
|
||||||
$this->get();
|
$this->get();
|
||||||
break;
|
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() {
|
protected function get() {
|
||||||
$this->response->statusCode = 405;
|
$this->response->statusCode = 405;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for handling HTTP HEAD requests.
|
||||||
|
*/
|
||||||
protected function head() {
|
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() {
|
protected function post() {
|
||||||
$this->response->statusCode = 405;
|
$this->response->statusCode = 405;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for handling HTTP PUT requests.
|
||||||
|
*/
|
||||||
protected function put() {
|
protected function put() {
|
||||||
$this->response->statusCode = 405;
|
$this->response->statusCode = 405;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for handling HTTP PATCH requests.
|
||||||
|
*/
|
||||||
protected function patch() {
|
protected function patch() {
|
||||||
$this->response->statusCode = 405;
|
$this->response->statusCode = 405;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for handling HTTP OPTION requests.
|
||||||
|
*/
|
||||||
protected function options() {
|
protected function options() {
|
||||||
$this->response->statusCode = 405;
|
$this->response->statusCode = 405;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // Handler
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,6 @@ namespace wellrested;
|
||||||
|
|
||||||
} // getRequest()
|
} // getRequest()
|
||||||
|
|
||||||
}
|
} // Request
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace wellrested;
|
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/Request.inc.php');
|
|
||||||
require_once(dirname(__FILE__) . '/Response.inc.php');
|
|
||||||
|
|
||||||
class Resource {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
@ -49,7 +49,7 @@ class Response {
|
||||||
$this->headers = array();
|
$this->headers = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // __construct()
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// !Accessors
|
// !Accessors
|
||||||
|
|
@ -76,7 +76,7 @@ class Response {
|
||||||
throw new Exception('Property ' . $name . ' does not exist or is read only.');
|
throw new Exception('Property ' . $name . ' does not exist or is read only.');
|
||||||
}
|
}
|
||||||
|
|
||||||
} // __get()
|
} // __set()
|
||||||
|
|
||||||
public function getBody() {
|
public function getBody() {
|
||||||
return $this->body;
|
return $this->body;
|
||||||
|
|
@ -92,12 +92,17 @@ class Response {
|
||||||
* of the new body string.
|
* of the new body string.
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @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;
|
$this->body = $value;
|
||||||
|
|
||||||
|
if ($setContentLength === true) {
|
||||||
$this->setHeader('Content-Length', strlen($value));
|
$this->setHeader('Content-Length', strlen($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // setBody()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add or update a header to a given value
|
* Add or update a header to a given value
|
||||||
|
|
@ -117,7 +122,6 @@ class Response {
|
||||||
*/
|
*/
|
||||||
public function hasHeader($header) {
|
public function hasHeader($header) {
|
||||||
return isset($this->headers[$header]);
|
return isset($this->headers[$header]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -172,10 +176,8 @@ class Response {
|
||||||
print $this->body;
|
print $this->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit;
|
} // respond()
|
||||||
|
|
||||||
}
|
} // Response
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
namespace wellrested;
|
namespace wellrested;
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Route
|
||||||
|
*
|
||||||
|
* @package WellRESTed
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
class Route {
|
class Route {
|
||||||
|
|
||||||
const RE_SLUG = '[0-9a-zA-Z\-_]+';
|
const RE_SLUG = '[0-9a-zA-Z\-_]+';
|
||||||
|
|
@ -14,27 +21,25 @@ class Route {
|
||||||
public $pattern;
|
public $pattern;
|
||||||
public $handler;
|
public $handler;
|
||||||
public $handlerPath;
|
public $handlerPath;
|
||||||
|
public $uriTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct($pattern, $handler, $handlerPath=null) {
|
public function __construct($pattern, $handler, $handlerPath=null) {
|
||||||
|
|
||||||
$this->pattern = $pattern;
|
$this->pattern = $pattern;
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
$this->handlerPath = $handlerPath;
|
$this->handlerPath = $handlerPath;
|
||||||
}
|
|
||||||
|
} // __construct
|
||||||
|
|
||||||
static public function newFromUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
|
static public function newFromUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
|
||||||
|
|
||||||
$pattern = '';
|
$pattern = '';
|
||||||
|
|
||||||
if ($uriTemplate[0] === '/') {
|
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]+)}/';
|
$expressionPattern = '/{([a-zA-Z]+)}/';
|
||||||
|
|
||||||
|
|
@ -69,10 +74,12 @@ class Route {
|
||||||
$pattern = '/^' . $pattern . '$/';
|
$pattern = '/^' . $pattern . '$/';
|
||||||
|
|
||||||
$klass = __CLASS__;
|
$klass = __CLASS__;
|
||||||
return new $klass($pattern, $handler, $handlerPath);
|
$route = new $klass($pattern, $handler, $handlerPath);
|
||||||
|
$route->uriTemplate = $uriTemplate;
|
||||||
|
return $route;
|
||||||
|
|
||||||
}
|
} // newFromUriTemplate()
|
||||||
|
|
||||||
}
|
} // Route
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,13 @@ namespace wellrested;
|
||||||
require_once(dirname(__FILE__) . '/Request.inc.php');
|
require_once(dirname(__FILE__) . '/Request.inc.php');
|
||||||
require_once(dirname(__FILE__) . '/Route.inc.php');
|
require_once(dirname(__FILE__) . '/Route.inc.php');
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Router
|
||||||
|
*
|
||||||
|
* @package WellRESTed
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
class Router {
|
class Router {
|
||||||
|
|
||||||
protected $routes;
|
protected $routes;
|
||||||
|
|
@ -27,7 +34,7 @@ class Router {
|
||||||
|
|
||||||
$this->routes[] = new Route($pattern, $handler, $handlerPath);
|
$this->routes[] = new Route($pattern, $handler, $handlerPath);
|
||||||
|
|
||||||
}
|
} // addRoute()
|
||||||
|
|
||||||
public function addUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
|
public function addUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
|
||||||
|
|
||||||
|
|
@ -37,7 +44,7 @@ class Router {
|
||||||
|
|
||||||
$this->routes[] = Route::newFromUriTemplate($uriTemplate, $handler, $handlerPath, $variables);
|
$this->routes[] = Route::newFromUriTemplate($uriTemplate, $handler, $handlerPath, $variables);
|
||||||
|
|
||||||
}
|
} // addUriTemplate()
|
||||||
|
|
||||||
public function getRequestHandler($request=null) {
|
public function getRequestHandler($request=null) {
|
||||||
|
|
||||||
|
|
@ -63,8 +70,8 @@ class Router {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
} // getRequestHandler()
|
||||||
|
|
||||||
}
|
} // Router
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue