diff --git a/.gitignore b/.gitignore index 005d2e3..848a58c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -composer.lock \ No newline at end of file +composer.lock +vendor/ \ No newline at end of file diff --git a/README.md b/README.md index fbc0d86..5d4197e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Simple, fast and yet powerful PHP router that is easy to get integrated and in a Add the latest version of Simple PHP Router running this command. ``` -composer require pecee/framework +composer require pecee/simple-router ``` ## Notes @@ -34,7 +34,7 @@ In your ```index.php``` require your ```routes.php``` and call the ```routeReque This is an example of a basic ```index.php``` file: ```php -use \Pecee\SimpleRouter; +use \Pecee\SimpleRouter\SimpleRouter; require_once 'routes.php'; // change this to whatever makes sense in your project @@ -42,7 +42,7 @@ require_once 'routes.php'; // change this to whatever makes sense in your projec $defaultControllerNamespace = 'MyWebsite\\Controller'; // Do the routing -SimpleRouter::init($defaultControllerNamespace); +SimpleRouter::start($defaultControllerNamespace); ``` ## Adding routes @@ -160,23 +160,6 @@ class Router extends SimpleRouter { parent::start($defaultNamespace); } catch(\Exception $e) { - $route = RouterBase::getInstance()->getLoadedRoute(); - - $exceptionHandler = null; - - // Load and use exception-handler defined on group - - if($route && $route->getGroup()) { - $exceptionHandler = $route->getGroup()->getExceptionHandler(); - - if($exceptionHandler !== null) { - $class = new $exceptionHandler(); - $class->handleError(RouterBase::getInstance()->getRequest(), $route, $e); - } - } - - // Otherwise use the fallback default exceptions handler - if(self::$defaultExceptionHandler !== null) { $class = new self::$defaultExceptionHandler(); $class->handleError(RouterBase::getInstance()->getRequest(), $route, $e); diff --git a/src/Pecee/Handler/ExceptionHandler.php b/src/Pecee/Handler/ExceptionHandler.php new file mode 100644 index 0000000..0c44a80 --- /dev/null +++ b/src/Pecee/Handler/ExceptionHandler.php @@ -0,0 +1,11 @@ +request->loadedRoute = $route; $route->loadMiddleware($this->request); - $this->request->loadedRoute->renderRoute($this->request); + try { + $this->request->loadedRoute->renderRoute($this->request); + } catch(\Exception $e) { + $this->handleException($e); + } + break; } } if($routeNotAllowed) { - throw new RouterException('Route or method not allowed', 403); + $this->handleException(new RouterException('Route or method not allowed', 403)); } if(!$this->request->loadedRoute) { @@ -172,6 +178,19 @@ class RouterBase { } } + protected function handleException(\Exception $e) { + if($this->request->loadedRoute !== null && $this->request->loadedRoute->exceptionHandler !== null) { + $handler = new $this->request->loadedRoute->exceptionHandler(); + if(!($handler instanceof ExceptionHandler)) { + throw new RouterException('Exception handler must be instanceof ExceptionHandler.'); + } + + $handler->handleError($this->request, $this->request->loadedRoute, $e); + } + + throw $e; + } + /** * @return string */ diff --git a/test/Dummy/DummyController.php b/test/Dummy/DummyController.php new file mode 100644 index 0000000..be3254b --- /dev/null +++ b/test/Dummy/DummyController.php @@ -0,0 +1,9 @@ +setMethod('get'); + + $router = new \Pecee\SimpleRouter\RouterBase(); + + $route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start'); + $route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_GET)); + $route->addSettings(['middleware' => 'DummyMiddleware']); + $router->addRoute($route); + + try { + $router->routeRequest(); + }catch(Exception $e) { + $this->assertTrue(($e instanceof MiddlewareLoadedException)); + return; + } + + throw new Exception('Middleware not loaded'); + + } + + + +} \ No newline at end of file diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php new file mode 100644 index 0000000..c003dce --- /dev/null +++ b/test/RouterRouteTest.php @@ -0,0 +1,82 @@ +setMethod('get'); + + $router = new \Pecee\SimpleRouter\RouterBase(); + + $route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start'); + $route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_GET)); + + $router->addRoute($route); + $router->routeRequest(); + } + + public function testPost() { + \Pecee\Http\Request::getInstance()->setMethod('post'); + + $router = new \Pecee\SimpleRouter\RouterBase(); + + $route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start'); + + $route->addSettings(array()); + $route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_POST)); + + $router->addRoute($route); + $router->routeRequest(); + } + + public function testPut() { + \Pecee\Http\Request::getInstance()->setMethod('put'); + + $router = new \Pecee\SimpleRouter\RouterBase(); + + $route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start'); + $route->addSettings(array()); + $route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_PUT)); + + $router->addRoute($route); + $router->routeRequest(); + + } + + public function testDelete() { + \Pecee\Http\Request::getInstance()->setMethod('delete'); + + $router = new \Pecee\SimpleRouter\RouterBase(); + + $route = new \Pecee\SimpleRouter\RouterRoute('/my/test/url', 'DummyController@start'); + $route->addSettings(array()); + $route->setRequestMethods(array(\Pecee\SimpleRouter\RouterRoute::REQUEST_TYPE_DELETE)); + + $router->addRoute($route); + $router->routeRequest(); + + } + + public function testMethodNotAllowed() { + \Pecee\Http\Request::getInstance()->setMethod('post'); + + \Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start'); + + try { + \Pecee\SimpleRouter\SimpleRouter::start(); + } catch(\Exception $e) { + $this->assertEquals(403, $e->getCode()); + } + + } + +} \ No newline at end of file