From d6bd9bbd724c3ca4160422624033ce00eb2b3efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 2 May 2021 13:48:13 +0200 Subject: [PATCH] =?UTF-8?q?Development=20-=20[FEATURE]=20Namespace=20overw?= =?UTF-8?q?rite=20now=20works=20globally.=20'Service=C2=A8'=20will=20appen?= =?UTF-8?q?d=20namespace=20whereas=20'\Service'=20will=20overwrite=20it.?= =?UTF-8?q?=20-=20[FEATURE]=20Exceptionhandlers=20are=20now=20rendered=20i?= =?UTF-8?q?n=20reverse=20order=20from=20newest=20to=20oldest.=20This=20all?= =?UTF-8?q?ows=20for=20exceptions=20to=20be=20handled=20from=20parent=20ex?= =?UTF-8?q?ceptions=20which=20were=20otherwise=20ignored.=20-=20Fixed=20in?= =?UTF-8?q?correct=20return=20type=20for=20InputFile::getError=20to=20null?= =?UTF-8?q?able=20int.=20-=20Added=20return=20type=20to=20all,=20match,=20?= =?UTF-8?q?controller=20and=20resource=20in=20SimpleRouter=20class.=20-=20?= =?UTF-8?q?Fixed=20incorrect=20usage=20of=20parse=5Fstr=20in=20Url::setQue?= =?UTF-8?q?ryString=20method.=20-=20Fixed=20incorrect=20expected=20value?= =?UTF-8?q?=20in=20array=5Fflip=20in=20Url::removeParams=20method.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Pecee/Http/Input/InputFile.php | 6 +-- src/Pecee/Http/Url.php | 5 ++- src/Pecee/SimpleRouter/Route/Route.php | 13 +++++- src/Pecee/SimpleRouter/Router.php | 2 +- src/Pecee/SimpleRouter/SimpleRouter.php | 23 +++------- .../{GroupTest.php => RouterGroupTest.php} | 42 +++++++++++++++++-- .../Pecee/SimpleRouter/RouterRewriteTest.php | 4 +- 7 files changed, 65 insertions(+), 30 deletions(-) rename tests/Pecee/SimpleRouter/{GroupTest.php => RouterGroupTest.php} (60%) diff --git a/src/Pecee/Http/Input/InputFile.php b/src/Pecee/Http/Input/InputFile.php index 7123c04..c829d01 100644 --- a/src/Pecee/Http/Input/InputFile.php +++ b/src/Pecee/Http/Input/InputFile.php @@ -216,11 +216,11 @@ class InputFile implements IInputItem /** * Get upload-error code. * - * @return int + * @return int|null */ - public function getError(): int + public function getError(): ?int { - return (int)$this->errors; + return $this->errors; } /** diff --git a/src/Pecee/Http/Url.php b/src/Pecee/Http/Url.php index 08dc50d..68786d3 100644 --- a/src/Pecee/Http/Url.php +++ b/src/Pecee/Http/Url.php @@ -248,8 +248,9 @@ class Url implements JsonSerializable public function setQueryString(string $queryString): self { $params = []; + parse_str($queryString, $params); - if(parse_str($queryString, $params) !== false) { + if(count($params) > 0) { return $this->setParams($params); } @@ -341,7 +342,7 @@ class Url implements JsonSerializable */ 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); return $this; diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 438d12a..62da265 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -337,6 +337,17 @@ abstract class Route implements 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; return $this; @@ -407,7 +418,7 @@ abstract class Route implements 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']); } diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 45deb1f..6a662f0 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -509,7 +509,7 @@ class Router ]); /* @var $handler IExceptionHandler */ - foreach ($this->exceptionHandlers as $key => $handler) { + foreach (array_reverse($this->exceptionHandlers) as $key => $handler) { if (is_object($handler) === false) { $handler = new $handler(); diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index f96bf84..60cf12e 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -348,7 +348,7 @@ class SimpleRouter * @param array|null $settings * @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->setRequestMethods($requestMethods); @@ -368,7 +368,7 @@ class SimpleRouter * @param array|null $settings * @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); @@ -387,7 +387,7 @@ class SimpleRouter * @param array|null $settings * @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); @@ -406,7 +406,7 @@ class SimpleRouter * @param array|null $settings * @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); @@ -512,20 +512,7 @@ class SimpleRouter public static function addDefaultNamespace(IRoute $route): IRoute { if (static::$defaultNamespace !== null) { - - $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); + $route->setNamespace(static::$defaultNamespace); } return $route; diff --git a/tests/Pecee/SimpleRouter/GroupTest.php b/tests/Pecee/SimpleRouter/RouterGroupTest.php similarity index 60% rename from tests/Pecee/SimpleRouter/GroupTest.php rename to tests/Pecee/SimpleRouter/RouterGroupTest.php index ce79b26..d922d96 100644 --- a/tests/Pecee/SimpleRouter/GroupTest.php +++ b/tests/Pecee/SimpleRouter/RouterGroupTest.php @@ -3,20 +3,20 @@ require_once 'Dummy/DummyMiddleware.php'; require_once 'Dummy/DummyController.php'; -class GroupTest extends \PHPUnit\Framework\TestCase +class RouterGroupTest extends \PHPUnit\Framework\TestCase { public function testGroupLoad() { $result = false; - TestRouter::group(['prefix' => '/group'], function () use(&$result) { + TestRouter::group(['prefix' => '/group'], function () use (&$result) { $result = true; }); try { TestRouter::debug('/', 'get'); - } catch(\Exception $e) { + } catch (\Exception $e) { } $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); + } + } \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/RouterRewriteTest.php b/tests/Pecee/SimpleRouter/RouterRewriteTest.php index 5764638..285036e 100644 --- a/tests/Pecee/SimpleRouter/RouterRewriteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRewriteTest.php @@ -49,9 +49,9 @@ class RouterRewriteTest extends \PHPUnit\Framework\TestCase } $expectedStack = [ - ExceptionHandlerFirst::class, - ExceptionHandlerSecond::class, ExceptionHandlerThird::class, + ExceptionHandlerSecond::class, + ExceptionHandlerFirst::class, ]; $this->assertEquals($expectedStack, $stack);