Last active
August 7, 2024 16:26
-
-
Save vudaltsov/dc4f372692d2eabbc8c3d29cd4de0ccd to your computer and use it in GitHub Desktop.
Revisions
-
vudaltsov revised this gist
Aug 7, 2024 . No changes.There are no files selected for viewing
-
vudaltsov revised this gist
Aug 7, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ * один в стиле PSR-7, другой — в стиле Symfony HttpFoundation. * * Нужно написать класс, который позволит интегрировать * PSR Middleware в Symfony. * * Всё необходимое для решения задачи есть в этом файле. * Опыт работы с Symfony/Laravel не обязателен. -
vudaltsov created this gist
Aug 7, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ <?php declare(strict_types=1); /** * Даны два разных механизма расширения обработки Http-сообщений: * один в стиле PSR-7, другой — в стиле Symfony HttpFoundation. * * Нужно написать класс, который позволит интегрировать * подписчик от Symfony в PSR фреймворк. * * Всё необходимое для решения задачи есть в этом файле. * Опыт работы с Symfony/Laravel не обязателен. */ 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; /** * Напиши адаптер, который позволит использовать Psr\Middleware как Symfony\Subscriber. */