77use Http \Discovery \Psr17FactoryDiscovery ;
88use Opis \JsonSchema \Errors \ErrorFormatter ;
99use Opis \JsonSchema \Validator ;
10+ use Psr \Http \Message \ResponseFactoryInterface ;
1011use Psr \Http \Message \ResponseInterface ;
1112use Psr \Http \Message \ServerRequestInterface ;
1213use Psr \Http \Message \StreamFactoryInterface ;
13- use Psr \Http \Server \RequestHandlerInterface ;
1414use Psr \Log \LoggerInterface ;
1515use Ramsey \Uuid \UuidFactory ;
1616use Ramsey \Uuid \UuidFactoryInterface ;
1717use Ramsey \Uuid \UuidInterface ;
1818use Teapot \StatusCode ;
1919use Throwable ;
2020use Usox \JsonSchemaApi \Contract \MethodProviderInterface ;
21- use Usox \JsonSchemaApi \Exception \ApiException ;
22- use Usox \JsonSchemaApi \Exception \InternalException ;
2321use Usox \JsonSchemaApi \Dispatch \MethodDispatcher ;
2422use Usox \JsonSchemaApi \Dispatch \MethodDispatcherInterface ;
2523use Usox \JsonSchemaApi \Dispatch \MethodValidator ;
2624use Usox \JsonSchemaApi \Dispatch \RequestValidator ;
2725use Usox \JsonSchemaApi \Dispatch \RequestValidatorInterface ;
2826use Usox \JsonSchemaApi \Dispatch \SchemaLoader ;
27+ use Usox \JsonSchemaApi \Exception \ApiException ;
28+ use Usox \JsonSchemaApi \Exception \InternalException ;
2929use Usox \JsonSchemaApi \Response \ResponseBuilder ;
3030use 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}
0 commit comments