Skip to content

Commit

Permalink
doc(runtime): Update reactphp example
Browse files Browse the repository at this point in the history
  • Loading branch information
chadyred committed Apr 1, 2024
1 parent 7ac32e7 commit d16b434
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions components/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 398 in components/runtime.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Psr\Http\Server\RequestHandlerInterface" does not exist
use React\EventLoop\Factory as ReactFactory;
use React\Http\Server as ReactHttpServer;
use React\Socket\Server as ReactSocketServer;
use React\Http\HttpServer as ReactHttpServer;

Check failure on line 399 in components/runtime.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "React\Http\HttpServer" does not exist
use React\Socket\SocketServer as ReactSocketServer;

Check failure on line 400 in components/runtime.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "React\Socket\SocketServer" does not exist
use Symfony\Component\Runtime\RunnerInterface;

class ReactPHPRunner implements RunnerInterface
class ReactPhpRunner implements RunnerInterface
{
public function __construct(
private RequestHandlerInterface $application,
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;

Check failure on line 462 in components/runtime.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Psr\Http\Server\RequestHandlerInterface" does not exist

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
Expand Down

0 comments on commit d16b434

Please sign in to comment.