Skip to content

Commit 49ced4d

Browse files
committed
Enhancement & bug fixes.
1 parent d81c13a commit 49ced4d

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

Src/PipelineBridge.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,15 @@ public function handle( mixed $subject, Closure $next, mixed ...$use ): mixed {
5151

5252
/**
5353
* @throws LogicException When invoked on projects that doesn't implement PSR7 & PSR15.
54-
* @throws TypeError When middleware creation fails due to invalid classname.
55-
* @throws Throwable When unknown error occurs.
54+
* @throws TypeError When middleware creation fails due to invalid classname.
5655
*/
5756
public static function toMiddleware( mixed $middleware ): object {
5857
$interface = static::hasMiddlewareInterfaceAdapter()
5958
? static::$middlewareInterface
6059
: '\\Psr\\Http\\Server\\MiddlewareInterface';
6160

6261
if ( ! interface_exists( $interface ) ) {
63-
throw new LogicException(
64-
'Project does not use implementation of PSR15 HTTP Server Middleware.'
65-
);
62+
throw new LogicException( 'Cannot find implementation of PSR15 HTTP Server Middleware.' );
6663
}
6764

6865
$provided = $middleware;
@@ -83,35 +80,34 @@ public static function toMiddleware( mixed $middleware ): object {
8380
'Non-existing class "%1$s". Middleware must be a Closure, an instance of %2$s'
8481
. ' or classname of a class that implements %2$s.',
8582
$provided,
86-
static::$middlewareInterface
83+
$interface
8784
)
8885
);
8986
}
9087

9188
return static::getMiddlewareAdapter( $middleware );
9289
} catch ( Throwable $e ) {
93-
if ( ! is_string( $middleware ) ) {
90+
if ( $e instanceof TypeError || ! is_string( $middleware ) ) {
9491
throw $e;
9592
}
9693

9794
throw new TypeError(
9895
sprintf(
9996
'The given middleware classname: "%1$s" must be an instance of "%2$s".',
10097
$middleware,
101-
static::$middlewareInterface
98+
$interface
10299
)
103100
);
104101
}//end try
105102
}
106103

107-
/** @param string|Closure|Middleware $middleware */
108-
public static function middlewareToPipe( $middleware ): Pipe {
104+
public static function middlewareToPipe( mixed $middleware ): Pipe {
109105
// Because Pipe::handle() wraps this function with the next pipe, we do not need to...
110106
// ...manually wrap middleware with $next & let createPipe take care of it.
111107
// If we do so, same middleware will recreate response multiple times
112108
// making our app less performant which we don't want at all cost.
113-
return self::toPipe(
114-
static fn ( $r, $next, $request, $handler ) => self::toMiddleware( $middleware )
109+
return static::toPipe(
110+
static fn ( $r, $next, $request, $handler ) => static::toMiddleware( $middleware )
115111
->process( $request->withAttribute( static::MIDDLEWARE_RESPONSE, $r ), $handler )
116112
);
117113
}

0 commit comments

Comments
 (0)