7
7
use Http \Discovery \Psr17FactoryDiscovery ;
8
8
use Opis \JsonSchema \Errors \ErrorFormatter ;
9
9
use Opis \JsonSchema \Validator ;
10
+ use Psr \Http \Message \ResponseFactoryInterface ;
10
11
use Psr \Http \Message \ResponseInterface ;
11
12
use Psr \Http \Message \ServerRequestInterface ;
12
13
use Psr \Http \Message \StreamFactoryInterface ;
13
- use Psr \Http \Server \RequestHandlerInterface ;
14
14
use Psr \Log \LoggerInterface ;
15
15
use Ramsey \Uuid \UuidFactory ;
16
16
use Ramsey \Uuid \UuidFactoryInterface ;
17
17
use Ramsey \Uuid \UuidInterface ;
18
18
use Teapot \StatusCode ;
19
19
use Throwable ;
20
20
use Usox \JsonSchemaApi \Contract \MethodProviderInterface ;
21
- use Usox \JsonSchemaApi \Exception \ApiException ;
22
- use Usox \JsonSchemaApi \Exception \InternalException ;
23
21
use Usox \JsonSchemaApi \Dispatch \MethodDispatcher ;
24
22
use Usox \JsonSchemaApi \Dispatch \MethodDispatcherInterface ;
25
23
use Usox \JsonSchemaApi \Dispatch \MethodValidator ;
26
24
use Usox \JsonSchemaApi \Dispatch \RequestValidator ;
27
25
use Usox \JsonSchemaApi \Dispatch \RequestValidatorInterface ;
28
26
use Usox \JsonSchemaApi \Dispatch \SchemaLoader ;
27
+ use Usox \JsonSchemaApi \Exception \ApiException ;
28
+ use Usox \JsonSchemaApi \Exception \InternalException ;
29
29
use Usox \JsonSchemaApi \Response \ResponseBuilder ;
30
30
use Usox \JsonSchemaApi \Response \ResponseBuilderInterface ;
31
31
@@ -41,16 +41,16 @@ public function __construct(
41
41
private ResponseBuilderInterface $ responseBuilder ,
42
42
private UuidFactoryInterface $ uuidFactory ,
43
43
private StreamFactoryInterface $ streamFactory ,
44
+ private ResponseFactoryInterface $ responseFactory ,
44
45
private ?LoggerInterface $ logger = null
45
46
) {
46
47
}
47
48
48
49
/**
49
- * Try to execute the api handler and build the response
50
+ * Execute the api handler and build the response
50
51
*/
51
52
public function serve (
52
53
ServerRequestInterface $ request ,
53
- ResponseInterface $ response
54
54
): ResponseInterface {
55
55
$ statusCode = StatusCode::OK ;
56
56
$ responseData = null ;
@@ -89,6 +89,8 @@ public function serve(
89
89
$ statusCode = StatusCode::INTERNAL_SERVER_ERROR ;
90
90
}
91
91
92
+ $ response = $ this ->responseFactory ->createResponse ($ statusCode );
93
+
92
94
if ($ responseData !== null ) {
93
95
$ response = $ response ->withBody (
94
96
$ this ->streamFactory ->createStream (
@@ -99,7 +101,7 @@ public function serve(
99
101
100
102
return $ response
101
103
->withHeader ('Content-Type ' , 'application/json ' )
102
- -> withStatus ( $ statusCode ) ;
104
+ ;
103
105
}
104
106
105
107
/**
@@ -123,12 +125,13 @@ private function logError(
123
125
124
126
/**
125
127
* 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
128
130
*/
129
131
public static function factory (
130
132
MethodProviderInterface $ methodProvider ,
131
133
?StreamFactoryInterface $ streamFactory = null ,
134
+ ?ResponseFactoryInterface $ responseFactory = null ,
132
135
?LoggerInterface $ logger = null
133
136
): EndpointInterface {
134
137
$ schemaValidator = new Validator ();
@@ -137,6 +140,9 @@ public static function factory(
137
140
if ($ streamFactory === null ) {
138
141
$ streamFactory = Psr17FactoryDiscovery::findStreamFactory ();
139
142
}
143
+ if ($ responseFactory === null ) {
144
+ $ responseFactory = Psr17FactoryDiscovery::findResponseFactory ();
145
+ }
140
146
141
147
return new self (
142
148
new RequestValidator (
@@ -155,17 +161,16 @@ public static function factory(
155
161
new ResponseBuilder (),
156
162
new UuidFactory (),
157
163
$ streamFactory ,
164
+ $ responseFactory ,
158
165
$ logger
159
166
);
160
167
}
161
168
162
- public function process (
169
+ public function handle (
163
170
ServerRequestInterface $ request ,
164
- RequestHandlerInterface $ handler
165
171
): ResponseInterface {
166
172
return $ this ->serve (
167
173
$ request ,
168
- $ handler ->handle ($ request )
169
174
);
170
175
}
171
176
}
0 commit comments