Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 248bb13

Browse files
committed
Replace MiddlewareInterface implementation by ServerRequestHandlerInterface
It makes no sense implementing the middleware inteface as the Endpoint is not a middleware but a request handler.
1 parent c3ebe19 commit 248bb13

File tree

6 files changed

+65
-106
lines changed

6 files changed

+65
-106
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $endpoint->serve(
5050

5151
#### Optional: PSR15 Middleware
5252

53-
The endpoint class implements the [PSR15](https://www.php-fig.org/psr/psr-15/) `MiddlewareInterface`.
53+
The endpoint class implements the [PSR15](https://www.php-fig.org/psr/psr-15/) `RequestHandlerInterface`.
5454

5555
### MethodProvider
5656

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"php-http/discovery": "^1.13",
1717
"psr/http-factory": "^1.0",
1818
"psr/http-message": "^1.0",
19-
"psr/http-server-middleware": "^1.0",
19+
"psr/http-server-handler": "^1.0",
2020
"psr/log": "^1.1||^3",
2121
"ramsey/uuid": "^4.1",
2222
"shrikeh/teapot": "^2.3"

composer.lock

Lines changed: 1 addition & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Endpoint.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
use Http\Discovery\Psr17FactoryDiscovery;
88
use Opis\JsonSchema\Errors\ErrorFormatter;
99
use Opis\JsonSchema\Validator;
10+
use Psr\Http\Message\ResponseFactoryInterface;
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use Psr\Http\Message\StreamFactoryInterface;
13-
use Psr\Http\Server\RequestHandlerInterface;
1414
use Psr\Log\LoggerInterface;
1515
use Ramsey\Uuid\UuidFactory;
1616
use Ramsey\Uuid\UuidFactoryInterface;
1717
use Ramsey\Uuid\UuidInterface;
1818
use Teapot\StatusCode;
1919
use Throwable;
2020
use Usox\JsonSchemaApi\Contract\MethodProviderInterface;
21-
use Usox\JsonSchemaApi\Exception\ApiException;
22-
use Usox\JsonSchemaApi\Exception\InternalException;
2321
use Usox\JsonSchemaApi\Dispatch\MethodDispatcher;
2422
use Usox\JsonSchemaApi\Dispatch\MethodDispatcherInterface;
2523
use Usox\JsonSchemaApi\Dispatch\MethodValidator;
2624
use Usox\JsonSchemaApi\Dispatch\RequestValidator;
2725
use Usox\JsonSchemaApi\Dispatch\RequestValidatorInterface;
2826
use Usox\JsonSchemaApi\Dispatch\SchemaLoader;
27+
use Usox\JsonSchemaApi\Exception\ApiException;
28+
use Usox\JsonSchemaApi\Exception\InternalException;
2929
use Usox\JsonSchemaApi\Response\ResponseBuilder;
3030
use Usox\JsonSchemaApi\Response\ResponseBuilderInterface;
3131

@@ -41,16 +41,16 @@ public function __construct(
4141
private ResponseBuilderInterface $responseBuilder,
4242
private UuidFactoryInterface $uuidFactory,
4343
private StreamFactoryInterface $streamFactory,
44+
private ResponseFactoryInterface $responseFactory,
4445
private ?LoggerInterface $logger = null
4546
) {
4647
}
4748

4849
/**
49-
* Try to execute the api handler and build the response
50+
* Execute the api handler and build the response
5051
*/
5152
public function serve(
5253
ServerRequestInterface $request,
53-
ResponseInterface $response
5454
): ResponseInterface {
5555
$statusCode = StatusCode::OK;
5656
$responseData = null;
@@ -89,6 +89,8 @@ public function serve(
8989
$statusCode = StatusCode::INTERNAL_SERVER_ERROR;
9090
}
9191

92+
$response = $this->responseFactory->createResponse($statusCode);
93+
9294
if ($responseData !== null) {
9395
$response = $response->withBody(
9496
$this->streamFactory->createStream(
@@ -99,7 +101,7 @@ public function serve(
99101

100102
return $response
101103
->withHeader('Content-Type', 'application/json')
102-
->withStatus($statusCode);
104+
;
103105
}
104106

105107
/**
@@ -123,12 +125,13 @@ private function logError(
123125

124126
/**
125127
* Builds the endpoint.
126-
* The StreamFactory may be omitted, the endpoint will try to autodetect
127-
* an existing PSR17 implementations
128+
*
129+
* The factories may be omitted, the endpoint will try to autodetect existing PSR17 implementations
128130
*/
129131
public static function factory(
130132
MethodProviderInterface $methodProvider,
131133
?StreamFactoryInterface $streamFactory = null,
134+
?ResponseFactoryInterface $responseFactory = null,
132135
?LoggerInterface $logger = null
133136
): EndpointInterface {
134137
$schemaValidator = new Validator();
@@ -137,6 +140,9 @@ public static function factory(
137140
if ($streamFactory === null) {
138141
$streamFactory = Psr17FactoryDiscovery::findStreamFactory();
139142
}
143+
if ($responseFactory === null) {
144+
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
145+
}
140146

141147
return new self(
142148
new RequestValidator(
@@ -155,17 +161,16 @@ public static function factory(
155161
new ResponseBuilder(),
156162
new UuidFactory(),
157163
$streamFactory,
164+
$responseFactory,
158165
$logger
159166
);
160167
}
161168

162-
public function process(
169+
public function handle(
163170
ServerRequestInterface $request,
164-
RequestHandlerInterface $handler
165171
): ResponseInterface {
166172
return $this->serve(
167173
$request,
168-
$handler->handle($request)
169174
);
170175
}
171176
}

src/EndpointInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
use Psr\Http\Message\ResponseInterface;
66
use Psr\Http\Message\ServerRequestInterface;
7-
use Psr\Http\Server\MiddlewareInterface;
7+
use Psr\Http\Server\RequestHandlerInterface;
88

99
interface EndpointInterface extends
10-
MiddlewareInterface
10+
RequestHandlerInterface
1111
{
1212
public function serve(
1313
ServerRequestInterface $request,
14-
ResponseInterface $response
1514
): ResponseInterface;
1615
}

0 commit comments

Comments
 (0)