Merge pull request #99 from skipperbent/development

Development
This commit is contained in:
Simon Sessingø
2016-04-22 15:38:02 +02:00
3 changed files with 61 additions and 39 deletions

View File

@@ -176,20 +176,33 @@ This is a simple example of an integration into a framework.
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
```php
namespace MyProject;
<?php
<?php
namespace Pecee;
use Pecee\Exception\RouterException;
use Pecee\Handler\ExceptionHandler;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\SimpleRouter\RouterBase;
use Pecee\SimpleRouter\SimpleRouter;
class Router extends SimpleRouter {
protected static $defaultExceptionHandler;
protected static $defaultMiddlewares = array();
public static function start($defaultNamespace = null) {
// Debug information
Debug::getInstance()->add('Router initialised.');
// Load framework specific controllers
static::get('/js-wrap', 'ControllerJs@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.js.wrap');
static::get('/css-wrap', 'ControllerCss@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.css.wrap');
static::get('/captcha', 'ControllerCaptcha@show', ['namespace' => '\Pecee\Controller']);
// Load routes.php
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'routes.php';
if(file_exists($file)) {
require_once $file;
}
@@ -199,20 +212,57 @@ class Router extends SimpleRouter {
// Handle exceptions
try {
if(count(static::$defaultMiddlewares)) {
/* @var $middleware \Pecee\Http\Middleware\IMiddleware */
foreach(static::$defaultMiddlewares as $middleware) {
$middleware = new $middleware();
if(!($middleware instanceof IMiddleware)) {
throw new RouterException('Middleware must be implement the IMiddleware interface.');
}
$middleware->handle(RouterBase::getInstance()->getRequest());
}
}
parent::start($defaultNamespace);
} catch(\Exception $e) {
if(self::$defaultExceptionHandler !== null) {
$class = new self::$defaultExceptionHandler();
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
$route = RouterBase::getInstance()->getLoadedRoute();
// Otherwise use the fallback default exceptions handler
if(static::$defaultExceptionHandler !== null) {
static::loadExceptionHandler(static::$defaultExceptionHandler, $route, $e);
}
throw $e;
}
}
public static function setDefaultExceptionHandler($handler) {
self::$defaultExceptionHandler = $handler;
protected static function loadExceptionHandler($class, $route, $e) {
$class = new $class();
if(!($class instanceof ExceptionHandler)) {
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\ExceptionHandler');
}
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
}
public static function defaultExceptionHandler($handler) {
static::$defaultExceptionHandler = $handler;
}
/**
* Add default middleware that will be loaded before any route
* @param string|array $middlewares
*/
public static function defaultMiddleware($middlewares) {
if(is_array($middlewares)) {
static::$defaultMiddlewares = $middlewares;
} else {
static::$defaultMiddlewares[] = $middlewares;
}
}
}

View File

@@ -19,7 +19,6 @@ class RouterBase {
protected $defaultNamespace;
protected $bootManagers;
protected $baseCsrfVerifier;
protected $middlewaresToLoad;
protected $exceptionHandlers;
// TODO: clean up - cut some of the methods down to smaller pieces
@@ -30,7 +29,6 @@ class RouterBase {
$this->backStack = array();
$this->controllerUrlMap = array();
$this->bootManagers = array();
$this->middlewaresToLoad = array();
$this->exceptionHandlers = array();
}
@@ -91,12 +89,9 @@ class RouterBase {
$route->renderRoute($this->request);
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
// Load middleware on group if route matches
if($route->getPrefix() !== null && $route->matchRoute($this->request)) {
if($route->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route->getExceptionHandler();
}
$this->middlewaresToLoad[] = $route;
// Add exceptionhandler
if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route->getExceptionHandler();
}
}
@@ -136,22 +131,8 @@ class RouterBase {
// Loop through each route-request
$this->processRoutes($this->routes);
// Load group middlewares
/* @var $route RouterEntry */
foreach($this->middlewaresToLoad as $route) {
$route->loadMiddleware($this->request);
}
$routeNotAllowed = false;
// Make sure routes with longer urls are rendered first
usort($this->controllerUrlMap, function($a, $b) {
if(strlen($a->getUrl()) < strlen($b->getUrl())) {
return 1;
}
return -1;
});
$max = count($this->controllerUrlMap);
/* @var $route RouterEntry */

View File

@@ -92,20 +92,11 @@ class RouterGroup extends RouterEntry {
if($this->getNamespace() !== null && isset($settings['namespace'])) {
unset($settings['namespace']);
}
if(is_array($settings)) {
$this->settings = array_merge($this->settings, $settings);
}
return $this;
}
public function getMergeableSettings() {
$settings = parent::getMergeableSettings();
if(isset($settings['middleware'])) {
unset($settings['middleware']);
}
return $settings;
}
}