diff --git a/README.md b/README.md index 7bfc8aa..28d5844 100644 --- a/README.md +++ b/README.md @@ -398,7 +398,7 @@ Named routes allow the convenient generation of URLs or redirects for specific r ```php SimpleRouter::get('/user/profile', function () { - // + // Your code here })->name('profile'); ``` @@ -1090,7 +1090,7 @@ $route = new RouteUrl('/answer/1', function() { }); -$route->setMiddleware(\Demo\Middlewares\AuthMiddleware::class); +$route->addMiddleware(\Demo\Middlewares\AuthMiddleware::class); $route->setNamespace('\Demo\Controllers'); $route->setPrefix('v1'); diff --git a/src/Pecee/Handlers/CallbackExceptionHandler.php b/src/Pecee/Handlers/CallbackExceptionHandler.php index d22753b..435a752 100644 --- a/src/Pecee/Handlers/CallbackExceptionHandler.php +++ b/src/Pecee/Handlers/CallbackExceptionHandler.php @@ -17,7 +17,7 @@ class CallbackExceptionHandler implements IExceptionHandler protected $callback; - public function __construct($callback) + public function __construct(\Closure $callback) { $this->callback = $callback; } diff --git a/src/Pecee/SimpleRouter/Exceptions/RouterException.php b/src/Pecee/SimpleRouter/Exceptions/RouterException.php deleted file mode 100644 index 974ef6b..0000000 --- a/src/Pecee/SimpleRouter/Exceptions/RouterException.php +++ /dev/null @@ -1,7 +0,0 @@ -getMiddlewares()[$i]; - $middleware = $this->loadClass($middleware); + if (is_object($middleware) === false) { + $middleware = $this->loadClass($middleware); + } if (($middleware instanceof IMiddleware) === false) { - throw new HttpException($middleware . ' must be instance of Middleware'); + throw new HttpException($middleware . ' must be inherit the IMiddleware interface'); } $middleware->handle($request); diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 9ef2f89..a898ee3 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -2,6 +2,7 @@ namespace Pecee\SimpleRouter\Route; +use Pecee\Http\Middleware\IMiddleware; use Pecee\Http\Request; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; @@ -52,7 +53,7 @@ abstract class Route implements IRoute protected function loadClass($name) { if (class_exists($name) === false) { - throw new NotFoundHttpException(sprintf('Class %s does not exist', $name), 404); + throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $name), 404); } return new $name(); @@ -62,39 +63,45 @@ abstract class Route implements IRoute { $callback = $this->getCallback(); - if ($callback !== null && is_callable($callback)) { + if ($callback === null) { + return; + } + + /* Render callback function */ + if (is_callable($callback) === true) { /* When the callback is a function */ call_user_func_array($callback, $this->getParameters()); - } else { + return; - /* When the callback is a method */ - $controller = explode('@', $callback); - - $namespace = $this->getNamespace(); - - $className = ($namespace !== null && $controller[0][0] !== '\\') ? $namespace . '\\' . $controller[0] : $controller[0]; - - $class = $this->loadClass($className); - $method = $controller[1]; - - if (method_exists($class, $method) === false) { - throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); - } - - $parameters = $this->getParameters(); - - /* Filter parameters with null-value */ - - if ($this->filterEmptyParams === true) { - $parameters = array_filter($parameters, function ($var) { - return ($var !== null); - }); - } - - call_user_func_array([$class, $method], $parameters); } + + /* When the callback is a class + method */ + $controller = explode('@', $callback); + + $namespace = $this->getNamespace(); + + $className = ($namespace !== null && $controller[0][0] !== '\\') ? $namespace . '\\' . $controller[0] : $controller[0]; + + $class = $this->loadClass($className); + $method = $controller[1]; + + if (method_exists($class, $method) === false) { + throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404); + } + + $parameters = $this->getParameters(); + + /* Filter parameters with null-value */ + + if ($this->filterEmptyParams === true) { + $parameters = array_filter($parameters, function ($var) { + return ($var !== null); + }); + } + + call_user_func_array([$class, $method], $parameters); } protected function parseParameters($route, $url, $parameterRegex = null) @@ -478,12 +485,12 @@ abstract class Route implements IRoute } /** - * Set middleware class-name + * Add middleware class-name * - * @param string $middleware + * @param IMiddleware|string $middleware * @return static */ - public function setMiddleware($middleware) + public function addMiddleware($middleware) { $this->middlewares[] = $middleware; diff --git a/src/Pecee/SimpleRouter/Route/RouteGroup.php b/src/Pecee/SimpleRouter/Route/RouteGroup.php index bd0771e..e6397b1 100644 --- a/src/Pecee/SimpleRouter/Route/RouteGroup.php +++ b/src/Pecee/SimpleRouter/Route/RouteGroup.php @@ -20,7 +20,7 @@ class RouteGroup extends Route implements IGroupRoute */ public function matchDomain(Request $request) { - if (count($this->domains) === 0) { + if ($this->domains === null || count($this->domains) === 0) { return true; } @@ -59,10 +59,10 @@ class RouteGroup extends Route implements IGroupRoute /** * Add exception handler * - * @param IExceptionHandler $handler + * @param IExceptionHandler|string $handler * @return static $this */ - public function addExceptionHandler(IExceptionHandler $handler) + public function addExceptionHandler($handler) { $this->exceptionHandlers[] = $handler; diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 45d9e77..01f898e 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -134,21 +134,18 @@ class Router $group = $route; - if ($route->getCallback() !== null && is_callable($route->getCallback())) { + $this->processingRoute = true; + $route->renderRoute($this->request); + $this->processingRoute = false; - $this->processingRoute = true; - $route->renderRoute($this->request); - $this->processingRoute = false; - - if ($route->matchRoute($url, $this->request) === true) { - - /* Add exception handlers */ - if (count($route->getExceptionHandlers()) > 0) { - /** @noinspection AdditionOperationOnArraysInspection */ - $exceptionHandlers += $route->getExceptionHandlers(); - } + if ($route->matchRoute($url, $this->request) === true) { + /* Add exception handlers */ + if (count($route->getExceptionHandlers()) > 0) { + /** @noinspection AdditionOperationOnArraysInspection */ + $exceptionHandlers += $route->getExceptionHandlers(); } + } } @@ -289,9 +286,9 @@ class Router $rewriteUrl = $this->request->getRewriteUrl(); if ($rewriteUrl !== null) { - $message = sprintf('Route not found: %s (redirected from: %s)', $rewriteUrl, $this->request->getUri()); + $message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()); } else { - $message = sprintf('Route not found: %s', $this->request->getUri()); + $message = sprintf('Route not found: "%s"', $this->request->getUri()); } $this->handleException(new NotFoundHttpException($message, 404)); diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index f347743..dd804e5 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -309,16 +309,13 @@ class SimpleRouter * @param \Closure $callback * @return CallbackExceptionHandler $callbackHandler */ - public static function error($callback) + public static function error(\Closure $callback) { $routes = static::router()->getRoutes(); $callbackHandler = new CallbackExceptionHandler($callback); $group = new RouteGroup(); - $group->setCallback(function () { - - }); $group->addExceptionHandler($callbackHandler); array_unshift($routes, $group); diff --git a/test/RouterRewriteTest.php b/test/RouterRewriteTest.php index 176ad39..e72bce6 100644 --- a/test/RouterRewriteTest.php +++ b/test/RouterRewriteTest.php @@ -1,4 +1,5 @@ setExpectedException(\Pecee\SimpleRouter\Exceptions\NotFoundHttpException::class); + + TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) { + + if (strtolower($request->getUri()) == '/my/test') { + $request->setRewriteUrl('/another-non-existing'); + + return $request; + } + + }); + + TestRouter::debug('/my/test', 'get'); + } + } \ No newline at end of file diff --git a/test/RouterUrlTest.php b/test/RouterUrlTest.php index 34816fe..93e6108 100644 --- a/test/RouterUrlTest.php +++ b/test/RouterUrlTest.php @@ -8,7 +8,7 @@ require_once 'Helpers/TestRouter.php'; class RouterUrlTest extends PHPUnit_Framework_TestCase { - public function testSimularUrls() + public function testSimilarUrls() { // Match normal route on alias TestRouter::resource('/url11', 'DummyController@method1');