From c3ecd037efa0bc6e77901be3d86bd06bbc88201c Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 26 Oct 2024 03:12:19 +0200 Subject: [PATCH] v3 --- src/Annotation/Path.php | 28 ++++++++ src/Annotation/PathHome.php | 28 ++++++++ src/Loader/DescriptorLoader.php | 8 +++ .../WhitespaceTrimmingMiddleware.php | 68 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 src/Annotation/Path.php create mode 100644 src/Annotation/PathHome.php create mode 100644 src/Middleware/WhitespaceTrimmingMiddleware.php diff --git a/src/Annotation/Path.php b/src/Annotation/Path.php new file mode 100644 index 00000000..bb827434 --- /dev/null +++ b/src/Annotation/Path.php @@ -0,0 +1,28 @@ + + * @copyright Copyright (c) 2018, Anatoly Nekhay + * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE + * @link https://github.com/sunrise-php/http-router + */ + +declare(strict_types=1); + +namespace Sunrise\Http\Router\Annotation; + +use Attribute; + +/** + * @since 3.0.0 + */ +#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] +class Path +{ + public function __construct( + public readonly string $value, + ) { + } +} diff --git a/src/Annotation/PathHome.php b/src/Annotation/PathHome.php new file mode 100644 index 00000000..958270fd --- /dev/null +++ b/src/Annotation/PathHome.php @@ -0,0 +1,28 @@ + + * @copyright Copyright (c) 2018, Anatoly Nekhay + * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE + * @link https://github.com/sunrise-php/http-router + */ + +declare(strict_types=1); + +namespace Sunrise\Http\Router\Annotation; + +use Attribute; + +/** + * @since 3.0.0 + */ +#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] +final class PathHome extends Path +{ + public function __construct() + { + parent::__construct('/'); + } +} diff --git a/src/Loader/DescriptorLoader.php b/src/Loader/DescriptorLoader.php index d9423b03..1971ac79 100644 --- a/src/Loader/DescriptorLoader.php +++ b/src/Loader/DescriptorLoader.php @@ -30,6 +30,7 @@ use Sunrise\Http\Router\Annotation\Method; use Sunrise\Http\Router\Annotation\Middleware; use Sunrise\Http\Router\Annotation\NamePrefix; +use Sunrise\Http\Router\Annotation\Path; use Sunrise\Http\Router\Annotation\PathPostfix; use Sunrise\Http\Router\Annotation\PathPrefix; use Sunrise\Http\Router\Annotation\Pattern; @@ -220,6 +221,13 @@ private static function enrichDescriptorFromClassOrMethod(Descriptor $descriptor $descriptor->namePrefixes[] = $annotation->value; } + /** @var list> $annotations */ + $annotations = $classOrMethod->getAttributes(Path::class, ReflectionAttribute::IS_INSTANCEOF); + if (isset($annotations[0])) { + $annotation = $annotations[0]->newInstance(); + $descriptor->path = $annotation->value; + } + /** @var list> $annotations */ $annotations = $classOrMethod->getAttributes(PathPrefix::class); if (isset($annotations[0])) { diff --git a/src/Middleware/WhitespaceTrimmingMiddleware.php b/src/Middleware/WhitespaceTrimmingMiddleware.php new file mode 100644 index 00000000..66a3d9ef --- /dev/null +++ b/src/Middleware/WhitespaceTrimmingMiddleware.php @@ -0,0 +1,68 @@ + + * @copyright Copyright (c) 2018, Anatoly Nekhay + * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE + * @link https://github.com/sunrise-php/http-router + */ + +declare(strict_types=1); + +namespace Sunrise\Http\Router\Middleware; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +use function array_walk_recursive; +use function is_array; +use function is_string; +use function trim; + +/** + * @since 3.0.0 + */ +final class WhitespaceTrimmingMiddleware implements MiddlewareInterface +{ + /** + * @var callable + * + * @readonly + */ + private $walker; + + /** + * @param null|callable(string):string $trimmer + */ + public function __construct(?callable $trimmer) + { + $trimmer ??= trim(...); + + $this->walker = static function (mixed &$value) use ($trimmer): void { + if (is_string($value)) { + $value = $trimmer($value); + } + }; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $parsedBody = $request->getParsedBody(); + if ($parsedBody !== [] && is_array($parsedBody)) { + array_walk_recursive($parsedBody, $this->walker); + $request = $request->withParsedBody($parsedBody); + } + + $queryParams = $request->getQueryParams(); + if ($queryParams !== []) { + array_walk_recursive($queryParams, $this->walker); + $request = $request->withQueryParams($queryParams); + } + + return $handler->handle($request); + } +}