@@ -51,18 +51,15 @@ public function handle( mixed $subject, Closure $next, mixed ...$use ): mixed {
51
51
52
52
/**
53
53
* @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.
56
55
*/
57
56
public static function toMiddleware ( mixed $ middleware ): object {
58
57
$ interface = static ::hasMiddlewareInterfaceAdapter ()
59
58
? static ::$ middlewareInterface
60
59
: '\\Psr \\Http \\Server \\MiddlewareInterface ' ;
61
60
62
61
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. ' );
66
63
}
67
64
68
65
$ provided = $ middleware ;
@@ -83,35 +80,34 @@ public static function toMiddleware( mixed $middleware ): object {
83
80
'Non-existing class "%1$s". Middleware must be a Closure, an instance of %2$s '
84
81
. ' or classname of a class that implements %2$s. ' ,
85
82
$ provided ,
86
- static :: $ middlewareInterface
83
+ $ interface
87
84
)
88
85
);
89
86
}
90
87
91
88
return static ::getMiddlewareAdapter ( $ middleware );
92
89
} catch ( Throwable $ e ) {
93
- if ( ! is_string ( $ middleware ) ) {
90
+ if ( $ e instanceof TypeError || ! is_string ( $ middleware ) ) {
94
91
throw $ e ;
95
92
}
96
93
97
94
throw new TypeError (
98
95
sprintf (
99
96
'The given middleware classname: "%1$s" must be an instance of "%2$s". ' ,
100
97
$ middleware ,
101
- static :: $ middlewareInterface
98
+ $ interface
102
99
)
103
100
);
104
101
}//end try
105
102
}
106
103
107
- /** @param string|Closure|Middleware $middleware */
108
- public static function middlewareToPipe ( $ middleware ): Pipe {
104
+ public static function middlewareToPipe ( mixed $ middleware ): Pipe {
109
105
// Because Pipe::handle() wraps this function with the next pipe, we do not need to...
110
106
// ...manually wrap middleware with $next & let createPipe take care of it.
111
107
// If we do so, same middleware will recreate response multiple times
112
108
// 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 )
115
111
->process ( $ request ->withAttribute ( static ::MIDDLEWARE_RESPONSE , $ r ), $ handler )
116
112
);
117
113
}
0 commit comments