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);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.20.0140.00319.39
8.4.10.0130.00619.33
8.3.160.0000.01818.27
8.3.150.0140.00616.80
8.3.140.0070.00716.69
8.3.130.0090.00018.35
8.3.120.0030.00620.63
8.3.110.0040.00422.58
8.3.100.0280.00737.54
8.3.90.0320.00737.54
8.3.80.0320.00937.54
8.3.70.0310.00837.54
8.3.60.0320.00737.54
8.3.50.0310.00837.54
8.3.40.0240.00737.54
8.3.30.0240.00837.54
8.3.20.0310.00537.54
8.3.10.0300.00737.54
8.3.00.0270.00637.54
8.2.270.0040.00416.73
8.2.260.0090.00916.84
8.2.250.0080.00016.93
8.2.240.0060.00317.17
8.2.230.0040.00422.58
8.2.220.0150.00537.54
8.2.210.0250.00737.54
8.2.200.0240.00737.54
8.2.190.0240.00737.54
8.2.180.0230.00737.54
8.2.170.0260.00637.54
8.2.160.0270.00537.54
8.2.150.0250.00537.54
8.2.140.0240.00637.54
8.2.130.0280.00437.54
8.2.120.0260.00437.54
8.2.110.0250.01037.54
8.2.100.0290.00737.54
8.2.90.0290.00737.54
8.2.80.0250.01137.54
8.2.70.0270.00837.54
8.2.60.0270.00537.54
8.2.50.0270.00637.54
8.2.40.0260.00637.54
8.2.30.0250.00937.54
8.2.20.0300.00537.54
8.2.10.0280.00837.54
8.2.00.0260.00837.54
8.1.310.0040.00418.09
8.1.300.0060.00317.78
8.1.290.0290.00837.54
8.1.280.0270.00937.54
8.1.270.0240.00837.54
8.1.260.0290.00637.54
8.1.250.0280.00637.54
8.1.240.0260.00537.54
8.1.230.0240.00637.54
8.1.220.0240.00737.54
8.1.210.0240.00637.54
8.1.200.0240.00737.54
8.1.190.0240.00537.54
8.1.180.0230.00937.54
8.1.170.0250.00637.54
8.1.160.0250.00937.54
8.1.150.0260.00837.54
8.1.140.0290.00537.54
8.1.130.0270.00537.54
8.1.120.0250.00637.54
8.1.110.0260.00737.54
8.1.100.0290.00437.54
8.1.90.0260.00737.54
8.1.80.0260.00737.54
8.1.70.0270.00637.54
8.1.60.0320.00837.54
8.1.50.0290.00937.54
8.1.40.0330.00537.54
8.1.30.0280.00837.54
8.1.20.0270.00737.54
8.1.10.0280.00737.54
8.1.00.0290.00637.54

preferences:
31.69 ms | 403 KiB | 5 Q