Development

- Removed temporary `RouterException` class.
- Added object-types to parameters in `CallbackExceptionHandler` and `SimpleRouter` classes.
- Router now renders groups even if callback is null.
- Renamed `setMiddleware` to `addMiddleware` in `Route` class and `IRoute` interface.
- `addMiddleware` now accept both object and class strings in `Route` class.
- `addExceptionHandler` now accept both object and class strings in `RouteGroup` class.
- Added unit-test for rewrite-exception message change: `testRewriteExceptionMessage` in `RouterRewriteTest`.
- Fixed typo: renamed `testSimularUrls` to `testSimilarUrls` in `RouterUrlTest`.
This commit is contained in:
Simon Sessingø
2017-07-08 11:21:18 +02:00
parent d411b31cc2
commit b07348a3df
12 changed files with 84 additions and 69 deletions
+2 -2
View File
@@ -398,7 +398,7 @@ Named routes allow the convenient generation of URLs or redirects for specific r
```php ```php
SimpleRouter::get('/user/profile', function () { SimpleRouter::get('/user/profile', function () {
// // Your code here
})->name('profile'); })->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->setNamespace('\Demo\Controllers');
$route->setPrefix('v1'); $route->setPrefix('v1');
@@ -17,7 +17,7 @@ class CallbackExceptionHandler implements IExceptionHandler
protected $callback; protected $callback;
public function __construct($callback) public function __construct(\Closure $callback)
{ {
$this->callback = $callback; $this->callback = $callback;
} }
@@ -1,7 +0,0 @@
<?php
namespace Pecee\SimpleRouter\Exceptions;
class RouterException extends \Exception
{
}
+2 -2
View File
@@ -18,10 +18,10 @@ interface IGroupRoute extends IRoute
/** /**
* Add exception handler * Add exception handler
* *
* @param IExceptionHandler $handler * @param IExceptionHandler|string $handler
* @return static $this; * @return static $this;
*/ */
public function addExceptionHandler(IExceptionHandler $handler); public function addExceptionHandler($handler);
/** /**
* Set exception-handlers for group * Set exception-handlers for group
+2 -1
View File
@@ -1,4 +1,5 @@
<?php <?php
namespace Pecee\SimpleRouter\Route; namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request; use Pecee\Http\Request;
@@ -173,7 +174,7 @@ interface IRoute
* @param string $middleware * @param string $middleware
* @return static * @return static
*/ */
public function setMiddleware($middleware); public function addMiddleware($middleware);
/** /**
* Set middlewares array * Set middlewares array
@@ -36,10 +36,12 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$middleware = $this->getMiddlewares()[$i]; $middleware = $this->getMiddlewares()[$i];
if (is_object($middleware) === false) {
$middleware = $this->loadClass($middleware); $middleware = $this->loadClass($middleware);
}
if (($middleware instanceof IMiddleware) === false) { 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); $middleware->handle($request);
+16 -9
View File
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter\Route; namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request; use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
@@ -52,7 +53,7 @@ abstract class Route implements IRoute
protected function loadClass($name) protected function loadClass($name)
{ {
if (class_exists($name) === false) { 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(); return new $name();
@@ -62,14 +63,21 @@ abstract class Route implements IRoute
{ {
$callback = $this->getCallback(); $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 */ /* When the callback is a function */
call_user_func_array($callback, $this->getParameters()); call_user_func_array($callback, $this->getParameters());
} else { return;
/* When the callback is a method */ }
/* When the callback is a class + method */
$controller = explode('@', $callback); $controller = explode('@', $callback);
$namespace = $this->getNamespace(); $namespace = $this->getNamespace();
@@ -80,7 +88,7 @@ abstract class Route implements IRoute
$method = $controller[1]; $method = $controller[1];
if (method_exists($class, $method) === false) { if (method_exists($class, $method) === false) {
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
} }
$parameters = $this->getParameters(); $parameters = $this->getParameters();
@@ -95,7 +103,6 @@ abstract class Route implements IRoute
call_user_func_array([$class, $method], $parameters); call_user_func_array([$class, $method], $parameters);
} }
}
protected function parseParameters($route, $url, $parameterRegex = null) 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 * @return static
*/ */
public function setMiddleware($middleware) public function addMiddleware($middleware)
{ {
$this->middlewares[] = $middleware; $this->middlewares[] = $middleware;
+3 -3
View File
@@ -20,7 +20,7 @@ class RouteGroup extends Route implements IGroupRoute
*/ */
public function matchDomain(Request $request) public function matchDomain(Request $request)
{ {
if (count($this->domains) === 0) { if ($this->domains === null || count($this->domains) === 0) {
return true; return true;
} }
@@ -59,10 +59,10 @@ class RouteGroup extends Route implements IGroupRoute
/** /**
* Add exception handler * Add exception handler
* *
* @param IExceptionHandler $handler * @param IExceptionHandler|string $handler
* @return static $this * @return static $this
*/ */
public function addExceptionHandler(IExceptionHandler $handler) public function addExceptionHandler($handler)
{ {
$this->exceptionHandlers[] = $handler; $this->exceptionHandlers[] = $handler;
+2 -5
View File
@@ -134,8 +134,6 @@ class Router
$group = $route; $group = $route;
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
$this->processingRoute = true; $this->processingRoute = true;
$route->renderRoute($this->request); $route->renderRoute($this->request);
$this->processingRoute = false; $this->processingRoute = false;
@@ -150,7 +148,6 @@ class Router
} }
} }
}
if ($group !== null) { if ($group !== null) {
@@ -289,9 +286,9 @@ class Router
$rewriteUrl = $this->request->getRewriteUrl(); $rewriteUrl = $this->request->getRewriteUrl();
if ($rewriteUrl !== null) { 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 { } 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)); $this->handleException(new NotFoundHttpException($message, 404));
+1 -4
View File
@@ -309,16 +309,13 @@ class SimpleRouter
* @param \Closure $callback * @param \Closure $callback
* @return CallbackExceptionHandler $callbackHandler * @return CallbackExceptionHandler $callbackHandler
*/ */
public static function error($callback) public static function error(\Closure $callback)
{ {
$routes = static::router()->getRoutes(); $routes = static::router()->getRoutes();
$callbackHandler = new CallbackExceptionHandler($callback); $callbackHandler = new CallbackExceptionHandler($callback);
$group = new RouteGroup(); $group = new RouteGroup();
$group->setCallback(function () {
});
$group->addExceptionHandler($callbackHandler); $group->addExceptionHandler($callbackHandler);
array_unshift($routes, $group); array_unshift($routes, $group);
+19 -1
View File
@@ -1,4 +1,5 @@
<?php <?php
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ResponseException.php'; require_once 'Dummy/Exceptions/ResponseException.php';
require_once 'Dummy/Handler/ExceptionHandlerFirst.php'; require_once 'Dummy/Handler/ExceptionHandlerFirst.php';
require_once 'Dummy/Handler/ExceptionHandlerSecond.php'; require_once 'Dummy/Handler/ExceptionHandlerSecond.php';
@@ -43,7 +44,7 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase
try { try {
TestRouter::debug('/my-non-existing-path', 'get'); TestRouter::debug('/my-non-existing-path', 'get');
} catch(\ResponseException $e) { } catch (\ResponseException $e) {
} }
@@ -57,4 +58,21 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase
} }
public function testRewriteExceptionMessage()
{
$this->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');
}
} }
+1 -1
View File
@@ -8,7 +8,7 @@ require_once 'Helpers/TestRouter.php';
class RouterUrlTest extends PHPUnit_Framework_TestCase class RouterUrlTest extends PHPUnit_Framework_TestCase
{ {
public function testSimularUrls() public function testSimilarUrls()
{ {
// Match normal route on alias // Match normal route on alias
TestRouter::resource('/url11', 'DummyController@method1'); TestRouter::resource('/url11', 'DummyController@method1');