Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Oct 26, 2024
1 parent 564e5b0 commit c3ecd03
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Annotation/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Nekhay <[email protected]>
* @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,
) {
}
}
28 changes: 28 additions & 0 deletions src/Annotation/PathHome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Nekhay <[email protected]>
* @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('/');
}
}
8 changes: 8 additions & 0 deletions src/Loader/DescriptorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,6 +221,13 @@ private static function enrichDescriptorFromClassOrMethod(Descriptor $descriptor
$descriptor->namePrefixes[] = $annotation->value;
}

/** @var list<ReflectionAttribute<Path>> $annotations */
$annotations = $classOrMethod->getAttributes(Path::class, ReflectionAttribute::IS_INSTANCEOF);
if (isset($annotations[0])) {
$annotation = $annotations[0]->newInstance();
$descriptor->path = $annotation->value;
}

/** @var list<ReflectionAttribute<PathPrefix>> $annotations */
$annotations = $classOrMethod->getAttributes(PathPrefix::class);
if (isset($annotations[0])) {
Expand Down
68 changes: 68 additions & 0 deletions src/Middleware/WhitespaceTrimmingMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Nekhay <[email protected]>
* @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);
}
}

0 comments on commit c3ecd03

Please sign in to comment.