From ba736b47a35402aa719d9a9a1534e05b5a68db32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Fri, 22 Apr 2016 15:37:27 +0200 Subject: [PATCH] - Removed support from middlewares on each load - this should be implemented in custom Router instead. - Updated documentation to reflect how to implement Middlewares loaded on each route. --- README.md | 64 +++++++++++++++++++++++--- src/Pecee/SimpleRouter/RouterBase.php | 25 ++-------- src/Pecee/SimpleRouter/RouterGroup.php | 11 +---- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 32120cd..391a3d4 100644 --- a/README.md +++ b/README.md @@ -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; +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; + } } } diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index cb7d358..8f58016 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -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 */ diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index ebf972a..7068500 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -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; - } - } \ No newline at end of file