From 2c0ce01df634b6da79d88eb20d04e87e04eba088 Mon Sep 17 00:00:00 2001 From: gokakyu Date: Mon, 1 Apr 2024 15:54:02 +0200 Subject: [PATCH 1/3] doc(runtime): Update reactphp example --- components/runtime.rst | 47 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/components/runtime.rst b/components/runtime.rst index 5e6e173240c..8d049b4f39b 100644 --- a/components/runtime.rst +++ b/components/runtime.rst @@ -396,12 +396,11 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; - use React\EventLoop\Factory as ReactFactory; - use React\Http\Server as ReactHttpServer; - use React\Socket\Server as ReactSocketServer; + use React\Http\HttpServer as ReactHttpServer; + use React\Socket\SocketServer as ReactSocketServer; use Symfony\Component\Runtime\RunnerInterface; - class ReactPHPRunner implements RunnerInterface + class ReactPhpRunner implements RunnerInterface { public function __construct( private RequestHandlerInterface $application, @@ -412,21 +411,18 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner public function run(): int { $application = $this->application; - $loop = ReactFactory::create(); - // configure ReactPHP to correctly handle the PSR-15 application - $server = new ReactHttpServer( - $loop, - function (ServerRequestInterface $request) use ($application): ResponseInterface { - return $application->handle($request); - } - ); + $serverAddress = '127.0.0.1:' . $this->port; + $socket = new ReactSocketServer($serverAddress); + + $server = new ReactHttpServer(function (ServerRequestInterface $requestHandler) use ($application) { + return $application->handle($requestHandler) ; + }); - // start the ReactPHP server - $socket = new ReactSocketServer($this->port, $loop); + // listen the ReactPHP socket $server->listen($socket); - $loop->run(); + echo "Server running at http://" . $serverAddress . PHP_EOL; return 0; } @@ -451,7 +447,7 @@ always using this ``ReactPHPRunner``:: public function getRunner(?object $application): RunnerInterface { if ($application instanceof RequestHandlerInterface) { - return new ReactPHPRunner($application, $this->port); + return new ReactPhpRunner($application, $this->port); } // if it's not a PSR-15 application, use the GenericRuntime to @@ -462,10 +458,23 @@ always using this ``ReactPHPRunner``:: The end user will now be able to create front controller like:: - require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; + use App\Runtime\ReactPhpRuntime; + use Psr\Http\Server\RequestHandlerInterface; - return function (array $context): SomeCustomPsr15Application { - return new SomeCustomPsr15Application(); + $_SERVER['APP_RUNTIME'] = ReactPhpRuntime::class; + + require_once dirname(__DIR__) . '/vendor/autoload_runtime.php'; + + return static function (): RequestHandlerInterface { + return new class implements RequestHandlerInterface { + public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface + { + return new \React\Http\Message\Response( + headers: ['Content-Type' => 'text/html; charset=utf-8'], + body: 'Welcome to your new application' + ); + } + }; }; .. _PHP-PM: https://github.com/php-pm/php-pm From facaa21fb61f572d4db9f3af6ea6fda24eac06b6 Mon Sep 17 00:00:00 2001 From: gokakyu Date: Tue, 9 Apr 2024 10:18:20 +0200 Subject: [PATCH 2/3] Feedback --- components/runtime.rst | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/components/runtime.rst b/components/runtime.rst index 8d049b4f39b..5be8e298478 100644 --- a/components/runtime.rst +++ b/components/runtime.rst @@ -400,7 +400,7 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner use React\Socket\SocketServer as ReactSocketServer; use Symfony\Component\Runtime\RunnerInterface; - class ReactPhpRunner implements RunnerInterface + class ReactPHPRunner implements RunnerInterface { public function __construct( private RequestHandlerInterface $application, @@ -447,7 +447,7 @@ always using this ``ReactPHPRunner``:: public function getRunner(?object $application): RunnerInterface { if ($application instanceof RequestHandlerInterface) { - return new ReactPhpRunner($application, $this->port); + return new ReactPHPRunner($application, $this->port); } // if it's not a PSR-15 application, use the GenericRuntime to @@ -458,23 +458,10 @@ always using this ``ReactPHPRunner``:: The end user will now be able to create front controller like:: - use App\Runtime\ReactPhpRuntime; - use Psr\Http\Server\RequestHandlerInterface; - - $_SERVER['APP_RUNTIME'] = ReactPhpRuntime::class; - - require_once dirname(__DIR__) . '/vendor/autoload_runtime.php'; + require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; - return static function (): RequestHandlerInterface { - return new class implements RequestHandlerInterface { - public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface - { - return new \React\Http\Message\Response( - headers: ['Content-Type' => 'text/html; charset=utf-8'], - body: 'Welcome to your new application' - ); - } - }; + return function (array $context): SomeCustomPsr15Application { + return new SomeCustomPsr15Application(); }; .. _PHP-PM: https://github.com/php-pm/php-pm From 905b235fd06c850c1e94c069ff2e1eeb5cfaa0ea Mon Sep 17 00:00:00 2001 From: gokakyu Date: Tue, 9 Apr 2024 10:20:37 +0200 Subject: [PATCH 3/3] Feedback --- components/runtime.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/runtime.rst b/components/runtime.rst index 5be8e298478..bdb5a9f6e20 100644 --- a/components/runtime.rst +++ b/components/runtime.rst @@ -458,9 +458,11 @@ always using this ``ReactPHPRunner``:: The end user will now be able to create front controller like:: + use Psr\Http\Server\RequestHandlerInterface; + require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; - return function (array $context): SomeCustomPsr15Application { + return function (array $context): RequestHandlerInterface { return new SomeCustomPsr15Application(); };