Development

- Better php7 support.
- Added easier way to debug router.
- Improvements and bugfixes.
- Updated documentation.
This commit is contained in:
Simon Sessingø
2018-03-26 23:43:27 +02:00
parent f23d569757
commit 085f98cf08
28 changed files with 847 additions and 233 deletions
@@ -1,4 +1,5 @@
<?php
namespace Pecee\SimpleRouter\Exceptions;
class HttpException extends \Exception
@@ -1,4 +1,5 @@
<?php
namespace Pecee\SimpleRouter\Exceptions;
class NotFoundHttpException extends HttpException
@@ -17,6 +17,6 @@ interface IControllerRoute extends IRoute
* @param string $controller
* @return static
*/
public function setController($controller): self;
public function setController(string $controller): self;
}
+3 -3
View File
@@ -19,15 +19,15 @@ interface IGroupRoute extends IRoute
* Add exception handler
*
* @param IExceptionHandler|string $handler
* @return static $this;
* @return static
*/
public function addExceptionHandler($handler);
public function addExceptionHandler($handler): self;
/**
* Set exception-handlers for group
*
* @param array $handlers
* @return static $this
* @return static
*/
public function setExceptionHandlers(array $handlers);
@@ -3,6 +3,7 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Router;
interface ILoadableRoute extends IRoute
{
@@ -18,22 +19,32 @@ interface ILoadableRoute extends IRoute
public function findUrl($method = null, $parameters = null, $name = null): string;
/**
* Loads and renders middlewares-classes
* Loads and renders middleware-classes
*
* @param Request $request
* @param Router $router
*/
public function loadMiddleware(Request $request);
public function loadMiddleware(Request $request, Router $router): void;
public function getUrl();
/**
* Get url
* @return string
*/
public function getUrl(): string;
public function setUrl($url);
/**
* Set url
* @param string $url
* @return static
*/
public function setUrl(string $url): self;
/**
* Returns the provided name for the router.
*
* @return string|null
*/
public function getName(): string;
public function getName(): ?string;
/**
* Check if route has given name.
@@ -41,7 +52,7 @@ interface ILoadableRoute extends IRoute
* @param string $name
* @return bool
*/
public function hasName($name): bool;
public function hasName(string $name): bool;
/**
* Sets the router name, which makes it easier to obtain the url or router at a later point.
@@ -49,14 +60,14 @@ interface ILoadableRoute extends IRoute
* @param string $name
* @return static
*/
public function setName($name): self;
public function setName(string $name): self;
/**
* Get regular expression match used for matching route (if defined).
*
* @return string
*/
public function getMatch(): string;
public function getMatch(): ?string;
/**
* Add regular expression match for the entire route.
+22 -11
View File
@@ -3,6 +3,7 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Router;
interface IRoute
{
@@ -20,10 +21,11 @@ interface IRoute
* Returns class to be rendered.
*
* @param Request $request
* @param Router $router
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
* @return string
*/
public function renderRoute(Request $request): ?string;
public function renderRoute(Request $request, Router $router): ?string;
/**
* Returns callback name/identifier for the current route based on the callback.
@@ -103,15 +105,20 @@ interface IRoute
* @param string $method
* @return static
*/
public function setMethod($method): self;
public function setMethod(string $method): self;
public function getClass();
/**
* Get class
*
* @return string|null
*/
public function getClass(): ?string;
/**
* @param string $namespace
* @return static
*/
public function setNamespace($namespace);
public function setNamespace(string $namespace): self;
/**
* @return string|null
@@ -122,9 +129,13 @@ interface IRoute
* @param string $namespace
* @return static
*/
public function setDefaultNamespace($namespace);
public function setDefaultNamespace($namespace): IRoute;
public function getDefaultNamespace();
/**
* Get default namespace
* @return string|null
*/
public function getDefaultNamespace(): ?string;
/**
* Get parameter names.
@@ -139,7 +150,7 @@ interface IRoute
* @param array $options
* @return static
*/
public function setWhere(array $options);
public function setWhere(array $options): self;
/**
* Get parameters
@@ -152,18 +163,18 @@ interface IRoute
* Get parameters
*
* @param array $parameters
* @return static $this
* @return static
*/
public function setParameters(array $parameters);
public function setParameters(array $parameters): self;
/**
* Merge with information from another route.
*
* @param array $settings
* @param bool $merge
* @return static $this
* @return static
*/
public function setSettings(array $settings, $merge = false);
public function setSettings(array $settings, bool $merge = false): self;
/**
* Export route settings to array so they can be merged with another route.
+20 -12
View File
@@ -5,6 +5,7 @@ namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Router;
abstract class LoadableRoute extends Route implements ILoadableRoute
{
@@ -24,10 +25,13 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Loads and renders middlewares-classes
*
* @param Request $request
* @param Router $router
* @throws HttpException
*/
public function loadMiddleware(Request $request) : void
public function loadMiddleware(Request $request, Router $router): void
{
$router->debug('Loading middlewares');
foreach ($this->getMiddlewares() as $middleware) {
if (\is_object($middleware) === false) {
@@ -38,11 +42,15 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
throw new HttpException($middleware . ' must be inherit the IMiddleware interface');
}
$router->debug('Loading middleware "%s"', \get_class($middleware));
$middleware->handle($request);
$router->debug('Finished loading middleware');
}
$router->debug('Finished loading middlewares');
}
public function matchRegex(Request $request, $url) : ?bool
public function matchRegex(Request $request, $url): ?bool
{
/* Match on custom defined regular expression */
@@ -59,7 +67,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string $url
* @return static
*/
public function setUrl($url) : self
public function setUrl(string $url): ILoadableRoute
{
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
@@ -75,7 +83,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return $this;
}
public function getUrl() : string
public function getUrl(): string
{
return $this->url;
}
@@ -89,7 +97,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string|null $name
* @return string
*/
public function findUrl($method = null, $parameters = null, $name = null) : string
public function findUrl($method = null, $parameters = null, $name = null): string
{
$url = $this->getUrl();
@@ -144,7 +152,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*
* @return string
*/
public function getName() : string
public function getName(): string
{
return $this->name;
}
@@ -155,7 +163,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string $name
* @return bool
*/
public function hasName($name) : bool
public function hasName(string $name): bool
{
return strtolower($this->name) === strtolower($name);
}
@@ -166,7 +174,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string $regex
* @return static
*/
public function setMatch($regex) : ILoadableRoute
public function setMatch($regex): ILoadableRoute
{
$this->regex = $regex;
@@ -178,7 +186,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*
* @return string
*/
public function getMatch() : string
public function getMatch(): string
{
return $this->regex;
}
@@ -191,7 +199,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string|array $name
* @return static
*/
public function name($name) : ILoadableRoute
public function name($name): ILoadableRoute
{
return $this->setName($name);
}
@@ -202,7 +210,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param string $name
* @return static
*/
public function setName($name) : ILoadableRoute
public function setName(string $name): ILoadableRoute
{
$this->name = $name;
@@ -216,7 +224,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* @param bool $merge
* @return static
*/
public function setSettings(array $values, $merge = false): IRoute
public function setSettings(array $values, bool $merge = false): IRoute
{
if (isset($values['as']) === true) {
+29 -18
View File
@@ -5,6 +5,7 @@ namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Router;
abstract class Route implements IRoute
{
@@ -59,10 +60,10 @@ abstract class Route implements IRoute
/**
* Load class by name
* @param string $name
* @return mixed
* @return object
* @throws NotFoundHttpException
*/
protected function loadClass($name)
protected function loadClass($name): object
{
if (class_exists($name) === false) {
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $name), 404);
@@ -75,21 +76,25 @@ abstract class Route implements IRoute
* Render route
*
* @param Request $request
* @param Router $router
* @return string|null
* @throws NotFoundHttpException
*/
public function renderRoute(Request $request): ?string
public function renderRoute(Request $request, Router $router): ?string
{
$router->debug('Starting rendering route');
$callback = $this->getCallback();
if ($callback === null) {
return null;
}
$router->debug('Parsing parameters');
$parameters = $this->getParameters();
$router->debug('Finished parsing parameters');
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
@@ -98,7 +103,10 @@ abstract class Route implements IRoute
/* Render callback function */
if (\is_callable($callback) === true) {
$router->debug('Executing callback');
/* When the callback is a function */
return \call_user_func_array($callback, $parameters);
}
@@ -109,6 +117,7 @@ abstract class Route implements IRoute
$className = ($namespace !== null && $controller[0][0] !== '\\') ? $namespace . '\\' . $controller[0] : $controller[0];
$router->debug('Loading class %s', $className);
$class = $this->loadClass($className);
$method = $controller[1];
@@ -116,6 +125,8 @@ abstract class Route implements IRoute
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
}
$router->debug('Executing callback');
return \call_user_func_array([$class, $method], $parameters);
}
@@ -300,7 +311,7 @@ abstract class Route implements IRoute
return null;
}
public function getClass()
public function getClass(): ?string
{
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
@@ -311,14 +322,14 @@ abstract class Route implements IRoute
return null;
}
public function setMethod($method): IRoute
public function setMethod(string $method): IRoute
{
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
return $this;
}
public function setClass($class)
public function setClass(string $class): IRoute
{
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
@@ -327,9 +338,9 @@ abstract class Route implements IRoute
/**
* @param string $namespace
* @return static $this
* @return static
*/
public function setNamespace($namespace)
public function setNamespace(string $namespace): IRoute
{
$this->namespace = $namespace;
@@ -338,16 +349,16 @@ abstract class Route implements IRoute
/**
* @param string $namespace
* @return static $this
* @return static
*/
public function setDefaultNamespace($namespace)
public function setDefaultNamespace($namespace): IRoute
{
$this->defaultNamespace = $namespace;
return $this;
}
public function getDefaultNamespace()
public function getDefaultNamespace(): ?string
{
return $this->defaultNamespace;
}
@@ -397,9 +408,9 @@ abstract class Route implements IRoute
*
* @param array $values
* @param bool $merge
* @return static $this
* @return static
*/
public function setSettings(array $values, $merge = false)
public function setSettings(array $values, bool $merge = false): IRoute
{
if ($this->namespace === null && isset($values['namespace']) === true) {
$this->setNamespace($values['namespace']);
@@ -445,7 +456,7 @@ abstract class Route implements IRoute
* @param array $options
* @return static
*/
public function setWhere(array $options)
public function setWhere(array $options): IRoute
{
$this->where = $options;
@@ -486,9 +497,9 @@ abstract class Route implements IRoute
* Get parameters
*
* @param array $parameters
* @return static $this
* @return static
*/
public function setParameters(array $parameters)
public function setParameters(array $parameters): IRoute
{
/*
* If this is the first time setting parameters we store them so we
@@ -556,7 +567,7 @@ abstract class Route implements IRoute
* This is used when no custom parameter regex is found.
*
* @param string $regex
* @return static $this
* @return static
*/
public function setDefaultParameterRegex($regex)
{
@@ -24,7 +24,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
* @param string $name
* @return bool
*/
public function hasName($name): bool
public function hasName(string $name): bool
{
if ($this->name === null) {
return false;
@@ -134,7 +134,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
* @param string $controller
* @return static
*/
public function setController($controller): IControllerRoute
public function setController(string $controller): IControllerRoute
{
$this->controller = $controller;
@@ -157,7 +157,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
* @param string $method
* @return static
*/
public function setMethod($method): IRoute
public function setMethod(string $method): IRoute
{
$this->method = $method;
@@ -171,7 +171,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
* @param bool $merge
* @return static
*/
public function setSettings(array $values, $merge = false): IRoute
public function setSettings(array $values, bool $merge = false): IRoute
{
if (isset($values['names']) === true) {
$this->names = $values['names'];
+1 -1
View File
@@ -147,7 +147,7 @@ class RouteGroup extends Route implements IGroupRoute
* @param bool $merge
* @return static
*/
public function setSettings(array $values, $merge = false): IRoute
public function setSettings(array $values, bool $merge = false): IRoute
{
if (isset($values['prefix']) === true) {
@@ -42,7 +42,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
* @param string $name
* @return bool
*/
public function hasName($name): bool
public function hasName(string $name): bool
{
if ($this->name === null) {
return false;
@@ -154,14 +154,14 @@ class RouteResource extends LoadableRoute implements IControllerRoute
* @param string $controller
* @return static
*/
public function setController($controller): IControllerRoute
public function setController(string $controller): IControllerRoute
{
$this->controller = $controller;
return $this;
}
public function setName($name): ILoadableRoute
public function setName(string $name): ILoadableRoute
{
$this->name = $name;
@@ -208,7 +208,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
* @param bool $merge
* @return static
*/
public function setSettings(array $values, $merge = false): IRoute
public function setSettings(array $values, bool $merge = false): IRoute
{
if (isset($values['names']) === true) {
$this->names = $values['names'];
+185 -62
View File
@@ -66,6 +66,32 @@ class Router
*/
protected $exceptionHandlers;
/**
* List of loaded exception that has been loaded.
* Used to ensure that exception-handlers aren't loaded twice when rewriting route.
*
* @var array
*/
protected $loadedExceptionHandlers;
/**
* Enable or disabled debugging
* @var bool
*/
protected $debugEnabled = false;
/**
* The start time used when debugging is enabled
* @var float
*/
protected $debugStartTime;
/**
* List containing all debug messages
* @var array
*/
protected $debugList = [];
/**
* Router constructor.
* @throws \Pecee\Http\Exceptions\MalformedUrlException
@@ -78,7 +104,7 @@ class Router
/**
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public function reset() : void
public function reset(): void
{
$this->processingRoute = false;
$this->request = new Request();
@@ -87,6 +113,7 @@ class Router
$this->routeStack = [];
$this->processedRoutes = [];
$this->exceptionHandlers = [];
$this->loadedExceptionHandlers = [];
}
/**
@@ -94,7 +121,7 @@ class Router
* @param IRoute $route
* @return IRoute
*/
public function addRoute(IRoute $route) : IRoute
public function addRoute(IRoute $route): IRoute
{
/*
* If a route is currently being processed, that means that the route being added are rendered from the parent
@@ -102,10 +129,12 @@ class Router
*/
if ($this->processingRoute === true) {
$this->routeStack[] = $route;
return $route;
}
$this->routes[] = $route;
return $route;
}
@@ -115,10 +144,11 @@ class Router
* @param IRoute $route
* @throws NotFoundHttpException
*/
protected function renderAndProcess(IRoute $route) : void {
protected function renderAndProcess(IRoute $route): void
{
$this->processingRoute = true;
$route->renderRoute($this->request);
$route->renderRoute($this->request, $this);
$this->processingRoute = false;
if (\count($this->routeStack) !== 0) {
@@ -139,13 +169,17 @@ class Router
* @param IGroupRoute|null $group
* @throws NotFoundHttpException
*/
protected function processRoutes(array $routes, IGroupRoute $group = null) : void
protected function processRoutes(array $routes, ?IGroupRoute $group = null): void
{
$this->debug('Processing routes');
// Loop through each route-request
$exceptionHandlers = [];
// Stop processing routes if no valid route is found.
if($this->request->getRewriteRoute() === null && $this->request->getUrl() === null) {
if ($this->request->getRewriteRoute() === null && $this->request->getUrl() === null) {
$this->debug('Halted route-processing as no valid route was found');
return;
}
@@ -154,6 +188,8 @@ class Router
/* @var $route IRoute */
foreach ($routes as $route) {
$this->debug('Processing route "%s"', \get_class($route));
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
@@ -199,16 +235,22 @@ class Router
* @throws NotFoundHttpException
* @return void
*/
public function loadRoutes() : void
public function loadRoutes(): void
{
$this->debug('Loading routes');
/* Initialize boot-managers */
/* @var $manager IRouterBootManager */
foreach ($this->bootManagers as $manager) {
$this->debug('Rendering bootmanager %s', \get_class($manager));
$manager->boot($this->request);
$this->debug('Finished rendering bootmanager');
}
/* Loop through each route-request */
$this->processRoutes($this->routes);
$this->debug('Finished loading routes');
}
/**
@@ -219,9 +261,11 @@ class Router
* @throws HttpException
* @throws \Exception
*/
public function routeRequest($rewrite = false) : ?string
public function routeRequest(bool $rewrite = false): ?string
{
$routeNotAllowed = false;
$this->debug('Started routing request (rewrite: %s)', $rewrite === true ? 'yes' : 'no');
$methodNotAllowed = false;
try {
@@ -233,8 +277,6 @@ class Router
/* Verify csrf token for request */
$this->csrfVerifier->handle($this->request);
}
} else {
$this->request->setHasRewrite(false);
}
$url = $this->request->getRewriteUrl() ?? $this->request->getUrl()->getPath();
@@ -242,38 +284,39 @@ class Router
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $key => $route) {
$this->debug('Matching route "%s"', \get_class($route));
/* If the route matches */
if ($route->matchRoute($url, $this->request) === true) {
/* Check if request method matches */
if (\count($route->getRequestMethods()) !== 0 && \in_array($this->request->getMethod(), $route->getRequestMethods(), true) === false) {
$routeNotAllowed = true;
$this->debug('Method "%s" not allowed', $this->request->getMethod());
$methodNotAllowed = true;
continue;
}
$route->loadMiddleware($this->request);
$route->loadMiddleware($this->request, $this);
if ($this->hasRewrite($url) === true) {
unset($this->processedRoutes[$key]);
return $this->routeRequest(true);
$output = $this->handleRouteRewrite($key, $url);
if ($output !== null) {
return $output;
}
/* Render route */
$routeNotAllowed = false;
$methodNotAllowed = false;
$this->request->addLoadedRoute($route);
$output = $route->renderRoute($this->request);
$output = $route->renderRoute($this->request, $this);
if ($output !== null) {
return $output;
}
if ($this->hasRewrite($url) === true) {
unset($this->processedRoutes[$key]);
return $this->routeRequest(true);
$output = $this->handleRouteRewrite($key, $url);
if ($output !== null) {
return $output;
}
}
}
@@ -282,7 +325,7 @@ class Router
$this->handleException($e);
}
if ($routeNotAllowed === true) {
if ($methodNotAllowed === true) {
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
$this->handleException(new HttpException($message, 403));
}
@@ -297,35 +340,45 @@ class Router
$message = sprintf('Route not found: "%s"', $this->request->getUrl()->getPath());
}
$this->handleException(new NotFoundHttpException($message, 404));
$this->debug($message);
return $this->handleException(new NotFoundHttpException($message, 404));
}
return null;
}
protected function hasRewrite($url) : bool
/**
* Handle route-rewrite
*
* @param string $key
* @param string $url
* @return string|null
* @throws HttpException
* @throws \Exception
*/
protected function handleRouteRewrite($key, string $url): ?string
{
/* If the request has changed */
if ($this->request->hasRewrite() === true) {
if ($this->request->getRewriteRoute() !== null) {
/* Render rewrite-route */
$this->processedRoutes[] = $this->request->getRewriteRoute();
return true;
}
if ($this->request->isRewrite($url) === false) {
/* Render rewrite-url */
$this->processedRoutes = array_values($this->processedRoutes);
return true;
}
if ($this->request->hasRewrite() === false) {
return null;
}
return false;
$route = $this->request->getRewriteRoute();
if ($route !== null) {
/* Add rewrite route */
$this->processedRoutes[] = $route;
}
if ($this->request->getRewriteUrl() !== $url) {
unset($this->processedRoutes[$key]);
$this->request->setHasRewrite(false);
return $this->routeRequest(true);
}
return null;
}
/**
@@ -334,8 +387,10 @@ class Router
* @throws \Exception
* @return string|null
*/
protected function handleException(\Exception $e) : ?string
protected function handleException(\Exception $e): ?string
{
$this->debug('Starting exception handling for "%s"', \get_class($e));
/* @var $handler IExceptionHandler */
foreach ($this->exceptionHandlers as $key => $handler) {
@@ -343,17 +398,22 @@ class Router
$handler = new $handler();
}
$this->debug('Processing exception-handler "%s"', \get_class($handler));
if (($handler instanceof IExceptionHandler) === false) {
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
}
try {
$this->debug('Start rendering exception handler');
$handler->handleError($this->request, $e);
$this->debug('Finished rendering exception-handler');
if ($this->request->hasRewrite() === true) {
unset($this->exceptionHandlers[$key]);
$this->exceptionHandlers = array_values($this->exceptionHandlers);
if (isset($this->loadedExceptionHandlers[$key]) === false && $this->request->hasRewrite() === true) {
$this->loadedExceptionHandlers[$key] = $handler;
$this->debug('Exception handler contains rewrite, reloading routes');
return $this->routeRequest(true);
}
@@ -361,12 +421,15 @@ class Router
} catch (\Exception $e) {
}
$this->debug('Finished processing');
}
$this->debug('Finished exception handling - exception not handled, throwing');
throw $e;
}
public function arrayToParams(array $getParams = [], $includeEmpty = true) : string
public function arrayToParams(array $getParams = [], bool $includeEmpty = true): string
{
if (\count($getParams) !== 0) {
@@ -388,18 +451,25 @@ class Router
* @param string $name
* @return ILoadableRoute|null
*/
public function findRoute($name) : ?ILoadableRoute
public function findRoute(string $name): ?ILoadableRoute
{
$this->debug('Finding route by name "%s"', $name);
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
if ($route->hasName($name) === true) {
$this->debug('Found route "%s" by name "%s"', $route->getUrl(), $name);
return $route;
}
/* Direct match to controller */
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($name)) {
$this->debug('Found route "%s" by controller "%s"', $route->getUrl(), $name);
return $route;
}
@@ -408,6 +478,8 @@ class Router
[$controller, $method] = array_map('strtolower', explode('@', $name));
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
$this->debug('Found route "%s" by controller "%s" and method "%s"', $route->getUrl(), $controller, $method);
return $route;
}
}
@@ -417,16 +489,22 @@ class Router
/* Check if the entire callback is matching */
if (strpos($route->getCallback(), $name) === 0 || strtolower($route->getCallback()) === strtolower($name)) {
$this->debug('Found route "%s" by callback "%s"', $route->getUrl(), $name);
return $route;
}
/* Check if the class part of the callback matches (class@method) */
if (strtolower($name) === strtolower($route->getClass())) {
$this->debug('Found route "%s" by class "%s"', $route->getUrl(), $name);
return $route;
}
}
}
$this->debug('Route not found');
return null;
}
@@ -448,8 +526,10 @@ class Router
* @throws InvalidArgumentException
* @return string
*/
public function getUrl($name = null, $parameters = null, $getParams = null) : string
public function getUrl(?string $name = null, $parameters = null, $getParams = null): string
{
$this->debug('Finding url', \func_get_args());
if ($getParams !== null && \is_array($getParams) === false) {
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
@@ -513,28 +593,28 @@ class Router
}
/**
* Get bootmanagers
* Get BootManagers
* @return array
*/
public function getBootManagers() : array
public function getBootManagers(): array
{
return $this->bootManagers;
}
/**
* Set bootmanagers
* Set BootManagers
* @param array $bootManagers
*/
public function setBootManagers(array $bootManagers) : void
public function setBootManagers(array $bootManagers): void
{
$this->bootManagers = $bootManagers;
}
/**
* Add bootmanager
* Add BootManager
* @param IRouterBootManager $bootManager
*/
public function addBootManager(IRouterBootManager $bootManager) : void
public function addBootManager(IRouterBootManager $bootManager): void
{
$this->bootManagers[] = $bootManager;
}
@@ -544,7 +624,7 @@ class Router
*
* @return array
*/
public function getProcessedRoutes() : array
public function getProcessedRoutes(): array
{
return $this->processedRoutes;
}
@@ -552,7 +632,7 @@ class Router
/**
* @return array
*/
public function getRoutes() : array
public function getRoutes(): array
{
return $this->routes;
}
@@ -563,7 +643,7 @@ class Router
* @param array $routes
* @return static
*/
public function setRoutes(array $routes) : self
public function setRoutes(array $routes): self
{
$this->routes = $routes;
@@ -575,7 +655,7 @@ class Router
*
* @return Request
*/
public function getRequest() : Request
public function getRequest(): Request
{
return $this->request;
}
@@ -584,7 +664,7 @@ class Router
* Get csrf verifier class
* @return BaseCsrfVerifier
*/
public function getCsrfVerifier() : BaseCsrfVerifier
public function getCsrfVerifier(): ?BaseCsrfVerifier
{
return $this->csrfVerifier;
}
@@ -602,4 +682,47 @@ class Router
return $this;
}
/**
* Add new debug message
* @param string $message
* @param array $args
*/
public function debug(string $message, ...$args): void
{
if ($this->debugEnabled === false) {
return;
}
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$this->debugList[] = [
'message' => vsprintf($message, $args),
'time' => number_format(microtime(true) - $this->debugStartTime, 10),
'trace' => end($trace),
];
}
/**
* Enable or disables debugging
*
* @param bool $boolean
*/
public function setDebugEnabled(bool $boolean): void
{
if ($boolean === true) {
$this->debugStartTime = microtime(true);
}
$this->debugEnabled = $boolean;
}
/**
* Get the list containing all debug messages.
*
* @return array
*/
public function getDebugLog(): array
{
return $this->debugList;
}
}
+45 -9
View File
@@ -46,6 +46,8 @@ class SimpleRouter
protected static $router;
/**
* Start routing
*
* @throws \Pecee\Http\Exceptions\MalformedUrlException
* @throws HttpException
* @throws \Exception
@@ -56,21 +58,55 @@ class SimpleRouter
}
/**
* Get debug info array
* Start the routing an return array with debugging-information
*
* @return array
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function debugInfo(): array
public static function startDebug(): array
{
$routerOutput = null;
try {
ob_start();
static::router()->setDebugEnabled(true);
static::start();
$routerOutput = ob_get_contents();
ob_end_clean();
} catch (\Exception $e) {
}
// Try to parse library version
$composerFile = \dirname(__DIR__, 3) . '/composer.lock';
$version = false;
if (is_file($composerFile) === true) {
$composerInfo = json_decode(file_get_contents($composerFile), true);
if (isset($composerInfo['packages']) === true && \is_array($composerInfo['packages']) === true) {
foreach ($composerInfo['packages'] as $package) {
if (isset($package['name']) === true && strtolower($package['name']) === 'pecee/simple-router') {
$version = $package['version'];
break;
}
}
}
}
return [
'url' => static::request()->getUrl(),
'method' => static::request()->getMethod(),
'host' => static::request()->getHost(),
'loaded_routes' => static::request()->getLoadedRoutes(),
'all_routes' => static::router()->getRoutes(),
'boot_managers' => static::router()->getBootManagers(),
'csrf_verifier' => static::router()->getCsrfVerifier(),
'url' => static::request()->getUrl(),
'method' => static::request()->getMethod(),
'host' => static::request()->getHost(),
'loaded_routes' => static::request()->getLoadedRoutes(),
'all_routes' => static::router()->getRoutes(),
'boot_managers' => static::router()->getBootManagers(),
'csrf_verifier' => static::router()->getCsrfVerifier(),
'log' => static::router()->getDebugLog(),
'router_output' => $routerOutput,
'library_version' => $version,
'php_version' => PHP_VERSION,
'server_params' => static::request()->getHeaders(),
];
}