-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Runtime] Update ReactPHP example #19727
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,13 +395,12 @@ | |
|
||
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 @@ | |
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; | ||
} | ||
|
@@ -435,7 +431,7 @@ | |
By extending the ``GenericRuntime``, you make sure that the application is | ||
always using this ``ReactPHPRunner``:: | ||
|
||
use Symfony\Component\Runtime\GenericRuntime; | ||
use Symfony\Component\Runtime\RunnerInterface; | ||
|
||
class ReactPHPRuntime extends GenericRuntime | ||
|
@@ -451,7 +447,7 @@ | |
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 @@ | |
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the common use case for a react-php application would be to define your request handler as an anonymous class in the front controller, as this does really allow testing your logic (or at least it makes it harder). The previous code suggesting to instantiate a class makes more sense to me (however, changing the return type of the closure to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @stof , sure, in fact this PR is to avoid frustration about testing this example with a minimal source code. Why not showing what "SomeCustomPsr15Application" could looks like ? (on another piece of code) It is for having a full functional example, for you is it too much ? In my mind, when I read it, I came to the fact it misses "things", maybe it is enough to stop at the "SomeCustomPsr15Application", but it requires more research if we want to enjoy the moment we end the reading to enjoy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I keep it simple, the full example could be too much as it requires attention to maintain details about it in the future (it depends too much of an external lib) |
||
public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface | ||
chadyred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this need to be set here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is for the application : by default it uses the SymfonyRuntime on the autoload_runtime, this is the way to choose our Runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if that is a good way to demo it in the example though. This would override whatever you would specify in the ENV of the webserver for example. Users might get the wrong impression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will push a shorter version with all feedbacks, thanks @alcohol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I push a new version without all this detail as explain just above to stof