* function ( * ServerRequestInterface $request, * RequestHandlerInterface $handler * ) : ResponseInterface * * * such that it will operate as PSR-15 middleware. * * Neither the arguments nor the return value need be typehinted; however, if * the signature is incompatible, a PHP Error will likely be thrown. */ final class CallableMiddlewareDecorator implements MiddlewareInterface { /** @var callable */ private $middleware; public function __construct(callable $middleware) { $this->middleware = $middleware; } /** * {@inheritDoc} * * @throws Exception\MissingResponseException If the decorated middleware * fails to produce a response. */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = ($this->middleware)($request, $handler); if (! $response instanceof ResponseInterface) { throw Exception\MissingResponseException::forCallableMiddleware($this->middleware); } return $response; } }