Merge pull request #25 from skipperbent/development

Development
This commit is contained in:
Simon Sessingø
2015-11-01 10:14:57 +01:00
4 changed files with 26 additions and 13 deletions
+2 -2
View File
@@ -27,7 +27,7 @@ class Request {
$this->host = $_SERVER['HTTP_HOST']; $this->host = $_SERVER['HTTP_HOST'];
$this->uri = $_SERVER['REQUEST_URI']; $this->uri = $_SERVER['REQUEST_URI'];
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']); $this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
$this->headers = getallheaders(); $this->headers = array_change_key_case(getallheaders(), CASE_LOWER);
} }
/** /**
@@ -105,7 +105,7 @@ class Request {
* @return string|null * @return string|null
*/ */
public function getHeader($name) { public function getHeader($name) {
return (isset($this->headers[$name])) ? $this->headers[$name] : null; return (isset($this->headers[strtolower($name)])) ? $this->headers[strtolower($name)] : null;
} }
/** /**
+1
View File
@@ -58,6 +58,7 @@ class RouterBase {
$newPrefixes = $prefixes; $newPrefixes = $prefixes;
$mergedSettings = array_merge($settings, $route->getMergeableSettings()); $mergedSettings = array_merge($settings, $route->getMergeableSettings());
if($route->getPrefix()) { if($route->getPrefix()) {
array_push($newPrefixes, rtrim($route->getPrefix(), '/')); array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
} }
+16 -4
View File
@@ -10,26 +10,26 @@ abstract class RouterEntry {
const REQUEST_TYPE_POST = 'post'; const REQUEST_TYPE_POST = 'post';
const REQUEST_TYPE_GET = 'get'; const REQUEST_TYPE_GET = 'get';
const REQUEST_TYPE_PUT = 'put'; const REQUEST_TYPE_PUT = 'put';
const REQUEST_TYPE_PATCH = 'patch';
const REQUEST_TYPE_DELETE = 'delete'; const REQUEST_TYPE_DELETE = 'delete';
public static $allowedRequestTypes = array( public static $allowedRequestTypes = array(
self::REQUEST_TYPE_DELETE, self::REQUEST_TYPE_DELETE,
self::REQUEST_TYPE_GET, self::REQUEST_TYPE_GET,
self::REQUEST_TYPE_POST, self::REQUEST_TYPE_POST,
self::REQUEST_TYPE_PUT self::REQUEST_TYPE_PUT,
self::REQUEST_TYPE_PATCH
); );
protected $settings; protected $settings;
protected $callback; protected $callback;
protected $parameters; protected $parameters;
protected $parametersRegex;
protected $regexMatch;
public function __construct() { public function __construct() {
$this->settings = array(); $this->settings = array();
$this->settings['requestMethods'] = array(); $this->settings['requestMethods'] = array();
$this->settings['parametersRegex'] = array();
$this->parameters = array(); $this->parameters = array();
$this->parametersRegex = array();
} }
/** /**
@@ -245,6 +245,17 @@ abstract class RouterEntry {
public function loadMiddleware(Request $request) { public function loadMiddleware(Request $request) {
if($this->getMiddleware()) { if($this->getMiddleware()) {
if(is_array($this->getMiddleware())) {
foreach($this->getMiddleware() as $middleware) {
$middleware = $this->loadClass($middleware);
if (!($middleware instanceof IMiddleware)) {
throw new RouterException($middleware . ' must be instance of Middleware');
}
/* @var $class Middleware */
$middleware->handle($request);
}
} else {
$middleware = $this->loadClass($this->getMiddleware()); $middleware = $this->loadClass($this->getMiddleware());
if (!($middleware instanceof IMiddleware)) { if (!($middleware instanceof IMiddleware)) {
throw new RouterException($this->getMiddleware() . ' must be instance of Middleware'); throw new RouterException($this->getMiddleware() . ' must be instance of Middleware');
@@ -254,6 +265,7 @@ abstract class RouterEntry {
$middleware->handle($request); $middleware->handle($request);
} }
} }
}
public function renderRoute(Request $request) { public function renderRoute(Request $request) {
+1 -1
View File
@@ -57,7 +57,7 @@ class SimpleRouter {
public static function put($url, $callback, array $settings = null) { public static function put($url, $callback, array $settings = null) {
$route = new RouterRoute($url, $callback); $route = new RouterRoute($url, $callback);
$route->addSettings($settings); $route->addSettings($settings);
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_PUT)); $route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_PUT, RouterRoute::REQUEST_TYPE_PATCH));
$router = RouterBase::getInstance(); $router = RouterBase::getInstance();
$router->addRoute($route); $router->addRoute($route);