Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- `Pipe->endpoint()->{method}()` pattern
- `Innmind\Router\Handle\Proxy`

### Fixed

Expand Down
40 changes: 40 additions & 0 deletions proofs/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,44 @@ static function($assert, $url, $method, $other, $protocolVersion) {
);
},
);

yield proof(
'Handle::of() with a proxy',
given(
Set::of(...Http\Method::cases()),
Set::of(...Http\ProtocolVersion::cases()),
Set::of(...Http\Response\StatusCode::cases()),
),
static function($assert, $method, $protocolVersion, $status) {
$req = Http\ServerRequest::of(
Url::of('/foo'),
$method,
$protocolVersion,
);
$expected = Http\Response::of(
$status,
$req->protocolVersion(),
);
$router = Router::of(
Method::{$method->name}()
->map(Collect::of('method2'))
->pipe(Collect::merge(Endpoint::of('{/name}')))
->pipe(Handle::of(Handle\Proxy::of(
static fn() => static function($method2, $name, $request, $unknown = null) use ($req, $expected, $assert, $method) {
$assert->same($method, $method2);
$assert->same('foo', $name);
$assert->same($req, $request);
$assert->null($unknown);

return Attempt::result($expected);
},
))),
);

$assert->same(
$expected,
$router($req)->unwrap(),
);
},
);
};
8 changes: 6 additions & 2 deletions src/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ public static function via(callable $handler): Component
/**
* @psalm-pure
*
* @param callable(...mixed): Attempt<Response> $handler
* @param Handle\Proxy|(callable(mixed...): Attempt<Response>) $handler
*
* @return Component<Map<string, mixed>, Response>
*/
#[\NoDiscard]
public static function of(callable $handler): Component
public static function of(Handle\Proxy|callable $handler): Component
{
/** @var Component<Map<string, mixed>, Response> */
return self::via(
static function($request, Map $variables) use ($handler) {
if ($handler instanceof Handle\Proxy) {
$handler = $handler->unwrap();
}

if (!$variables->contains('request')) {
$variables = ($variables)('request', $request);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Handle/Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types = 1);

namespace Innmind\Router\Handle;

use Innmind\Http\Response;
use Innmind\Immutable\Attempt;

final class Proxy
{
/**
* @param \Closure(): (callable(mixed...): Attempt<Response>) $load
*/
private function __construct(
private \Closure $load,
) {
}

/**
* @param callable(): (callable(mixed...): Attempt<Response>) $load
*/
public static function of(callable $load): self
{
return new self(\Closure::fromCallable($load));
}

/**
* @return callable(...mixed): Attempt<Response>
*/
public function unwrap(): callable
{
return ($this->load)();
}
}
4 changes: 2 additions & 2 deletions src/Pipe/Endpoint/Method/Spread.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public static function of(Component $previous): self
}

/**
* @param callable(...mixed): Attempt<Http\Response> $handle
* @param Handle\Proxy|(callable(mixed...): Attempt<Http\Response>) $handle
*
* @return Component<mixed, Http\Response>
*/
#[\NoDiscard]
public function handle(callable $handle): Component
public function handle(Handle\Proxy|callable $handle): Component
{
/** @psalm-suppress MixedArgumentTypeCoercion */
return $this->previous->feed(Handle::of($handle));
Expand Down
4 changes: 2 additions & 2 deletions src/Pipe/Forward/Method/Spread.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public static function of(Component $previous): self
}

/**
* @param callable(...mixed): Attempt<Http\Response> $handle
* @param Handle\Proxy|(callable(mixed...): Attempt<Http\Response>) $handle
*
* @return Component<Map<string, string>, Http\Response>
*/
#[\NoDiscard]
public function handle(callable $handle): Component
public function handle(Handle\Proxy|callable $handle): Component
{
/** @psalm-suppress MixedArgumentTypeCoercion */
return $this->previous->feed(Handle::of($handle));
Expand Down
4 changes: 2 additions & 2 deletions src/Pipe/Method/Endpoint/Spread.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public static function of(
}

/**
* @param callable(...mixed): Attempt<Response> $handle
* @param Handle\Proxy|(callable(mixed...): Attempt<Response>) $handle
*
* @return Component<mixed, Response>
*/
#[\NoDiscard]
public function handle(callable $handle): Component
public function handle(Handle\Proxy|callable $handle): Component
{
/** @psalm-suppress MixedArgumentTypeCoercion Don't know why it complains */
return $this
Expand Down