Merge pull request #563 from skipperbent/v4-development

Version 4.3.6.0
This commit is contained in:
Simon Sessingø
2021-06-09 09:19:14 +02:00
committed by GitHub
16 changed files with 109 additions and 56 deletions
+6 -2
View File
@@ -32,7 +32,11 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7", "phpunit/phpunit": "^7",
"mockery/mockery": "^1" "mockery/mockery": "^1",
"phpstan/phpstan": "^0",
"phpstan/phpstan-phpunit": "^0",
"phpstan/phpstan-deprecation-rules": "^0",
"phpstan/phpstan-strict-rules": "^0"
}, },
"scripts": { "scripts": {
"test": [ "test": [
@@ -44,4 +48,4 @@
"Pecee\\": "src/Pecee/" "Pecee\\": "src/Pecee/"
} }
} }
} }
+22
View File
@@ -0,0 +1,22 @@
parameters:
level: 6
paths:
- src
fileExtensions:
- php
bootstrapFiles:
- ./vendor/autoload.php
ignoreErrors:
reportUnmatchedIgnoredErrors: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
parallel:
processTimeout: 300.0
jobSize: 10
maximumNumberOfProcesses: 4
minimumNumberOfJobsPerProcess: 4
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
+6
View File
@@ -13,8 +13,14 @@ interface IInputItem
public function setName(string $name): self; public function setName(string $name): self;
/**
* @return mixed
*/
public function getValue(); public function getValue();
/**
* @param mixed $value
*/
public function setValue($value): self; public function setValue($value): self;
public function __toString(): string; public function __toString(): string;
+2 -2
View File
@@ -115,7 +115,7 @@ class InputHandler
// Handle array input // Handle array input
if (is_array($value['name']) === false) { if (is_array($value['name']) === false) {
$values['index'] = $parentKey ?? $key; $values = ['index' => $parentKey ?? $key];
try { try {
$list[$key] = InputFile::createFromArray($values + $value); $list[$key] = InputFile::createFromArray($values + $value);
@@ -161,7 +161,7 @@ class InputHandler
try { try {
$file = InputFile::createFromArray([ $file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key, 'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key], 'name' => $original['name'][$key],
'error' => $original['error'][$key], 'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key], 'tmp_name' => $original['tmp_name'][$key],
+1 -1
View File
@@ -137,7 +137,7 @@ class Request
public function isSecure(): bool public function isSecure(): bool
{ {
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443; return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || (int)$this->getHeader('server-port') === 443;
} }
/** /**
+4 -4
View File
@@ -387,7 +387,7 @@ class Url implements JsonSerializable
{ {
$encodedUrl = preg_replace_callback( $encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u', '/[^:\/@?&=#]+/u',
static function ($matches) { static function ($matches): string {
return urlencode($matches[0]); return urlencode($matches[0]);
}, },
$url $url
@@ -414,7 +414,7 @@ class Url implements JsonSerializable
if (count($getParams) !== 0) { if (count($getParams) !== 0) {
if ($includeEmpty === false) { if ($includeEmpty === false) {
$getParams = array_filter($getParams, static function ($item) { $getParams = array_filter($getParams, static function ($item): bool {
return (trim($item) !== ''); return (trim($item) !== '');
}); });
} }
@@ -458,7 +458,7 @@ class Url implements JsonSerializable
$port = $this->port !== null ? ':' . $this->port : ''; $port = $this->port !== null ? ':' . $this->port : '';
$user = $this->username ?? ''; $user = $this->username ?? '';
$pass = $this->password !== null ? ':' . $this->password : ''; $pass = $this->password !== null ? ':' . $this->password : '';
$pass = ($user || $pass) ? $pass . '@' : ''; $pass = ($user !== '' || $pass !== '') ? $pass . '@' : '';
return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams); return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams);
} }
@@ -466,7 +466,7 @@ class Url implements JsonSerializable
/** /**
* Specify data which should be serialized to JSON * Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>, * @return string data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource. * which is a value of any type other than a resource.
* @since 5.4.0 * @since 5.4.0
*/ */
@@ -17,7 +17,7 @@ class DebugEventHandler implements IEventHandler
public function __construct() public function __construct()
{ {
$this->callback = static function (EventArgument $argument) { $this->callback = static function (EventArgument $argument): void {
// todo: log in database // todo: log in database
}; };
} }
@@ -2,7 +2,7 @@
namespace Pecee\SimpleRouter\Route; namespace Pecee\SimpleRouter\Route;
interface IControllerRoute extends IRoute interface IControllerRoute extends ILoadableRoute
{ {
/** /**
* Get controller class-name * Get controller class-name
+11 -2
View File
@@ -30,6 +30,9 @@ abstract class Route implements IRoute
protected $urlRegex = '/^%s\/?$/u'; protected $urlRegex = '/^%s\/?$/u';
protected $group; protected $group;
protected $parent; protected $parent;
/**
* @var string|callable|null
*/
protected $callback; protected $callback;
protected $defaultNamespace; protected $defaultNamespace;
@@ -67,7 +70,7 @@ abstract class Route implements IRoute
/* Filter parameters with null-value */ /* Filter parameters with null-value */
if ($this->filterEmptyParams === true) { if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, static function ($var) { $parameters = array_filter($parameters, static function ($var): bool {
return ($var !== null); return ($var !== null);
}); });
} }
@@ -82,6 +85,7 @@ abstract class Route implements IRoute
} }
/* When the callback is a function */ /* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters); return $router->getClassLoader()->loadClosure($callback, $parameters);
} }
@@ -280,7 +284,7 @@ abstract class Route implements IRoute
} }
/** /**
* @return string|callable * @return string|callable|null
*/ */
public function getCallback() public function getCallback()
{ {
@@ -337,6 +341,11 @@ abstract class Route implements IRoute
*/ */
public function setNamespace(string $namespace): 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(); $ns = $this->getNamespace();
if ($ns !== null) { 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 public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{ {
if (strpos($name, '.') !== false) { 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) { if ($found !== false) {
$method = (string)$found; $method = (string)$found;
} }
@@ -67,7 +67,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
foreach (Request::$requestTypes as $requestType) { foreach (Request::$requestTypes as $requestType) {
if (stripos($method, $requestType) === 0) { if (stripos($method, $requestType) === 0) {
$method = (string)substr($method, strlen($requestType)); $method = substr($method, strlen($requestType));
break; break;
} }
} }
@@ -54,7 +54,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
/* Remove method/type */ /* Remove method/type */
if (strpos($name, '.') !== false) { if (strpos($name, '.') !== false) {
$name = (string)substr($name, 0, strrpos($name, '.')); $name = substr($name, 0, strrpos($name, '.'));
} }
return (strtolower($this->name) === strtolower($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 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) { if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/'; return rtrim($this->url . $this->urls[$url], '/') . '/';
} }
+12 -17
View File
@@ -44,7 +44,7 @@ class Router
/** /**
* List of processed routes * List of processed routes
* @var array * @var array|ILoadableRoute[]
*/ */
protected $processedRoutes = []; protected $processedRoutes = [];
@@ -63,7 +63,7 @@ class Router
/** /**
* Csrf verifier class * Csrf verifier class
* @var BaseCsrfVerifier * @var BaseCsrfVerifier|null
*/ */
protected $csrfVerifier; protected $csrfVerifier;
@@ -107,7 +107,7 @@ class Router
/** /**
* Class loader instance * Class loader instance
* @var ClassLoader * @var IClassLoader
*/ */
protected $classLoader; protected $classLoader;
@@ -215,7 +215,7 @@ class Router
$exceptionHandlers = []; $exceptionHandlers = [];
// Stop processing routes if no valid route is found. // 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'); $this->debug('Halted route-processing as no valid route was found');
return; return;
@@ -575,7 +575,6 @@ class Router
'name' => $name, 'name' => $name,
]); ]);
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) { foreach ($this->processedRoutes as $route) {
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */ /* 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 */ /* 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)); [$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())) {
@@ -605,7 +604,7 @@ class Router
/* Check if callback matches (if it's not a function) */ /* Check if callback matches (if it's not a function) */
$callback = $route->getCallback(); $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 */ /* Check if the entire callback is matching */
if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) { if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) {
@@ -656,10 +655,6 @@ class Router
'getParams' => $getParams, 'getParams' => $getParams,
]); ]);
if ($getParams !== null && is_array($getParams) === false) {
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
if ($name === '' && $parameters === '') { if ($name === '' && $parameters === '') {
return new Url('/'); return new Url('/');
} }
@@ -703,21 +698,21 @@ class Router
/* 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 */
/* @var $route ILoadableRoute */ /* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) { foreach ($this->processedRoutes as $processedRoute) {
/* Check if the route contains the name/alias */ /* Check if the route contains the name/alias */
if ($route->hasName($controller) === true) { if ($processedRoute->hasName($controller) === true) {
return $this->request return $this->request
->getUrlCopy() ->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name)) ->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams); ->setParams($getParams);
} }
/* Check if the route controller is equal to the name */ /* 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 return $this->request
->getUrlCopy() ->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name)) ->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams); ->setParams($getParams);
} }
@@ -842,7 +837,7 @@ class Router
/** /**
* Get class loader * Get class loader
* *
* @return ClassLoader * @return IClassLoader
*/ */
public function getClassLoader(): 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\CallbackExceptionHandler;
use Pecee\SimpleRouter\Handlers\IEventHandler; use Pecee\SimpleRouter\Handlers\IEventHandler;
use Pecee\SimpleRouter\Route\IGroupRoute; use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute; use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute; use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RouteController; use Pecee\SimpleRouter\Route\RouteController;
@@ -173,7 +174,7 @@ class SimpleRouter
*/ */
public static function redirect(string $where, string $to, int $httpCode = 301): IRoute 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); static::response()->redirect($to, $httpCode);
}); });
} }
@@ -185,7 +186,7 @@ class SimpleRouter
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* *
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function get(string $url, $callback, array $settings = null): IRoute public static function get(string $url, $callback, array $settings = null): IRoute
{ {
@@ -198,7 +199,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function post(string $url, $callback, array $settings = null): IRoute public static function post(string $url, $callback, array $settings = null): IRoute
{ {
@@ -211,7 +212,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function put(string $url, $callback, array $settings = null): IRoute public static function put(string $url, $callback, array $settings = null): IRoute
{ {
@@ -224,7 +225,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function patch(string $url, $callback, array $settings = null): IRoute public static function patch(string $url, $callback, array $settings = null): IRoute
{ {
@@ -237,7 +238,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function options(string $url, $callback, array $settings = null): IRoute public static function options(string $url, $callback, array $settings = null): IRoute
{ {
@@ -250,7 +251,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
*/ */
public static function delete(string $url, $callback, array $settings = null): IRoute public static function delete(string $url, $callback, array $settings = null): IRoute
{ {
@@ -262,15 +263,11 @@ class SimpleRouter
* *
* @param array $settings * @param array $settings
* @param Closure $callback * @param Closure $callback
* @return RouteGroup * @return RouteGroup|IGroupRoute
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public static function group(array $settings, Closure $callback): IGroupRoute 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 = new RouteGroup();
$group->setCallback($callback); $group->setCallback($callback);
$group->setSettings($settings); $group->setSettings($settings);
@@ -287,15 +284,11 @@ class SimpleRouter
* @param string $url * @param string $url
* @param Closure $callback * @param Closure $callback
* @param array $settings * @param array $settings
* @return RoutePartialGroup * @return RoutePartialGroup|IPartialGroupRoute
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public static function partialGroup(string $url, Closure $callback, array $settings = []): IPartialGroupRoute 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; $settings['prefix'] = $url;
$group = new RoutePartialGroup(); $group = new RoutePartialGroup();
@@ -313,7 +306,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
* @see SimpleRouter::form * @see SimpleRouter::form
*/ */
public static function basic(string $url, $callback, array $settings = null): IRoute public static function basic(string $url, $callback, array $settings = null): IRoute
@@ -328,7 +321,7 @@ class SimpleRouter
* @param string $url * @param string $url
* @param string|array|Closure $callback * @param string|array|Closure $callback
* @param array|null $settings * @param array|null $settings
* @return RouteUrl * @return RouteUrl|IRoute
* @see SimpleRouter::form * @see SimpleRouter::form
*/ */
public static function form(string $url, $callback, array $settings = null): IRoute 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. * Prepends the default namespace to all new routes added.
* *
* @param IRoute $route * @param ILoadableRoute|IRoute $route
* @return IRoute * @return IRoute
*/ */
public static function addDefaultNamespace(IRoute $route): IRoute public static function addDefaultNamespace(IRoute $route): IRoute
@@ -0,0 +1,11 @@
<?php
namespace MyNamespace;
class NSController {
public function method()
{
return true;
}
}
@@ -2,6 +2,7 @@
require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php'; require_once 'Dummy/DummyController.php';
require_once 'Dummy/NSController.php';
require_once 'Dummy/Exception/ExceptionHandlerException.php'; require_once 'Dummy/Exception/ExceptionHandlerException.php';
class RouterRouteTest extends \PHPUnit\Framework\TestCase class RouterRouteTest extends \PHPUnit\Framework\TestCase
@@ -246,6 +247,16 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
$this->assertTrue(true); $this->assertTrue(true);
} }
public function testDefaultNameSpaceOverload()
{
TestRouter::setDefaultNamespace('DefaultNamespace\\Controllers');
TestRouter::get('/test', [\MyNamespace\NSController::class, 'method']);
$result = TestRouter::debugOutput('/test');
$this->assertTrue( (bool)$result);
}
public function testSameRoutes() public function testSameRoutes()
{ {
TestRouter::get('/recipe', 'DummyController@method1')->name('add'); TestRouter::get('/recipe', 'DummyController@method1')->name('add');
+4 -2
View File
@@ -22,12 +22,14 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
{ {
try { try {
static::debugNoReset($testUrl, $testMethod); static::debugNoReset($testUrl, $testMethod);
} catch(\Exception $e) { } catch (\Exception $e) {
static::$defaultNamespace = null;
static::router()->reset(); static::router()->reset();
throw $e; throw $e;
} }
if($reset === true) { if ($reset === true) {
static::$defaultNamespace = null;
static::router()->reset(); static::router()->reset();
} }