Added Handler, Router, and Route classes.

This commit is contained in:
PJ Dietz 2012-09-03 14:02:49 -04:00
parent 81dd2c3f1c
commit 6664c73605
3 changed files with 204 additions and 0 deletions

56
Handler.inc.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace wellrested;
require_once(dirname(__FILE__) . '/Request.inc.php');
require_once(dirname(__FILE__) . '/Response.inc.php');
class Handler {
protected $request;
protected $response;
protected $matches;
public function __construct($matches=null) {
if (is_null($matches)) {
$matches = array();
}
$this->matches = $matches;
$this->request = Request::getRequest();
$this->response = new Response();
}
public function respond() {
switch ($this->request->method) {
case 'GET':
case 'HEAD':
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
case 'OPTIONS':
}
$this->response->body = 'Do stuff' . "\n";
$this->response->body = print_r($this->matches, true);
$this->response->statusCode = 200;
$this->response->respond();
}
}
?>

78
Route.inc.php Normal file
View File

@ -0,0 +1,78 @@
<?php
namespace wellrested;
class Route {
const RE_SLUG = '[0-9a-zA-Z\-_]+';
const RE_NUM = '[0-9]+';
const RE_ALPHA = '[a-zA-Z]+';
const RE_ALPHANUM = '[0-9a-zA-Z]+';
static public $defaultVariablePattern = self::RE_SLUG;
public $pattern;
public $handler;
public $handlerPath;
public function __construct($pattern, $handler, $handlerPath=null) {
$this->pattern = $pattern;
$this->handler = $handler;
$this->handlerPath = $handlerPath;
}
static public function newFromUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
$pattern = '';
if ($uriTemplate[0] === '/') {
$uriTemplate = substr($uriTemplate, 1);
}
$parts = explode('/', $uriTemplate);
// TODO: Look up what characters are legal in Level 1 template expressions.
$expressionPattern = '/{([a-zA-Z]+)}/';
foreach ($parts as $part) {
$pattern .= '\/';
if (preg_match($expressionPattern, $part, $matches)) {
$variablePattern = self::$defaultVariablePattern;
if (count($matches) === 2) {
$variableName = $matches[1];
if (isset($groups[$variableName])) {
$variablePattern = $groups[$variableName];
}
}
$pattern .= sprintf('(?<%s>%s)', $variableName, $variablePattern);
} else {
$pattern .= $part;
}
}
$pattern = '/^' . $pattern . '$/';
$klass = __CLASS__;
return new $klass($pattern, $handler, $handlerPath);
}
}
?>

70
Router.inc.php Normal file
View File

@ -0,0 +1,70 @@
<?php
namespace wellrested;
require_once(dirname(__FILE__) . '/Request.inc.php');
require_once(dirname(__FILE__) . '/Route.inc.php');
class Router {
protected $routes;
public $handlerPathPattern = '%s.inc.php';
public function __construct() {
$this->routes = array();
}
protected function getHandlerPath($handler) {
return sprintf($this->handlerPathPattern, $handler);
}
public function addRoute($pattern, $handler, $handlerPath=null) {
if (is_null($handlerPath)) {
$handlerPath = $this->getHandlerPath($handler);
}
$this->routes[] = new Route($pattern, $handler, $handlerPath);
}
public function addUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) {
if (is_null($handlerPath)) {
$handlerPath = $this->getHandlerPath($handler);
}
$this->routes[] = Route::newFromUriTemplate($uriTemplate, $handler, $handlerPath, $variables);
}
public function getRequestHandler($request=null) {
if (is_null($request)) {
$request = Request::getRequest();
}
$path = $request->path;
foreach ($this->routes as $route) {
if (preg_match($route->pattern, $path, $matches)) {
if (!class_exists($route->handler)) {
require_once($route->handlerPath);
}
return $handler = new $route->handler($matches);
}
}
return false;
}
}
?>