Compare commits

..

8 Commits

Author SHA1 Message Date
Simon Sessingø 448a423d5d Merge pull request #544 from skipperbent/v4-development
Version 4.3.3.0
2021-05-02 14:00:10 +02:00
Simon Sessingø df9a855579 Merge pull request #543 from skipperbent/v4-bugfixes
Bugfixes
2021-05-02 13:58:00 +02:00
Simon Sessingø d6bd9bbd72 Development
- [FEATURE] Namespace overwrite now works globally. 'Service¨' will append namespace whereas '\Service' will overwrite it.
- [FEATURE] Exceptionhandlers are now rendered in reverse order from newest to oldest. This allows for exceptions to be handled from parent exceptions which were otherwise ignored.
- Fixed incorrect return type for InputFile::getError to nullable int.
- Added return type to all, match, controller and resource in SimpleRouter class.
- Fixed incorrect usage of parse_str in Url::setQueryString method.
- Fixed incorrect expected value in array_flip in Url::removeParams method.
2021-05-02 13:48:13 +02:00
Simon Sessingø 569b3a8760 Merge pull request #541 from skipperbent/v4-development
Version 4.3.2.3
2021-04-28 10:05:34 +02:00
Simon Sessingø 79a075ef49 Merge pull request #540 from skipperbent/v4-type-hint-notice
Fixed deprecated notice when using class type hinting (issue: #538)
2021-04-28 10:03:55 +02:00
Simon Sessingø c66d7f7df7 Fixed deprecated notice when using class type hinting (issue: #538) 2021-04-28 09:57:47 +02:00
Simon Sessingø c6d0ff3c0e Merge pull request #536 from skipperbent/v4-development
Version 4.3.2.2
2021-04-28 03:41:47 +02:00
Simon Sessingø 718d60c53b Removed return type from IResourceController as the return type can be mixed. 2021-04-04 11:21:26 +02:00
10 changed files with 94 additions and 53 deletions
+14 -14
View File
@@ -6,43 +6,43 @@ interface IResourceController
{ {
/** /**
* @return string|null * @return mixed
*/ */
public function index(): ?string; public function index();
/** /**
* @param mixed $id * @param mixed $id
* @return string|null * @return mixed
*/ */
public function show($id): ?string; public function show($id);
/** /**
* @return string|null * @return mixed
*/ */
public function store(): ?string; public function store();
/** /**
* @return string|null * @return mixed
*/ */
public function create(): ?string; public function create();
/** /**
* View * View
* @param mixed $id * @param mixed $id
* @return string|null * @return mixed
*/ */
public function edit($id): ?string; public function edit($id);
/** /**
* @param mixed $id * @param mixed $id
* @return string|null * @return mixed
*/ */
public function update($id): ?string; public function update($id);
/** /**
* @param mixed $id * @param mixed $id
* @return string|null * @return mixed
*/ */
public function destroy($id): ?string; public function destroy($id);
} }
+9 -9
View File
@@ -165,7 +165,7 @@ class InputFile implements IInputItem
* @param string $name * @param string $name
* @return static * @return static
*/ */
public function setFilename($name): IInputItem public function setFilename(string $name): IInputItem
{ {
$this->filename = $name; $this->filename = $name;
@@ -188,7 +188,7 @@ class InputFile implements IInputItem
* @param string $destination * @param string $destination
* @return bool * @return bool
*/ */
public function move($destination): bool public function move(string $destination): bool
{ {
return move_uploaded_file($this->tmpName, $destination); return move_uploaded_file($this->tmpName, $destination);
} }
@@ -216,20 +216,20 @@ 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;
} }
/** /**
* Set error * Set error
* *
* @param int $error * @param int|null $error
* @return static * @return static
*/ */
public function setError($error): IInputItem public function setError(?int $error): IInputItem
{ {
$this->errors = (int)$error; $this->errors = (int)$error;
@@ -249,7 +249,7 @@ class InputFile implements IInputItem
* @param string $name * @param string $name
* @return static * @return static
*/ */
public function setTmpName($name): IInputItem public function setTmpName(string $name): IInputItem
{ {
$this->tmpName = $name; $this->tmpName = $name;
@@ -261,7 +261,7 @@ class InputFile implements IInputItem
return $this->getTmpName(); return $this->getTmpName();
} }
public function getValue() public function getValue(): string
{ {
return $this->getFilename(); return $this->getFilename();
} }
+1 -1
View File
@@ -147,7 +147,7 @@ class InputHandler
* @param array|null $original * @param array|null $original
* @return array * @return array
*/ */
protected function rearrangeFile(array $values, &$index, $original): array protected function rearrangeFile(array $values, array &$index, ?array $original): array
{ {
$originalIndex = $index[0]; $originalIndex = $index[0];
array_shift($index); array_shift($index);
+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;
@@ -55,7 +55,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
public function matchRegex(Request $request, $url): ?bool public function matchRegex(Request $request, $url): ?bool
{ {
/* Match on custom defined regular expression */ /* Match on custom defined regular expression */
if ($this->regex === null) { if ($this->regex === null) {
return null; return null;
} }
+20 -2
View File
@@ -76,6 +76,11 @@ abstract class Route implements IRoute
if (is_callable($callback) === true) { if (is_callable($callback) === true) {
$router->debug('Executing callback'); $router->debug('Executing callback');
/* Load class from type hinting */
if (is_array($callback) === true && isset($callback[0], $callback[1]) === true) {
$callback[0] = $router->getClassLoader()->loadClass($callback[0]);
}
/* When the callback is a function */ /* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters); return $router->getClassLoader()->loadClosure($callback, $parameters);
} }
@@ -160,7 +165,7 @@ abstract class Route implements IRoute
foreach ((array)$parameters[1] as $name) { foreach ((array)$parameters[1] as $name) {
// Ignore parent parameters // Ignore parent parameters
if(isset($groupParameters[$name]) === true) { if (isset($groupParameters[$name]) === true) {
$lastParams[$name] = $matches[$name]; $lastParams[$name] = $matches[$name];
continue; continue;
} }
@@ -244,6 +249,7 @@ abstract class Route implements IRoute
$this->group = $group; $this->group = $group;
/* Add/merge parent settings with child */ /* Add/merge parent settings with child */
return $this->setSettings($group->toArray(), true); return $this->setSettings($group->toArray(), true);
} }
@@ -331,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;
@@ -401,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']);
} }
@@ -576,6 +593,7 @@ abstract class Route implements IRoute
public function setFilterEmptyParams(bool $enabled): IRoute public function setFilterEmptyParams(bool $enabled): IRoute
{ {
$this->filterEmptyParams = $enabled; $this->filterEmptyParams = $enabled;
return $this; return $this;
} }
+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);