Merge pull request #544 from skipperbent/v4-development

Version 4.3.3.0
This commit is contained in:
Simon Sessingø
2021-05-02 14:00:10 +02:00
committed by GitHub
7 changed files with 65 additions and 30 deletions
+3 -3
View File
@@ -216,11 +216,11 @@ class InputFile implements IInputItem
/** /**
* Get upload-error code. * Get upload-error code.
* *
* @return int * @return int|null
*/ */
public function getError(): int public function getError(): ?int
{ {
return (int)$this->errors; return $this->errors;
} }
/** /**
+3 -2
View File
@@ -248,8 +248,9 @@ class Url implements JsonSerializable
public function setQueryString(string $queryString): self public function setQueryString(string $queryString): self
{ {
$params = []; $params = [];
parse_str($queryString, $params);
if(parse_str($queryString, $params) !== false) { if(count($params) > 0) {
return $this->setParams($params); return $this->setParams($params);
} }
@@ -341,7 +342,7 @@ class Url implements JsonSerializable
*/ */
public function removeParams(...$names): self public function removeParams(...$names): self
{ {
$params = array_diff_key($this->getParams(), array_flip($names)); $params = array_diff_key($this->getParams(), array_flip(...$names));
$this->setParams($params); $this->setParams($params);
return $this; return $this;
+12 -1
View File
@@ -337,6 +337,17 @@ abstract class Route implements IRoute
*/ */
public function setNamespace(string $namespace): IRoute public function setNamespace(string $namespace): IRoute
{ {
$ns = $this->getNamespace();
if ($ns !== null) {
// Don't overwrite namespaces that starts with \
if ($ns[0] !== '\\') {
$namespace .= '\\' . $ns;
} else {
$namespace = $ns;
}
}
$this->namespace = $namespace; $this->namespace = $namespace;
return $this; return $this;
@@ -407,7 +418,7 @@ abstract class Route implements IRoute
*/ */
public function setSettings(array $settings, bool $merge = false): IRoute public function setSettings(array $settings, bool $merge = false): IRoute
{ {
if ($this->namespace === null && isset($settings['namespace']) === true) { if (isset($settings['namespace']) === true) {
$this->setNamespace($settings['namespace']); $this->setNamespace($settings['namespace']);
} }
+1 -1
View File
@@ -509,7 +509,7 @@ class Router
]); ]);
/* @var $handler IExceptionHandler */ /* @var $handler IExceptionHandler */
foreach ($this->exceptionHandlers as $key => $handler) { foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
if (is_object($handler) === false) { if (is_object($handler) === false) {
$handler = new $handler(); $handler = new $handler();
+5 -18
View File
@@ -348,7 +348,7 @@ class SimpleRouter
* @param array|null $settings * @param array|null $settings
* @return RouteUrl|IRoute * @return RouteUrl|IRoute
*/ */
public static function match(array $requestMethods, string $url, $callback, array $settings = null) public static function match(array $requestMethods, string $url, $callback, array $settings = null): IRoute
{ {
$route = new RouteUrl($url, $callback); $route = new RouteUrl($url, $callback);
$route->setRequestMethods($requestMethods); $route->setRequestMethods($requestMethods);
@@ -368,7 +368,7 @@ class SimpleRouter
* @param array|null $settings * @param array|null $settings
* @return RouteUrl|IRoute * @return RouteUrl|IRoute
*/ */
public static function all(string $url, $callback, array $settings = null) public static function all(string $url, $callback, array $settings = null): IRoute
{ {
$route = new RouteUrl($url, $callback); $route = new RouteUrl($url, $callback);
@@ -387,7 +387,7 @@ class SimpleRouter
* @param array|null $settings * @param array|null $settings
* @return RouteController|IRoute * @return RouteController|IRoute
*/ */
public static function controller(string $url, string $controller, array $settings = null) public static function controller(string $url, string $controller, array $settings = null): IRoute
{ {
$route = new RouteController($url, $controller); $route = new RouteController($url, $controller);
@@ -406,7 +406,7 @@ class SimpleRouter
* @param array|null $settings * @param array|null $settings
* @return RouteResource|IRoute * @return RouteResource|IRoute
*/ */
public static function resource(string $url, string $controller, array $settings = null) public static function resource(string $url, string $controller, array $settings = null): IRoute
{ {
$route = new RouteResource($url, $controller); $route = new RouteResource($url, $controller);
@@ -512,20 +512,7 @@ class SimpleRouter
public static function addDefaultNamespace(IRoute $route): IRoute public static function addDefaultNamespace(IRoute $route): IRoute
{ {
if (static::$defaultNamespace !== null) { if (static::$defaultNamespace !== null) {
$route->setNamespace(static::$defaultNamespace);
$ns = static::$defaultNamespace;
$namespace = $route->getNamespace();
if ($namespace !== null) {
// Don't overwrite namespaces that starts with \
if ($namespace[0] !== '\\') {
$ns .= '\\' . $namespace;
} else {
$ns = $namespace;
}
}
$route->setNamespace($ns);
} }
return $route; return $route;
@@ -3,20 +3,20 @@
require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php'; require_once 'Dummy/DummyController.php';
class GroupTest extends \PHPUnit\Framework\TestCase class RouterGroupTest extends \PHPUnit\Framework\TestCase
{ {
public function testGroupLoad() public function testGroupLoad()
{ {
$result = false; $result = false;
TestRouter::group(['prefix' => '/group'], function () use(&$result) { TestRouter::group(['prefix' => '/group'], function () use (&$result) {
$result = true; $result = true;
}); });
try { try {
TestRouter::debug('/', 'get'); TestRouter::debug('/', 'get');
} catch(\Exception $e) { } catch (\Exception $e) {
} }
$this->assertTrue($result); $this->assertTrue($result);
@@ -81,4 +81,40 @@ class GroupTest extends \PHPUnit\Framework\TestCase
} }
public function testNamespaceExtend()
{
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
TestRouter::group(['namespace' => 'Service'], function () use (&$result) {
TestRouter::get('/test', function () use (&$result) {
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
});
});
});
$namespace = TestRouter::debugOutput('/test');
$this->assertEquals('\My\Namespace\Service', $namespace);
}
public function testNamespaceOverwrite()
{
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
TestRouter::group(['namespace' => '\Service'], function () use (&$result) {
TestRouter::get('/test', function () use (&$result) {
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
});
});
});
$namespace = TestRouter::debugOutput('/test');
$this->assertEquals('\Service', $namespace);
}
} }
@@ -49,9 +49,9 @@ class RouterRewriteTest extends \PHPUnit\Framework\TestCase
} }
$expectedStack = [ $expectedStack = [
ExceptionHandlerFirst::class,
ExceptionHandlerSecond::class,
ExceptionHandlerThird::class, ExceptionHandlerThird::class,
ExceptionHandlerSecond::class,
ExceptionHandlerFirst::class,
]; ];
$this->assertEquals($expectedStack, $stack); $this->assertEquals($expectedStack, $stack);