- Simplified exception-handling (see demo project for examples).
- Optimised sample-project.
- Optimised and added further unit-tests.
- Optimised and bugfixes.
This commit is contained in:
Simon Sessingø
2016-10-20 08:31:21 +02:00
parent 4e054dccf5
commit a8620cbc70
13 changed files with 151 additions and 175 deletions
+10 -39
View File
@@ -8,14 +8,12 @@
namespace Demo;
use Pecee\Exception\RouterException;
use Pecee\Handler\IExceptionHandler;
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) {
@@ -23,50 +21,23 @@ class Router extends SimpleRouter {
// change this to whatever makes sense in your project
require_once 'routes.php';
// 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());
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());
}
// Set default namespace
$defaultNamespace = '\\Demo\\Controllers';
parent::start($defaultNamespace);
} catch(\Exception $e) {
$route = RouterBase::getInstance()->getLoadedRoute();
// Otherwise use the fallback default exceptions handler
if(static::$defaultExceptionHandler !== null) {
static::loadExceptionHandler(static::$defaultExceptionHandler, $route, $e);
}
throw $e;
}
}
// Set default namespace
$defaultNamespace = '\\Demo\\Controllers';
protected static function loadExceptionHandler($class, $route, $e) {
$class = new $class();
parent::start($defaultNamespace);
if(!($class instanceof IExceptionHandler)) {
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\IExceptionHandler');
}
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
}
public static function defaultExceptionHandler($handler) {
static::$defaultExceptionHandler = $handler;
}
/**