Skip to content

Commit 448a423

Browse files
authored
Merge pull request #544 from skipperbent/v4-development
Version 4.3.3.0
2 parents 569b3a8 + df9a855 commit 448a423

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed

src/Pecee/Http/Input/InputFile.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ public function hasError(): bool
216216
/**
217217
* Get upload-error code.
218218
*
219-
* @return int
219+
* @return int|null
220220
*/
221-
public function getError(): int
221+
public function getError(): ?int
222222
{
223-
return (int)$this->errors;
223+
return $this->errors;
224224
}
225225

226226
/**

src/Pecee/Http/Url.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ public function setParams(array $params): self
248248
public function setQueryString(string $queryString): self
249249
{
250250
$params = [];
251+
parse_str($queryString, $params);
251252

252-
if(parse_str($queryString, $params) !== false) {
253+
if(count($params) > 0) {
253254
return $this->setParams($params);
254255
}
255256

@@ -341,7 +342,7 @@ public function hasParam(string $name): bool
341342
*/
342343
public function removeParams(...$names): self
343344
{
344-
$params = array_diff_key($this->getParams(), array_flip($names));
345+
$params = array_diff_key($this->getParams(), array_flip(...$names));
345346
$this->setParams($params);
346347

347348
return $this;

src/Pecee/SimpleRouter/Route/Route.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ public function setClass(string $class): IRoute
337337
*/
338338
public function setNamespace(string $namespace): IRoute
339339
{
340+
$ns = $this->getNamespace();
341+
342+
if ($ns !== null) {
343+
// Don't overwrite namespaces that starts with \
344+
if ($ns[0] !== '\\') {
345+
$namespace .= '\\' . $ns;
346+
} else {
347+
$namespace = $ns;
348+
}
349+
}
350+
340351
$this->namespace = $namespace;
341352

342353
return $this;
@@ -407,7 +418,7 @@ public function toArray(): array
407418
*/
408419
public function setSettings(array $settings, bool $merge = false): IRoute
409420
{
410-
if ($this->namespace === null && isset($settings['namespace']) === true) {
421+
if (isset($settings['namespace']) === true) {
411422
$this->setNamespace($settings['namespace']);
412423
}
413424

src/Pecee/SimpleRouter/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ protected function handleException(Exception $e): ?string
509509
]);
510510

511511
/* @var $handler IExceptionHandler */
512-
foreach ($this->exceptionHandlers as $key => $handler) {
512+
foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
513513

514514
if (is_object($handler) === false) {
515515
$handler = new $handler();

src/Pecee/SimpleRouter/SimpleRouter.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public static function form(string $url, $callback, array $settings = null): IRo
348348
* @param array|null $settings
349349
* @return RouteUrl|IRoute
350350
*/
351-
public static function match(array $requestMethods, string $url, $callback, array $settings = null)
351+
public static function match(array $requestMethods, string $url, $callback, array $settings = null): IRoute
352352
{
353353
$route = new RouteUrl($url, $callback);
354354
$route->setRequestMethods($requestMethods);
@@ -368,7 +368,7 @@ public static function match(array $requestMethods, string $url, $callback, arra
368368
* @param array|null $settings
369369
* @return RouteUrl|IRoute
370370
*/
371-
public static function all(string $url, $callback, array $settings = null)
371+
public static function all(string $url, $callback, array $settings = null): IRoute
372372
{
373373
$route = new RouteUrl($url, $callback);
374374

@@ -387,7 +387,7 @@ public static function all(string $url, $callback, array $settings = null)
387387
* @param array|null $settings
388388
* @return RouteController|IRoute
389389
*/
390-
public static function controller(string $url, string $controller, array $settings = null)
390+
public static function controller(string $url, string $controller, array $settings = null): IRoute
391391
{
392392
$route = new RouteController($url, $controller);
393393

@@ -406,7 +406,7 @@ public static function controller(string $url, string $controller, array $settin
406406
* @param array|null $settings
407407
* @return RouteResource|IRoute
408408
*/
409-
public static function resource(string $url, string $controller, array $settings = null)
409+
public static function resource(string $url, string $controller, array $settings = null): IRoute
410410
{
411411
$route = new RouteResource($url, $controller);
412412

@@ -512,20 +512,7 @@ public static function router(): Router
512512
public static function addDefaultNamespace(IRoute $route): IRoute
513513
{
514514
if (static::$defaultNamespace !== null) {
515-
516-
$ns = static::$defaultNamespace;
517-
$namespace = $route->getNamespace();
518-
519-
if ($namespace !== null) {
520-
// Don't overwrite namespaces that starts with \
521-
if ($namespace[0] !== '\\') {
522-
$ns .= '\\' . $namespace;
523-
} else {
524-
$ns = $namespace;
525-
}
526-
}
527-
528-
$route->setNamespace($ns);
515+
$route->setNamespace(static::$defaultNamespace);
529516
}
530517

531518
return $route;

tests/Pecee/SimpleRouter/GroupTest.php renamed to tests/Pecee/SimpleRouter/RouterGroupTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
require_once 'Dummy/DummyMiddleware.php';
44
require_once 'Dummy/DummyController.php';
55

6-
class GroupTest extends \PHPUnit\Framework\TestCase
6+
class RouterGroupTest extends \PHPUnit\Framework\TestCase
77
{
88

99
public function testGroupLoad()
1010
{
1111
$result = false;
1212

13-
TestRouter::group(['prefix' => '/group'], function () use(&$result) {
13+
TestRouter::group(['prefix' => '/group'], function () use (&$result) {
1414
$result = true;
1515
});
1616

1717
try {
1818
TestRouter::debug('/', 'get');
19-
} catch(\Exception $e) {
19+
} catch (\Exception $e) {
2020

2121
}
2222
$this->assertTrue($result);
@@ -81,4 +81,40 @@ public function testUrls()
8181

8282
}
8383

84+
public function testNamespaceExtend()
85+
{
86+
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
87+
88+
TestRouter::group(['namespace' => 'Service'], function () use (&$result) {
89+
90+
TestRouter::get('/test', function () use (&$result) {
91+
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
92+
});
93+
94+
});
95+
96+
});
97+
98+
$namespace = TestRouter::debugOutput('/test');
99+
$this->assertEquals('\My\Namespace\Service', $namespace);
100+
}
101+
102+
public function testNamespaceOverwrite()
103+
{
104+
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
105+
106+
TestRouter::group(['namespace' => '\Service'], function () use (&$result) {
107+
108+
TestRouter::get('/test', function () use (&$result) {
109+
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
110+
});
111+
112+
});
113+
114+
});
115+
116+
$namespace = TestRouter::debugOutput('/test');
117+
$this->assertEquals('\Service', $namespace);
118+
}
119+
84120
}

tests/Pecee/SimpleRouter/RouterRewriteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function testExceptionHandlerRewrite()
4949
}
5050

5151
$expectedStack = [
52-
ExceptionHandlerFirst::class,
53-
ExceptionHandlerSecond::class,
5452
ExceptionHandlerThird::class,
53+
ExceptionHandlerSecond::class,
54+
ExceptionHandlerFirst::class,
5555
];
5656

5757
$this->assertEquals($expectedStack, $stack);

0 commit comments

Comments
 (0)