Development

- Fixed issue causing default-namespace to add duplicate namespace when using type-hints (issue: #561).
- Fixed phpstan issues.
- Tests: Fixed TestRouter not resetting namespace upon reset.
- Tests: Added NSController (namespace controller) class.
- Tests: added test for class hint + default namespace case.
- Composer: added phpstan support + configuration.
- Composer: updated phpunit version.
This commit is contained in:
Simon Sessingø
2021-06-09 08:54:24 +02:00
parent 4dff4006bf
commit 5cd6cab801
16 changed files with 107 additions and 57 deletions
@@ -17,7 +17,7 @@ class DebugEventHandler implements IEventHandler
public function __construct()
{
$this->callback = static function (EventArgument $argument) {
$this->callback = static function (EventArgument $argument): void {
// todo: log in database
};
}
@@ -2,7 +2,7 @@
namespace Pecee\SimpleRouter\Route;
interface IControllerRoute extends IRoute
interface IControllerRoute extends ILoadableRoute
{
/**
* Get controller class-name
+8 -2
View File
@@ -67,7 +67,7 @@ abstract class Route implements IRoute
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, static function ($var) {
$parameters = array_filter($parameters, static function ($var): bool {
return ($var !== null);
});
}
@@ -82,6 +82,7 @@ abstract class Route implements IRoute
}
/* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters);
}
@@ -280,7 +281,7 @@ abstract class Route implements IRoute
}
/**
* @return string|callable
* @return string|callable|null
*/
public function getCallback()
{
@@ -337,6 +338,11 @@ abstract class Route implements IRoute
*/
public function setNamespace(string $namespace): IRoute
{
// Do not set namespace when class-hinting is used
if (is_array($this->callback) === true) {
return $this;
}
$ns = $this->getNamespace();
if ($ns !== null) {
@@ -52,7 +52,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
if (strpos($name, '.') !== false) {
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, false);
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, true);
if ($found !== false) {
$method = (string)$found;
}
@@ -67,7 +67,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
foreach (Request::$requestTypes as $requestType) {
if (stripos($method, $requestType) === 0) {
$method = (string)substr($method, strlen($requestType));
$method = substr($method, strlen($requestType));
break;
}
}
@@ -54,7 +54,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
/* Remove method/type */
if (strpos($name, '.') !== false) {
$name = (string)substr($name, 0, strrpos($name, '.'));
$name = substr($name, 0, strrpos($name, '.'));
}
return (strtolower($this->name) === strtolower($name));
@@ -68,7 +68,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
*/
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
$url = array_search($name, $this->names, false);
$url = array_search($name, $this->names, true);
if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/';
}
+12 -17
View File
@@ -44,7 +44,7 @@ class Router
/**
* List of processed routes
* @var array
* @var array|ILoadableRoute[]
*/
protected $processedRoutes = [];
@@ -63,7 +63,7 @@ class Router
/**
* Csrf verifier class
* @var BaseCsrfVerifier
* @var BaseCsrfVerifier|null
*/
protected $csrfVerifier;
@@ -107,7 +107,7 @@ class Router
/**
* Class loader instance
* @var ClassLoader
* @var IClassLoader
*/
protected $classLoader;
@@ -215,7 +215,7 @@ class Router
$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()->getOriginalUrl() === '') {
$this->debug('Halted route-processing as no valid route was found');
return;
@@ -575,7 +575,6 @@ class Router
'name' => $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. */
@@ -593,7 +592,7 @@ class Router
}
/* Using @ is most definitely a controller@method or alias@method */
if (is_string($name) === true && strpos($name, '@') !== false) {
if (strpos($name, '@') !== false) {
[$controller, $method] = array_map('strtolower', explode('@', $name));
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
@@ -605,7 +604,7 @@ class Router
/* Check if callback matches (if it's not a function) */
$callback = $route->getCallback();
if (is_string($name) === true && is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
if (is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
/* Check if the entire callback is matching */
if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) {
@@ -656,10 +655,6 @@ class Router
'getParams' => $getParams,
]);
if ($getParams !== null && is_array($getParams) === false) {
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
if ($name === '' && $parameters === '') {
return new Url('/');
}
@@ -703,21 +698,21 @@ class Router
/* Loop through all the routes to see if we can find a match */
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
foreach ($this->processedRoutes as $processedRoute) {
/* Check if the route contains the name/alias */
if ($route->hasName($controller) === true) {
if ($processedRoute->hasName($controller) === true) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
/* Check if the route controller is equal to the name */
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
@@ -842,7 +837,7 @@ class Router
/**
* Get class loader
*
* @return ClassLoader
* @return IClassLoader
*/
public function getClassLoader(): IClassLoader
{
+13 -20
View File
@@ -22,6 +22,7 @@ use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Handlers\CallbackExceptionHandler;
use Pecee\SimpleRouter\Handlers\IEventHandler;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RouteController;
@@ -173,7 +174,7 @@ class SimpleRouter
*/
public static function redirect(string $where, string $to, int $httpCode = 301): IRoute
{
return static::get($where, function () use ($to, $httpCode) {
return static::get($where, static function () use ($to, $httpCode): void {
static::response()->redirect($to, $httpCode);
});
}
@@ -185,7 +186,7 @@ class SimpleRouter
* @param string|array|Closure $callback
* @param array|null $settings
*
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function get(string $url, $callback, array $settings = null): IRoute
{
@@ -198,7 +199,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function post(string $url, $callback, array $settings = null): IRoute
{
@@ -211,7 +212,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function put(string $url, $callback, array $settings = null): IRoute
{
@@ -224,7 +225,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function patch(string $url, $callback, array $settings = null): IRoute
{
@@ -237,7 +238,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function options(string $url, $callback, array $settings = null): IRoute
{
@@ -250,7 +251,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function delete(string $url, $callback, array $settings = null): IRoute
{
@@ -262,15 +263,11 @@ class SimpleRouter
*
* @param array $settings
* @param Closure $callback
* @return RouteGroup
* @return RouteGroup|IGroupRoute
* @throws InvalidArgumentException
*/
public static function group(array $settings, Closure $callback): IGroupRoute
{
if (is_callable($callback) === false) {
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$group = new RouteGroup();
$group->setCallback($callback);
$group->setSettings($settings);
@@ -287,15 +284,11 @@ class SimpleRouter
* @param string $url
* @param Closure $callback
* @param array $settings
* @return RoutePartialGroup
* @return RoutePartialGroup|IPartialGroupRoute
* @throws InvalidArgumentException
*/
public static function partialGroup(string $url, Closure $callback, array $settings = []): IPartialGroupRoute
{
if (is_callable($callback) === false) {
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$settings['prefix'] = $url;
$group = new RoutePartialGroup();
@@ -313,7 +306,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
* @see SimpleRouter::form
*/
public static function basic(string $url, $callback, array $settings = null): IRoute
@@ -328,7 +321,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
* @see SimpleRouter::form
*/
public static function form(string $url, $callback, array $settings = null): IRoute
@@ -499,7 +492,7 @@ class SimpleRouter
/**
* Prepends the default namespace to all new routes added.
*
* @param IRoute $route
* @param ILoadableRoute|IRoute $route
* @return IRoute
*/
public static function addDefaultNamespace(IRoute $route): IRoute