diff --git a/src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php b/src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php index 846182b3..92748529 100644 --- a/src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php +++ b/src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php @@ -43,8 +43,8 @@ final class OAuthProxyMiddleware implements MiddlewareInterface private const CLIENT_SECRET_BASIC = 'client_secret_basic'; private const CLIENT_SECRET_POST = 'client_secret_post'; - private ?ClientInterface $httpClient; - private ?RequestFactoryInterface $requestFactory; + private ClientInterface $httpClient; + private RequestFactoryInterface $requestFactory; private ResponseFactoryInterface $responseFactory; private StreamFactoryInterface $streamFactory; @@ -64,8 +64,8 @@ public function __construct( ?ResponseFactoryInterface $responseFactory = null, ?StreamFactoryInterface $streamFactory = null, ) { - $this->httpClient = $httpClient; - $this->requestFactory = $requestFactory; + $this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); $this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory(); $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); } @@ -143,7 +143,7 @@ private function handleToken(ServerRequestInterface $request): ResponseInterface $body = http_build_query($params); - $upstreamRequest = $this->getRequestFactory() + $upstreamRequest = $this->requestFactory ->createRequest('POST', $tokenEndpoint) ->withHeader('Content-Type', 'application/x-www-form-urlencoded') ->withBody($this->streamFactory->createStream($body)); @@ -153,7 +153,7 @@ private function handleToken(ServerRequestInterface $request): ResponseInterface } try { - $upstreamResponse = $this->getHttpClient()->sendRequest($upstreamRequest); + $upstreamResponse = $this->httpClient->sendRequest($upstreamRequest); $responseBody = $upstreamResponse->getBody()->__toString(); return $this->responseFactory @@ -260,14 +260,4 @@ private function getTokenEndpointAuthMethods(): array return array_values(array_unique($normalized)); } - - private function getHttpClient(): ClientInterface - { - return $this->httpClient ??= Psr18ClientDiscovery::find(); - } - - private function getRequestFactory(): RequestFactoryInterface - { - return $this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory(); - } } diff --git a/src/Server/Transport/Http/Middleware/OAuthRequestMetaMiddleware.php b/src/Server/Transport/Http/Middleware/OAuthRequestMetaMiddleware.php index 9301352e..d1595a58 100644 --- a/src/Server/Transport/Http/Middleware/OAuthRequestMetaMiddleware.php +++ b/src/Server/Transport/Http/Middleware/OAuthRequestMetaMiddleware.php @@ -28,9 +28,11 @@ */ final class OAuthRequestMetaMiddleware implements MiddlewareInterface { - public function __construct( - private ?StreamFactoryInterface $streamFactory = null, - ) { + private StreamFactoryInterface $streamFactory; + + public function __construct(?StreamFactoryInterface $streamFactory = null) + { + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface @@ -66,7 +68,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface return $handler->handle($request); } - $request = $request->withBody($this->getStreamFactory()->createStream($updatedBody)); + $request = $request->withBody($this->streamFactory->createStream($updatedBody)); return $handler->handle($request); } @@ -141,9 +143,4 @@ private function injectIntoMessage(array $message, array $oauthMeta): array return $message; } - - private function getStreamFactory(): StreamFactoryInterface - { - return $this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory(); - } } diff --git a/src/Server/Transport/Http/MiddlewareRequestHandler.php b/src/Server/Transport/Http/MiddlewareRequestHandler.php index 7d371626..32ad3725 100644 --- a/src/Server/Transport/Http/MiddlewareRequestHandler.php +++ b/src/Server/Transport/Http/MiddlewareRequestHandler.php @@ -24,24 +24,25 @@ * * @internal */ -class MiddlewareRequestHandler implements RequestHandlerInterface +final class MiddlewareRequestHandler implements RequestHandlerInterface { + private int $index = 0; + /** * @param list $middleware */ public function __construct( - private array $middleware, - private \Closure $application, + private readonly array $middleware, + private readonly \Closure $application, ) { } public function handle(ServerRequestInterface $request): ResponseInterface { - $middleware = array_shift($this->middleware); - if (null === $middleware) { + if (!isset($this->middleware[$this->index])) { return ($this->application)($request); } - return $middleware->process($request, $this); + return $this->middleware[$this->index++]->process($request, $this); } }