Merge pull request #243 from skipperbent/v3

V3
This commit is contained in:
Simon Sessingø
2017-07-08 12:03:42 +02:00
committed by GitHub
17 changed files with 275 additions and 74 deletions
-1
View File
@@ -1,4 +1,3 @@
.idea .idea
composer.lock composer.lock
vendor/ vendor/
demo-project/vendor
+25 -7
View File
@@ -51,8 +51,9 @@ If you want a great new feature or experience any issues what-so-ever, please fe
- [Middlewares](#middlewares) - [Middlewares](#middlewares)
- [Example](#example) - [Example](#example)
- [ExceptionHandler](#exceptionhandler) - [ExceptionHandlers](#exceptionhandlers)
- [Example](#example-1) - [Handling 404, 403 and other errors](#handling-404-403-and-other-errors)
- [Using custom exception handlers](#using-custom-exception-handlers)
- [Urls](#urls) - [Urls](#urls)
- [Get by name (single route)](#get-by-name-single-route) - [Get by name (single route)](#get-by-name-single-route)
@@ -397,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');
``` ```
@@ -626,13 +627,30 @@ class CustomMiddleware implements Middleware {
--- ---
# ExceptionHandler # ExceptionHandlers
ExceptionHandler are classes that handles all exceptions. ExceptionsHandlers must implement the `IExceptionHandler` interface. ExceptionHandler are classes that handles all exceptions. ExceptionsHandlers must implement the `IExceptionHandler` interface.
## Example ## Handling 404, 403 and other errors
Resource controllers can implement the `IRestController` interface, but is not required. If you simply want to catch a 404 (page not found) etc. you can use the `Router::error($callback)` static helper method.
This will add a callback method which is fired whenever an error occurs on all routes.
The basic example below simply redirect the page to `/not-found` if an `NotFoundHttpException` (404) occurred.
The code should be placed in the file that contains your routes.
```php
Router::get('/not-found', 'PageController@notFound');
Router::error(function(Request $request, \Exception $exception) {
if($exception instanceof NotFoundHttpException && $exception->getCode == 404) {
response()->redirect('/not-found');
}
});
```
## Using custom exception handlers
This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback). This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback).
@@ -1072,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');
+2 -2
View File
@@ -3,7 +3,7 @@ namespace Pecee;
class CsrfToken class CsrfToken
{ {
const CSRF_KEY = 'XSRF-TOKEN'; const CSRF_KEY = 'CSRF-TOKEN';
protected $token; protected $token;
@@ -60,7 +60,7 @@ class CsrfToken
*/ */
public function getToken() public function getToken()
{ {
if ($this->hasToken()) { if ($this->hasToken() === true) {
return $_COOKIE[static::CSRF_KEY]; return $_COOKIE[static::CSRF_KEY];
} }
@@ -0,0 +1,38 @@
<?php
namespace Pecee\Handlers;
use Pecee\Http\Request;
/**
* Class CallbackExceptionHandler
*
* Class is used to create callbacks which are fired when an exception is reached.
* This allows for easy handling 404-exception etc. without creating an custom ExceptionHandler.
*
* @package Pecee\Handlers
*/
class CallbackExceptionHandler implements IExceptionHandler
{
protected $callback;
public function __construct(\Closure $callback)
{
$this->callback = $callback;
}
/**
* @param Request $request
* @param \Exception $error
* @return Request|null
*/
public function handleError(Request $request, \Exception $error)
{
/* Fire exceptions */
return call_user_func($this->callback,
$request,
$error
);
}
}
+1 -1
View File
@@ -108,11 +108,11 @@ class Input
$file = InputFile::createFromArray([ $file = InputFile::createFromArray([
'index' => $key, 'index' => $key,
'filename' => $getItem($key),
'error' => $getItem($key, 'error'), 'error' => $getItem($key, 'error'),
'tmp_name' => $getItem($key, 'tmp_name'), 'tmp_name' => $getItem($key, 'tmp_name'),
'type' => $getItem($key, 'type'), 'type' => $getItem($key, 'type'),
'size' => $getItem($key, 'size'), 'size' => $getItem($key, 'size'),
'filename' => $getItem($key, 'name'),
]); ]);
if (isset($output[$key])) { if (isset($output[$key])) {
+1 -1
View File
@@ -8,7 +8,7 @@ use Pecee\SimpleRouter\SimpleRouter;
class Request class Request
{ {
protected $data = []; private $data = [];
protected $headers; protected $headers;
protected $host; protected $host;
protected $uri; protected $uri;
@@ -1,6 +1,8 @@
<?php <?php
namespace Pecee\SimpleRouter\Route; namespace Pecee\SimpleRouter\Route;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request; use Pecee\Http\Request;
interface IGroupRoute extends IRoute interface IGroupRoute extends IRoute
@@ -13,6 +15,14 @@ interface IGroupRoute extends IRoute
*/ */
public function matchDomain(Request $request); public function matchDomain(Request $request);
/**
* Add exception handler
*
* @param IExceptionHandler|string $handler
* @return static $this;
*/
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);
@@ -98,8 +100,10 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
{ {
$url = $this->getUrl(); $url = $this->getUrl();
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) { $group = $this->getGroup();
$url = '//' . $this->getGroup()->getDomains()[0] . $url;
if ($group !== null && count($group->getDomains()) > 0) {
$url = '//' . $group->getDomains()[0] . $url;
} }
/* Contains parameters that aren't recognized and will be appended at the end of the url */ /* Contains parameters that aren't recognized and will be appended at the end of the url */
+32 -11
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)
{ {
@@ -168,7 +175,7 @@ abstract class Route implements IRoute
*/ */
public function getIdentifier() public function getIdentifier()
{ {
if (strpos($this->callback, '@') !== false) { if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
return $this->callback; return $this->callback;
} }
@@ -265,7 +272,7 @@ abstract class Route implements IRoute
public function getMethod() public function getMethod()
{ {
if (strpos($this->callback, '@') !== false) { if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback); $tmp = explode('@', $this->callback);
return $tmp[1]; return $tmp[1];
@@ -276,7 +283,7 @@ abstract class Route implements IRoute
public function getClass() public function getClass()
{ {
if (strpos($this->callback, '@') !== false) { if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback); $tmp = explode('@', $this->callback);
return $tmp[0]; return $tmp[0];
@@ -478,9 +485,10 @@ abstract class Route implements IRoute
} }
/** /**
* Set middleware class-name * Add middleware class-name
* *
* @param string $middleware * @deprecated This method is deprecated and will be removed in the near future.
* @param IMiddleware|string $middleware
* @return static * @return static
*/ */
public function setMiddleware($middleware) public function setMiddleware($middleware)
@@ -490,6 +498,19 @@ abstract class Route implements IRoute
return $this; return $this;
} }
/**
* Add middleware class-name
*
* @param IMiddleware|string $middleware
* @return static
*/
public function addMiddleware($middleware)
{
$this->middlewares[] = $middleware;
return $this;
}
/** /**
* Set middlewares array * Set middlewares array
* *
@@ -74,8 +74,10 @@ class RouteController extends LoadableRoute implements IControllerRoute
$method .= '/'; $method .= '/';
} }
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) { $group = $this->getGroup();
$url .= '//' . $this->getGroup()->getDomains()[0];
if ($group !== null && count($group->getDomains()) > 0) {
$url .= '//' . $group->getDomains()[0];
} }
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters); $url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
+16 -1
View File
@@ -1,6 +1,8 @@
<?php <?php
namespace Pecee\SimpleRouter\Route; namespace Pecee\SimpleRouter\Route;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request; use Pecee\Http\Request;
class RouteGroup extends Route implements IGroupRoute class RouteGroup extends Route implements IGroupRoute
@@ -18,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;
} }
@@ -54,6 +56,19 @@ class RouteGroup extends Route implements IGroupRoute
return $this->matchDomain($request); return $this->matchDomain($request);
} }
/**
* Add exception handler
*
* @param IExceptionHandler|string $handler
* @return static $this
*/
public function addExceptionHandler($handler)
{
$this->exceptionHandlers[] = $handler;
return $this;
}
/** /**
* Set exception-handlers for group * Set exception-handlers for group
* *
+32 -8
View File
@@ -1,4 +1,5 @@
<?php <?php
namespace Pecee\SimpleRouter; namespace Pecee\SimpleRouter;
use Pecee\Handlers\IExceptionHandler; use Pecee\Handlers\IExceptionHandler;
@@ -133,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;
@@ -143,12 +142,12 @@ class Router
/* Add exception handlers */ /* Add exception handlers */
if (count($route->getExceptionHandlers()) > 0) { if (count($route->getExceptionHandlers()) > 0) {
/** @noinspection AdditionOperationOnArraysInspection */
$exceptionHandlers += $route->getExceptionHandlers(); $exceptionHandlers += $route->getExceptionHandlers();
} }
} }
} }
}
if ($group !== null) { if ($group !== null) {
@@ -183,7 +182,7 @@ class Router
} }
} }
$this->exceptionHandlers = array_unique(array_merge($exceptionHandlers, $this->exceptionHandlers)); $this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
} }
/** /**
@@ -283,7 +282,16 @@ class Router
} }
if ($this->request->getLoadedRoute() === null) { if ($this->request->getLoadedRoute() === null) {
$this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404));
$rewriteUrl = $this->request->getRewriteUrl();
if ($rewriteUrl !== null) {
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri());
} else {
$message = sprintf('Route not found: "%s"', $this->request->getUri());
}
$this->handleException(new NotFoundHttpException($message, 404));
} }
} }
@@ -297,7 +305,10 @@ class Router
for ($i = 0; $i < $max; $i++) { for ($i = 0; $i < $max; $i++) {
$handler = $this->exceptionHandlers[$i]; $handler = $this->exceptionHandlers[$i];
if (is_object($handler) === false) {
$handler = new $handler(); $handler = new $handler();
}
if (($handler instanceof IExceptionHandler) === false) { if (($handler instanceof IExceptionHandler) === false) {
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500); throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
@@ -372,7 +383,7 @@ class Router
} }
/* Using @ is most definitely a controller@method or alias@method */ /* Using @ is most definitely a controller@method or alias@method */
if (strpos($name, '@') !== false) { if (is_string($name) === true && strpos($name, '@') !== false) {
list($controller, $method) = array_map('strtolower', explode('@', $name)); list($controller, $method) = array_map('strtolower', explode('@', $name));
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) { if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
@@ -381,7 +392,7 @@ class Router
} }
/* Check if callback matches (if it's not a function) */ /* Check if callback matches (if it's not a function) */
if (strpos($name, '@') !== false && strpos($route->getCallback(), '@') !== false && !is_callable($route->getCallback())) { if (is_string($name) === true && is_string($route->getCallback()) && strpos($name, '@') !== false && strpos($route->getCallback(), '@') !== false && is_callable($route->getCallback()) === false) {
/* Check if the entire callback is matching */ /* Check if the entire callback is matching */
if (strpos($route->getCallback(), $name) === 0 || strtolower($route->getCallback()) === strtolower($name)) { if (strpos($route->getCallback(), $name) === 0 || strtolower($route->getCallback()) === strtolower($name)) {
@@ -451,7 +462,7 @@ class Router
} }
/* Using @ is most definitely a controller@method or alias@method */ /* Using @ is most definitely a controller@method or alias@method */
if (strpos($name, '@') !== false) { if (is_string($name) === true && strpos($name, '@') !== false) {
list($controller, $method) = explode('@', $name); list($controller, $method) = explode('@', $name);
/* Loop through all the routes to see if we can find a match */ /* Loop through all the routes to see if we can find a match */
@@ -517,6 +528,19 @@ class Router
return $this->routes; return $this->routes;
} }
/**
* Set routes
*
* @param array $routes
* @return static $this
*/
public function setRoutes(array $routes)
{
$this->routes = $routes;
return $this;
}
/** /**
* Get current request * Get current request
* *
+25 -1
View File
@@ -7,8 +7,10 @@
* This class is added so calls can be made statically like Router::get() making the code look pretty. * This class is added so calls can be made statically like Router::get() making the code look pretty.
* It also adds some extra functionality like default-namespace. * It also adds some extra functionality like default-namespace.
*/ */
namespace Pecee\SimpleRouter; namespace Pecee\SimpleRouter;
use Pecee\Handlers\CallbackExceptionHandler;
use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Response; use Pecee\Http\Response;
use Pecee\SimpleRouter\Exceptions\HttpException; use Pecee\SimpleRouter\Exceptions\HttpException;
@@ -301,6 +303,28 @@ class SimpleRouter
return $route; return $route;
} }
/**
* Add exception callback handler.
*
* @param \Closure $callback
* @return CallbackExceptionHandler $callbackHandler
*/
public static function error(\Closure $callback)
{
$routes = static::router()->getRoutes();
$callbackHandler = new CallbackExceptionHandler($callback);
$group = new RouteGroup();
$group->addExceptionHandler($callbackHandler);
array_unshift($routes, $group);
static::router()->setRoutes($routes);
return $callbackHandler;
}
/** /**
* Get url for a route by using either name/alias, class or method name. * Get url for a route by using either name/alias, class or method name.
* *
@@ -355,7 +379,7 @@ class SimpleRouter
*/ */
public static function router() public static function router()
{ {
if(static::$router === null) { if (static::$router === null) {
static::$router = new Router(); static::$router = new Router();
} }
@@ -0,0 +1,27 @@
<?php
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ExceptionHandlerException.php';
require_once 'Helpers/TestRouter.php';
class RouterCallbackExceptionHandlerTest extends PHPUnit_Framework_TestCase
{
public function testCallbackExceptionHandler()
{
$this->setExpectedException(ExceptionHandlerException::class);
// Match normal route on alias
TestRouter::get('/my-new-url', 'DummyController@method2');
TestRouter::get('/my-url', 'DummyController@method1');
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $exception) {
throw new ExceptionHandlerException();
});
TestRouter::debugNoReset('/404-url', 'get');
TestRouter::router()->reset();
}
}
+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');