Skip to content

Commit 54ea2ae

Browse files
committed
doc(runtime): Update reactphp example
1 parent 7ac32e7 commit 54ea2ae

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

components/runtime.rst

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,11 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner
396396
use Psr\Http\Message\ResponseInterface;
397397
use Psr\Http\Message\ServerRequestInterface;
398398
use Psr\Http\Server\RequestHandlerInterface;
399-
use React\EventLoop\Factory as ReactFactory;
400-
use React\Http\Server as ReactHttpServer;
401-
use React\Socket\Server as ReactSocketServer;
399+
use React\Http\HttpServer as ReactHttpServer;
400+
use React\Socket\SocketServer as ReactSocketServer;
402401
use Symfony\Component\Runtime\RunnerInterface;
403402

404-
class ReactPHPRunner implements RunnerInterface
403+
class ReactPhpRunner implements RunnerInterface
405404
{
406405
public function __construct(
407406
private RequestHandlerInterface $application,
@@ -412,21 +411,18 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner
412411
public function run(): int
413412
{
414413
$application = $this->application;
415-
$loop = ReactFactory::create();
416414

417-
// configure ReactPHP to correctly handle the PSR-15 application
418-
$server = new ReactHttpServer(
419-
$loop,
420-
function (ServerRequestInterface $request) use ($application): ResponseInterface {
421-
return $application->handle($request);
422-
}
423-
);
415+
$serverAddress = '127.0.0.1:' . $this->port;
416+
$socket = new ReactSocketServer($serverAddress);
417+
418+
$server = new ReactHttpServer(function (ServerRequestInterface $requestHandler) use ($application) {
419+
return $application->handle($requestHandler) ;
420+
});
424421

425-
// start the ReactPHP server
426-
$socket = new ReactSocketServer($this->port, $loop);
422+
// listen the ReactPHP socket
427423
$server->listen($socket);
428424

429-
$loop->run();
425+
echo "Server running at http://" . $serverAddress . PHP_EOL;
430426

431427
return 0;
432428
}
@@ -451,7 +447,7 @@ always using this ``ReactPHPRunner``::
451447
public function getRunner(?object $application): RunnerInterface
452448
{
453449
if ($application instanceof RequestHandlerInterface) {
454-
return new ReactPHPRunner($application, $this->port);
450+
return new ReactPhpRunner($application, $this->port);
455451
}
456452

457453
// if it's not a PSR-15 application, use the GenericRuntime to
@@ -462,12 +458,24 @@ always using this ``ReactPHPRunner``::
462458

463459
The end user will now be able to create front controller like::
464460

465-
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
461+
use App\Runtime\ReactPhpRuntime;
462+
use Psr\Http\Server\RequestHandlerInterface;
466463

467-
return function (array $context): SomeCustomPsr15Application {
468-
return new SomeCustomPsr15Application();
469-
};
464+
$_SERVER['APP_RUNTIME'] = ReactPhpRuntime::class;
465+
466+
require_once dirname(__DIR__) . '/vendor/autoload_runtime.php';
470467

468+
return static function (): RequestHandlerInterface {
469+
return new class implements RequestHandlerInterface {
470+
public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface
471+
{
472+
return new \React\Http\Message\Response(
473+
headers: ['Content-Type' => 'text/html; charset=utf-8'],
474+
body: 'Welcome to your new application'
475+
);
476+
}
477+
};
478+
};
471479
.. _PHP-PM: https://github.com/php-pm/php-pm
472480
.. _Swoole: https://openswoole.com/
473481
.. _ReactPHP: https://reactphp.org/

0 commit comments

Comments
 (0)