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
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');
@@ -17,7 +17,7 @@ class CallbackExceptionHandler implements IExceptionHandler
protected $callback;
public function __construct($callback)
public function __construct(\Closure $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
*
* @param IExceptionHandler $handler
* @param IExceptionHandler|string $handler
* @return static $this;
*/
public function addExceptionHandler(IExceptionHandler $handler);
public function addExceptionHandler($handler);
/**
* Set exception-handlers for group
+2 -1
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
@@ -173,7 +174,7 @@ interface IRoute
* @param string $middleware
* @return static
*/
public function setMiddleware($middleware);
public function addMiddleware($middleware);
/**
* Set middlewares array
@@ -36,10 +36,12 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$middleware = $this->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);
+38 -31
View File
@@ -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;
+3 -3
View File
@@ -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;
+11 -14
View File
@@ -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));
+1 -4
View File
@@ -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);
+19 -1
View File
@@ -1,4 +1,5 @@
<?php
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ResponseException.php';
require_once 'Dummy/Handler/ExceptionHandlerFirst.php';
require_once 'Dummy/Handler/ExceptionHandlerSecond.php';
@@ -43,7 +44,7 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase
try {
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
{
public function testSimularUrls()
public function testSimilarUrls()
{
// Match normal route on alias
TestRouter::resource('/url11', 'DummyController@method1');