3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** * Даны два разных механизма расширения обработки Http-сообщений: * один в стиле PSR-7, другой — в стиле Symfony HttpFoundation. * * Нужно написать класс, который позволит интегрировать * подписчик от Symfony в PSR фреймворк. * * Всё необходимое для решения задачи есть в этом файле. */ namespace Psr; /** * Request иммутабельный, как в реальном PSR-7. * Для простоты оставим только тело запроса. */ final readonly class Request { public function __construct( public string $body, ) {} } /** * Для простоты представим, что Response — это любое mixed значение. */ interface RequestHandler { public function handle(Request $request): mixed; } interface Middleware { public function process(Request $request, RequestHandler $handler): mixed; } namespace Symfony; /** * Здесь уже Request мутабельный, как в реальном Symfony HttpFoundation. */ final class Request { public function __construct( public string $body, ) {} } final readonly class RequestEvent { public function __construct( public Request $request, ) {} } /** * Тут Response тоже представлен любым mixed значением. * Обрати внимание, что $response можно менять на объекте ивента. */ final class ResponseEvent { public function __construct( public readonly Request $request, public mixed $response, ) {} } interface Subscriber { public function onRequest(RequestEvent $event): void; public function onResponse(ResponseEvent $event): void; } namespace Solution; use Psr\Middleware; use Psr\Request as PsrRequest; use Psr\RequestHandler; use Symfony\RequestEvent; use Symfony\ResponseEvent; use Symfony\Subscriber; use Symfony\Request as SymfonyRequest; final readonly class FiberRequestHandler implements RequestHandler { public function handle(PsrRequest $request): mixed { return \Fiber::suspend($request); } } final class MiddlewareToSubscriber implements Subscriber { /** * @var \WeakMap<SymfonyRequest, \Fiber> */ private \WeakMap $requestFibers; public function __construct( private readonly Middleware $middleware, ) { /** @var \WeakMap<SymfonyRequest, \Fiber> */ $this->requestFibers = new \WeakMap(); } public function onRequest(RequestEvent $event): void { $fiber = new \Fiber(fn(): mixed => $this->middleware->process(new PsrRequest($event->request->body), new FiberRequestHandler())); $newRequest = $fiber->start(); $event->request->body = $newRequest->body; $this->requestFibers[$event->request] = $fiber; } public function onResponse(ResponseEvent $event): void { $fiber = $this->requestFibers[$event->request]; $fiber->resume($event->response); $event->response = $fiber->getReturn(); unset($this->requestFibers[$event->request]); } } final class DumpMiddleware implements Middleware { public function process(PsrRequest $request, RequestHandler $handler): mixed { var_dump($request); $response = $handler->handle($request); var_dump($response); return 'modified response'; } } $subscriber = new MiddlewareToSubscriber(new DumpMiddleware()); $requestEvent = new RequestEvent(new SymfonyRequest('body')); $subscriber->onRequest($requestEvent); var_dump('Handling request'); $responseEvent = new ResponseEvent($requestEvent->request, 'response'); $subscriber->onResponse($responseEvent); var_dump($responseEvent->response);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  (null)
number of ops:  38
compiled vars:  !0 = $subscriber, !1 = $requestEvent, !2 = $responseEvent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   DECLARE_CLASS                                            'solution%5Cfiberrequesthandler'
   96     1        DECLARE_CLASS                                            'solution%5Cmiddlewaretosubscriber'
  127     2        DECLARE_CLASS                                            'solution%5Cdumpmiddleware'
  141     3        NEW                                              $3      'Solution%5CMiddlewareToSubscriber'
          4        NEW                                              $4      'Solution%5CDumpMiddleware'
          5        DO_FCALL                                      0          
          6        SEND_VAR_NO_REF_EX                                       $4
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !0, $3
  142     9        NEW                                              $8      'Symfony%5CRequestEvent'
         10        NEW                                              $9      'Symfony%5CRequest'
         11        SEND_VAL_EX                                              'body'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $9
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !1, $8
  143    16        INIT_METHOD_CALL                                         !0, 'onRequest'
         17        SEND_VAR_EX                                              !1
         18        DO_FCALL                                      0          
  144    19        INIT_NS_FCALL_BY_NAME                                    'Solution%5Cvar_dump'
         20        SEND_VAL_EX                                              'Handling+request'
         21        DO_FCALL                                      0          
  145    22        NEW                                              $15     'Symfony%5CResponseEvent'
         23        CHECK_FUNC_ARG                                           
         24        FETCH_OBJ_FUNC_ARG                               $16     !1, 'request'
         25        SEND_FUNC_ARG                                            $16
         26        SEND_VAL_EX                                              'response'
         27        DO_FCALL                                      0          
         28        ASSIGN                                                   !2, $15
  146    29        INIT_METHOD_CALL                                         !0, 'onResponse'
         30        SEND_VAR_EX                                              !2
         31        DO_FCALL                                      0          
  147    32        INIT_NS_FCALL_BY_NAME                                    'Solution%5Cvar_dump'
         33        CHECK_FUNC_ARG                                           
         34        FETCH_OBJ_FUNC_ARG                               $20     !2, 'response'
         35        SEND_FUNC_ARG                                            $20
         36        DO_FCALL                                      0          
         37      > RETURN                                                   1

Class Psr\Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  __construct
number of ops:  4
compiled vars:  !0 = $body
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'body'
          2        OP_DATA                                                  !0
   24     3      > RETURN                                                   null

End of function __construct

End of class Psr\Request.

Class Psr\RequestHandler:
Function handle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  handle
number of ops:  3
compiled vars:  !0 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function handle

End of class Psr\RequestHandler.

Class Psr\Middleware:
Function process:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  process
number of ops:  4
compiled vars:  !0 = $request, !1 = $handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        VERIFY_RETURN_TYPE                                       
          3      > RETURN                                                   null

End of function process

End of class Psr\Middleware.

Class Symfony\Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  __construct
number of ops:  4
compiled vars:  !0 = $body
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'body'
          2        OP_DATA                                                  !0
   49     3      > RETURN                                                   null

End of function __construct

End of class Symfony\Request.

Class Symfony\RequestEvent:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  __construct
number of ops:  4
compiled vars:  !0 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'request'
          2        OP_DATA                                                  !0
   56     3      > RETURN                                                   null

End of function __construct

End of class Symfony\RequestEvent.

Class Symfony\ResponseEvent:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  __construct
number of ops:  7
compiled vars:  !0 = $request, !1 = $response
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        ASSIGN_OBJ                                               'request'
          3        OP_DATA                                                  !0
          4        ASSIGN_OBJ                                               'response'
          5        OP_DATA                                                  !1
   68     6      > RETURN                                                   null

End of function __construct

End of class Symfony\ResponseEvent.

Class Symfony\Subscriber:
Function onrequest:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  onRequest
number of ops:  2
compiled vars:  !0 = $event
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function onrequest

Function onresponse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  onResponse
number of ops:  2
compiled vars:  !0 = $event
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function onresponse

End of class Symfony\Subscriber.

Class Solution\FiberRequestHandler:
Function handle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  handle
number of ops:  7
compiled vars:  !0 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
   92     1        INIT_STATIC_METHOD_CALL                                  'Fiber', 'suspend'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
   93     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function handle

End of class Solution\FiberRequestHandler.

Class Solution\MiddlewareToSubscriber:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  __construct
number of ops:  8
compiled vars:  !0 = $middleware
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  103     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'middleware'
          2        OP_DATA                                                  !0
  107     3        NEW                                              $2      'WeakMap'
          4        DO_FCALL                                      0          
          5        ASSIGN_OBJ                                               'requestFibers'
          6        OP_DATA                                                  $2
  108     7      > RETURN                                                   null

End of function __construct

Function onrequest:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  onRequest
number of ops:  19
compiled vars:  !0 = $event, !1 = $fiber, !2 = $newRequest
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   RECV                                             !0      
  112     1        NEW                                              $3      'Fiber'
          2        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          3        BIND_LEXICAL                                             ~4, !0
          4        SEND_VAL_EX                                              ~4
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $3
  113     7        INIT_METHOD_CALL                                         !1, 'start'
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !2, $7
  114    10        FETCH_OBJ_R                                      ~11     !2, 'body'
         11        FETCH_OBJ_W                                      $9      !0, 'request'
         12        ASSIGN_OBJ                                               $9, 'body'
         13        OP_DATA                                                  ~11
  115    14        FETCH_OBJ_R                                      ~13     !0, 'request'
         15        FETCH_OBJ_W                                      $12     'requestFibers'
         16        ASSIGN_DIM                                               $12, ~13
         17        OP_DATA                                                  !1
  116    18      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  Solution\{closure}
number of ops:  18
compiled vars:  !0 = $event
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E >   BIND_STATIC                                              !0
          1        FETCH_THIS                                       $1      
          2        FETCH_OBJ_R                                      ~2      $1, 'middleware'
          3        INIT_METHOD_CALL                                         ~2, 'process'
          4        NEW                                              $3      'Psr%5CRequest'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $4      !0, 'request'
          7        FETCH_OBJ_FUNC_ARG                               $5      $4, 'body'
          8        SEND_FUNC_ARG                                            $5
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $3
         11        NEW                                              $7      'Solution%5CFiberRequestHandler'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $7
         14        DO_FCALL                                      0  $9      
         15      > RETURN                                                   $9
         16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of Dynamic Function 0

End of function onrequest

Function onresponse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  onResponse
number of ops:  18
compiled vars:  !0 = $event, !1 = $fiber
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  118     0  E >   RECV                                             !0      
  120     1        FETCH_OBJ_R                                      ~3      !0, 'request'
          2        FETCH_OBJ_R                                      ~2      'requestFibers'
          3        FETCH_DIM_R                                      ~4      ~2, ~3
          4        ASSIGN                                                   !1, ~4
  121     5        INIT_METHOD_CALL                                         !1, 'resume'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $6      !0, 'response'
          8        SEND_FUNC_ARG                                            $6
          9        DO_FCALL                                      0          
  122    10        INIT_METHOD_CALL                                         !1, 'getReturn'
         11        DO_FCALL                                      0  $9      
         12        ASSIGN_OBJ                                               !0, 'response'
         13        OP_DATA                                                  $9
  123    14        FETCH_OBJ_R                                      ~11     !0, 'request'
         15        FETCH_OBJ_UNSET                                  $10     'requestFibers'
         16        UNSET_DIM                                                $10, ~11
  124    17      > RETURN                                                   null

End of function onresponse

End of class Solution\MiddlewareToSubscriber.

Class Solution\DumpMiddleware:
Function process:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0SjiQ
function name:  process
number of ops:  15
compiled vars:  !0 = $request, !1 = $handler, !2 = $response
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  129     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  131     2        INIT_NS_FCALL_BY_NAME                                    'Solution%5Cvar_dump'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
  133     5        INIT_METHOD_CALL                                         !1, 'handle'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $4      
          8        ASSIGN                                                   !2, $4
  135     9        INIT_NS_FCALL_BY_NAME                                    'Solution%5Cvar_dump'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0          
  137    12      > RETURN                                                   'modified+response'
  138    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function process

End of class Solution\DumpMiddleware.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.84 ms | 970 KiB | 14 Q