Skip to content

Commit e0ebf3b

Browse files
committed
Moved Middleware to Pipe creation to Middleware class.
1 parent 633f543 commit e0ebf3b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

Src/PipelineBridge.php

+1-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use Psr\Http\Server\MiddlewareInterface;
1111
use TheWebSolver\Codegarage\Lib\Psr\Middleware;
1212
use Psr\Http\Message\ResponseInterface as Response;
13-
use TheWebSolver\Codegarage\Lib\Psr\RequestHandler;
1413
use Psr\Http\Message\ServerRequestInterface as Request;
15-
use Psr\Http\Server\RequestHandlerInterface as Handler;
1614
use TheWebSolver\Codegarage\Lib\Interfaces\PipeInterface;
1715

1816
class PipelineBridge {
@@ -82,19 +80,6 @@ public static function middlewareToPipe(
8280
string|Closure|MiddlewareInterface $handler,
8381
?ContainerInterface $container = null
8482
): PipeInterface {
85-
return Pipe::create(
86-
static fn ( Response $subject, Closure $next, Request $request, mixed ...$args ) => $next(
87-
Middleware::create( $handler, $container )
88-
->process( $request, self::withHandler( $subject, reset( $args ) ) )
89-
)
90-
);
91-
}
92-
93-
private static function withHandler( Response $response, mixed $arg ): Handler {
94-
$handler = is_string( $arg ) ? new $arg( $response ) : new RequestHandler( $response );
95-
96-
return $handler instanceof Handler
97-
? $handler
98-
: throw new LogicException( 'Invalid Request Handler provided for pipeline: ' . $arg );
83+
return Middleware::toPipe( $handler, $container );
9984
}
10085
}

Src/Psr/Middleware.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
use Closure;
77
use Throwable;
8+
use LogicException;
89
use Psr\Container\ContainerInterface;
9-
use Psr\Http\Message\ResponseInterface;
10+
use TheWebSolver\Codegarage\Lib\Pipe;
1011
use Psr\Http\Server\MiddlewareInterface;
1112
use TheWebSolver\Codegarage\Lib\Resolver;
12-
use Psr\Http\Message\ServerRequestInterface;
13-
use Psr\Http\Server\RequestHandlerInterface;
13+
use Psr\Http\Message\ResponseInterface as Response;
14+
use Psr\Http\Message\ServerRequestInterface as Request;
15+
use Psr\Http\Server\RequestHandlerInterface as Handler;
16+
use TheWebSolver\Codegarage\Lib\Interfaces\PipeInterface;
1417
use TheWebSolver\Codegarage\Lib\Error\InvalidMiddlewareForPipe;
1518

1619
class Middleware implements MiddlewareInterface {
@@ -19,11 +22,11 @@ class Middleware implements MiddlewareInterface {
1922

2023
private const RESOLVER_TYPES = array( MiddlewareInterface::class, InvalidMiddlewareForPipe::class );
2124

22-
/** @param Closure(ServerRequestInterface, RequestHandlerInterface): ResponseInterface $middleware */
25+
/** @param Closure(Request, Handler): Response $middleware */
2326
// phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
2427
public function __construct( private readonly Closure $middleware ) {}
2528

26-
public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface {
29+
public function process( Request $request, Handler $handler ): Response {
2730
return ( $this->middleware )( $request, $handler );
2831
}
2932

@@ -38,4 +41,24 @@ public static function create(
3841
throw new InvalidMiddlewareForPipe( $e->getMessage(), $e->getCode(), $e );
3942
}
4043
}
44+
45+
public static function toPipe(
46+
string|Closure|MiddlewareInterface $handler,
47+
?ContainerInterface $container = null
48+
): PipeInterface {
49+
return Pipe::create(
50+
static fn ( Response $subject, Closure $next, Request $request, mixed ...$args ) => $next(
51+
self::create( $handler, $container )
52+
->process( $request, self::withHandler( $subject, reset( $args ) ) )
53+
)
54+
);
55+
}
56+
57+
private static function withHandler( Response $response, mixed $arg ): Handler {
58+
$handler = is_string( $arg ) ? new $arg( $response ) : new RequestHandler( $response );
59+
60+
return $handler instanceof Handler
61+
? $handler
62+
: throw new LogicException( 'Invalid Request Handler provided for pipeline: ' . $arg );
63+
}
4164
}

0 commit comments

Comments
 (0)