1515
1616namespace Symfony \Component \HttpKernel ;
1717
18+ use Exception ;
1819use React \Promise \FulfilledPromise ;
1920use React \Promise \PromiseInterface ;
2021use React \Promise \RejectedPromise ;
2728use Symfony \Component \HttpKernel \Controller \ControllerResolverInterface ;
2829use Symfony \Component \HttpKernel \Event \FilterControllerArgumentsEvent ;
2930use Symfony \Component \HttpKernel \Event \FilterControllerEvent ;
30- use Symfony \Component \HttpKernel \Event \FilterResponsePromiseEvent ;
31+ use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
3132use Symfony \Component \HttpKernel \Event \FinishRequestEvent ;
3233use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
33- use Symfony \Component \HttpKernel \Event \GetResponsePromiseEvent ;
34- use Symfony \Component \HttpKernel \Event \GetResponsePromiseForExceptionEvent ;
35- use Symfony \Component \HttpKernel \Event \PromiseEvent ;
34+ use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
3635use Symfony \Component \HttpKernel \Exception \AsyncEventDispatcherNeededException ;
3736use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
3837use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
3938use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
40- use Throwable ;
4139
4240/**
4341 * Class AsyncHttpKernel.
@@ -115,7 +113,7 @@ public function handleAsync(
115113 $ type
116114 )
117115 ->then (null ,
118- function (Throwable $ exception ) use ($ request , $ type , $ catch ) {
116+ function (Exception $ exception ) use ($ request , $ type , $ catch ) {
119117 if ($ exception instanceof RequestExceptionInterface) {
120118 $ exception = new BadRequestHttpException ($ exception ->getMessage (), $ exception );
121119 }
@@ -151,36 +149,19 @@ private function handleAsyncRaw(
151149 ): PromiseInterface {
152150 $ dispatcher = $ this ->dispatcher ;
153151
154- // request
155- $ event = new GetResponseEvent ($ this , $ request , $ type );
156- $ this ->dispatcher ->dispatch (KernelEvents::REQUEST , $ event );
157-
158- if ($ event ->hasResponse ()) {
159- return $ this
160- ->filterResponsePromise (new FulfilledPromise ($ event ->getResponse ()), $ request , $ type );
161- }
162-
163152 $ this ->requestStack ->push ($ request );
164- $ event = new GetResponsePromiseEvent ($ this , $ request , $ type );
153+ $ event = new GetResponseEvent ($ this , $ request , $ type );
165154
166155 return $ dispatcher
167- ->asyncDispatch (AsyncKernelEvents::ASYNC_REQUEST , $ event )
168- ->then (function (PromiseEvent $ event ) use ($ request , $ type ) {
169- if ($ event ->hasPromise ()) {
170- return $ event
171- ->getPromise ()
172- ->then (function ($ response ) use ($ request , $ type ) {
173- return $ response instanceof Response
174- ? $ this ->filterResponsePromise (
175- new FulfilledPromise ($ response ),
176- $ request ,
177- $ type
178- )
179- : $ this ->callAsyncController ($ request , $ type );
180- });
181- }
182-
183- return $ this ->callAsyncController ($ request , $ type );
156+ ->asyncDispatch (KernelEvents::REQUEST , $ event )
157+ ->then (function (GetResponseEvent $ event ) use ($ request , $ type ) {
158+ return $ event ->hasResponse ()
159+ ? $ this ->filterResponsePromise (
160+ $ event ->getResponse (),
161+ $ request ,
162+ $ type
163+ )
164+ : $ this ->callAsyncController ($ request , $ type );
184165 });
185166 }
186167
@@ -212,40 +193,37 @@ private function callAsyncController(Request $request, int $type): PromiseInterf
212193 $ controller = $ event ->getController ();
213194 $ arguments = $ event ->getArguments ();
214195
215- /**
216- * Call controller.
217- *
218- * @var PromiseInterface
219- */
220- $ promise = $ controller (...$ arguments );
221-
222- return $ this ->filterResponsePromise ($ promise , $ request , $ type );
196+ return (new FulfilledPromise ())
197+ ->then (function () use ($ controller , $ arguments ) {
198+ return $ controller (...$ arguments );
199+ })
200+ ->then (function ($ response ) use ($ request , $ type ) {
201+ return $ this ->filterResponsePromise ($ response , $ request , $ type );
202+ });
223203 }
224204
225205 /**
226206 * Filters a response object.
227207 *
228- * @param PromiseInterface $promise
229- * @param Request $request
230- * @param int $type
208+ * @param Response $response
209+ * @param Request $request
210+ * @param int $type
231211 *
232212 * @return PromiseInterface
233213 *
234214 * @throws \RuntimeException if the passed object is not a Response instance
235215 */
236- private function filterResponsePromise (PromiseInterface $ promise , Request $ request , int $ type )
216+ private function filterResponsePromise (Response $ response , Request $ request , int $ type )
237217 {
238- $ event = new FilterResponsePromiseEvent ($ this , $ request , $ type , $ promise );
218+ $ event = new FilterResponseEvent ($ this , $ request , $ type , $ response );
239219
240220 return $ this
241221 ->dispatcher
242- ->asyncDispatch (AsyncKernelEvents:: ASYNC_RESPONSE , $ event )
243- ->then (function (PromiseEvent $ event ) use ($ request , $ type, $ promise ) {
222+ ->asyncDispatch (KernelEvents:: RESPONSE , $ event )
223+ ->then (function (FilterResponseEvent $ event ) use ($ request , $ type ) {
244224 $ this ->finishRequestPromise ($ request , $ type );
245225
246- return $ event ->hasPromise ()
247- ? $ event ->getPromise ()
248- : $ promise ;
226+ return $ event ->getResponse ();
249227 });
250228 }
251229
@@ -269,50 +247,48 @@ private function finishRequestPromise(Request $request, int $type)
269247 /**
270248 * Handles an exception by trying to convert it to a Response.
271249 *
272- * @param Throwable $exception
250+ * @param Exception $exception
273251 * @param Request $request
274252 * @param int $type
275253 *
276254 * @return PromiseInterface
277255 *
278- * @throws \Throwable
256+ * @throws Exception
279257 */
280258 private function handleExceptionPromise (
281- Throwable $ exception ,
259+ Exception $ exception ,
282260 Request $ request ,
283261 int $ type
284262 ): PromiseInterface {
285- $ event = new GetResponsePromiseForExceptionEvent ($ this , $ request , $ type , $ exception );
286- $ promise = $ this
263+ $ event = new GetResponseForExceptionEvent ($ this , $ request , $ type , $ exception );
264+
265+ return $ this
287266 ->dispatcher
288- ->asyncDispatch (AsyncKernelEvents:: ASYNC_EXCEPTION , $ event )
289- ->then (function (GetResponsePromiseForExceptionEvent $ event ) use ($ request , $ type ) {
267+ ->asyncDispatch (KernelEvents:: EXCEPTION , $ event )
268+ ->then (function (GetResponseForExceptionEvent $ event ) use ($ request , $ type ) {
290269 $ exception = $ event ->getException ();
291- if (!$ event ->hasPromise ()) {
270+ if (!$ event ->hasResponse ()) {
292271 $ this ->finishRequestPromise ($ request , $ type );
293272
294273 throw $ event ->getException ();
295274 } else {
296- return $ event
297- ->getPromise ()
298- ->then (function (Response $ response ) use ($ request , $ type , $ event , $ exception ) {
299- // the developer asked for a specific status code
300- if (!$ event ->isAllowingCustomResponseCode () && !$ response ->isClientError () && !$ response ->isServerError () && !$ response ->isRedirect ()) {
301- // ensure that we actually have an error response
302- if ($ exception instanceof HttpExceptionInterface) {
303- // keep the HTTP status code and headers
304- $ response ->setStatusCode ($ exception ->getStatusCode ());
305- $ response ->headers ->add ($ exception ->getHeaders ());
306- } else {
307- $ response ->setStatusCode (500 );
308- }
309- }
275+ $ response = $ event ->getResponse ();
276+ if (!$ event ->isAllowingCustomResponseCode () && !$ response ->isClientError () && !$ response ->isServerError () && !$ response ->isRedirect ()) {
277+ // ensure that we actually have an error response
278+ if ($ exception instanceof HttpExceptionInterface) {
279+ // keep the HTTP status code and headers
280+ $ response ->setStatusCode ($ exception ->getStatusCode ());
281+ $ response ->headers ->add ($ exception ->getHeaders ());
282+ } else {
283+ $ response ->setStatusCode (500 );
284+ }
285+ }
310286
311- return $ response ;
312- });
287+ return $ response ;
313288 }
289+ })
290+ ->then (function (Response $ response ) use ($ request , $ type ) {
291+ return $ this ->filterResponsePromise ($ response , $ request , $ type );
314292 });
315-
316- return $ this ->filterResponsePromise ($ promise , $ request , $ type );
317293 }
318294}
0 commit comments