From adfe70f191c5f8fe435c9fcc1d3aea07a43ba472 Mon Sep 17 00:00:00 2001 From: ATC-4K <84913633+ATC-4K@users.noreply.github.com> Date: Mon, 10 Jul 2023 21:16:32 +0200 Subject: [PATCH 1/4] Added @return never to Response.php In PHP8.1 instead of :void :never would be returned. As this project is PHP7.4 compatible, we add it as a PHPDOC, so IDEs using this project will no longer complain and automatic checks will successfully detect dead code after calling a redirect() --- src/Pecee/Http/Response.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index 0c4b910..5e6ca47 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -32,6 +32,8 @@ class Response * * @param string $url * @param ?int $httpCode + * + * @return never */ public function redirect(string $url, ?int $httpCode = null): void { @@ -127,4 +129,4 @@ class Response return $this; } -} \ No newline at end of file +} From cdf165d0f464eee74564379375d753199ddeb3fc Mon Sep 17 00:00:00 2001 From: Marco Scagnol <58094448+ms-afk@users.noreply.github.com> Date: Sat, 30 Sep 2023 16:22:52 +0200 Subject: [PATCH 2/4] Fixed the php-di integration example The previous version of the example in the README used exceptions that were not imported (NotFoundHttpException) to wrap existing exceptions. I've adapted the example to be similar to the current version of Pecee\SimpleRouter\ClassLoader\ClassLoader. That includes returning strings from loadClassMethod and loadClosure and not wrapping every exception, thrown by the called functions, into NotFoundHttpException exceptions, as well as using the exception ClassNotFoundHttpException when a class cannot be found, instead of the generic NotFoundHttpException. I also changed the way the ClassLoader checks if the container can resolve the class by using the container's method "has" instead of the php function class_exists. --- README.md | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3a50b96..3dbbb21 100644 --- a/README.md +++ b/README.md @@ -1742,6 +1742,7 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader()); php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below: ```php +use Pecee\SimpleRouter\ClassLoader\IClassLoader; use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException; class MyCustomClassLoader implements IClassLoader @@ -1762,19 +1763,14 @@ class MyCustomClassLoader implements IClassLoader * * @param string $class * @return object - * @throws NotFoundHttpException + * @throws ClassNotFoundHttpException */ public function loadClass(string $class) { - if (class_exists($class) === false) { - throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); + if ($this->container->has($class) === false) { + throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null); } - - try { - return $this->container->get($class); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } + return $this->container->get($class); } /** @@ -1782,15 +1778,11 @@ class MyCustomClassLoader implements IClassLoader * @param object $class * @param string $method * @param array $parameters - * @return object + * @return string */ public function loadClassMethod($class, string $method, array $parameters) { - try { - return $this->container->call([$class, $method], $parameters); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } + return (string)$this->container->call([$class, $method], $parameters); } /** @@ -1798,15 +1790,11 @@ class MyCustomClassLoader implements IClassLoader * * @param Callable $closure * @param array $parameters - * @return mixed + * @return string */ public function loadClosure(callable $closure, array $parameters) { - try { - return $this->container->call($closure, $parameters); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } + return (string)$this->container->call($closure, $parameters); } } ``` From 5986dc9a0801e4f93ddb38e32f0d48ecc2cade68 Mon Sep 17 00:00:00 2001 From: ms-afk <58094448+ms-afk@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:26:41 +0200 Subject: [PATCH 3/4] fixed rare double execution of rewrite routes in exception handler If a rewrite route is present, Router's method handleException will, currently, be adding that route to the processedRoutes array without removing the hasPendingRewrite flag. This leads to the associated callback being executed twice if the callback itself returns NULL. This happens because the handleRouteRewrite method, finding that hasPendingRewrite is still set to true, adds the rewriteRoute to the processedRoutes for a second time, before finally setting that flag to false. --- src/Pecee/SimpleRouter/Router.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index fd2affa..8c88793 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -562,6 +562,7 @@ class Router if ($this->request->getRewriteRoute() !== null) { $this->processedRoutes[] = $this->request->getRewriteRoute(); + $this->request->setHasPendingRewrite(false); } return $this->routeRequest(); From 08d78c8f7165a2592bf7877bc44132a79852008d Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 21 Nov 2023 16:08:22 +0100 Subject: [PATCH 4/4] Added support for input stream when not json encoded --- src/Pecee/Http/Input/InputHandler.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Pecee/Http/Input/InputHandler.php b/src/Pecee/Http/Input/InputHandler.php index 943ea04..3d90b3c 100644 --- a/src/Pecee/Http/Input/InputHandler.php +++ b/src/Pecee/Http/Input/InputHandler.php @@ -82,6 +82,8 @@ class InputHandler if ($post !== false) { $this->originalPost += $post; } + } else { + parse_str($contents, $this->originalPost); } } @@ -108,7 +110,7 @@ class InputHandler foreach ($files as $key => $value) { // Parse multi dept file array - if(isset($value['name']) === false && is_array($value) === true) { + if (isset($value['name']) === false && is_array($value) === true) { $list[$key] = $this->parseFiles($value, $key); continue; } @@ -161,12 +163,12 @@ class InputHandler try { $file = InputFile::createFromArray([ - 'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key, - 'name' => $original['name'][$key], - 'error' => $original['error'][$key], + 'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key, + 'name' => $original['name'][$key], + 'error' => $original['error'][$key], 'tmp_name' => $original['tmp_name'][$key], - 'type' => $original['type'][$key], - 'size' => $original['size'][$key], + 'type' => $original['type'][$key], + 'size' => $original['size'][$key], ]); if (isset($output[$key]) === true) { @@ -231,7 +233,7 @@ class InputHandler { $element = null; - if(count($methods) > 0) { + if (count($methods) > 0) { $methods = is_array(...$methods) ? array_values(...$methods) : $methods; } @@ -303,9 +305,9 @@ class InputHandler public function exists($index, ...$methods): bool { // Check array - if(is_array($index) === true) { - foreach($index as $key) { - if($this->value($key, null, ...$methods) === null) { + if (is_array($index) === true) { + foreach ($index as $key) { + if ($this->value($key, null, ...$methods) === null) { return false; } }