From 6ae4f2f071b97f899670a449f2575582e22ef505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 8 Jul 2021 16:58:02 +0200 Subject: [PATCH 1/2] Simplify usage by supporting new default loop --- README.md | 5 +---- composer.json | 2 +- examples/client.php | 17 ++++++++--------- examples/server.php | 5 +---- src/Factory.php | 12 ++++++++++-- tests/FactoryTest.php | 15 +++++++++++++-- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6bf3b41..329feaa 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ Once [installed](#install), you can use the following code to connect to an UDP `localhost:1234` and send and receive UDP datagrams: ```php -$loop = React\EventLoop\Factory::create(); -$factory = new React\Datagram\Factory($loop); +$factory = new React\Datagram\Factory(); $factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) { $client->send('first'); @@ -20,8 +19,6 @@ $factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $ echo 'received "' . $message . '" from ' . $serverAddress. PHP_EOL; }); }); - -$loop->run(); ``` See also the [examples](examples). diff --git a/composer.json b/composer.json index 8fede46..09566e2 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "require": { "php": ">=5.3", "evenement/evenement": "^3.0 || ^2.0 || ^1.0", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/event-loop": "dev-master#78f7f43 as 1.2.0", "react/dns": "^1.7", "react/promise": "~2.1|~1.2" }, diff --git a/examples/client.php b/examples/client.php index d35d7d2..90c4518 100644 --- a/examples/client.php +++ b/examples/client.php @@ -1,11 +1,12 @@ createClient('localhost:1234')->then(function (React\Datagram\Socket $client) use ($loop) { +$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) { $client->send('first'); $client->on('message', function($message, $serverAddress, $client) { @@ -17,18 +18,18 @@ }); $n = 0; - $tid = $loop->addPeriodicTimer(2.0, function() use ($client, &$n) { + $tid = Loop::addPeriodicTimer(2.0, function() use ($client, &$n) { $client->send('tick' . ++$n); }); // read input from STDIN and forward everything to server - $loop->addReadStream(STDIN, function () use ($client, $loop, $tid) { + Loop::addReadStream(STDIN, function () use ($client, $tid) { $msg = fgets(STDIN, 2000); if ($msg === false) { // EOF => flush client and stop perodic sending and waiting for input $client->end(); - $loop->cancelTimer($tid); - $loop->removeReadStream(STDIN); + Loop::cancelTimer($tid); + Loop::removeReadStream(STDIN); } else { $client->send(trim($msg)); } @@ -36,5 +37,3 @@ }, function($error) { echo 'ERROR: ' . $error->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/server.php b/examples/server.php index 5c8603e..a685bf5 100644 --- a/examples/server.php +++ b/examples/server.php @@ -2,8 +2,7 @@ require_once __DIR__.'/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); -$factory = new React\Datagram\Factory($loop); +$factory = new React\Datagram\Factory(); $factory->createServer('localhost:1234')->then(function (React\Datagram\Socket $server) { $server->on('message', function($message, $address, $server) { @@ -12,5 +11,3 @@ echo 'client ' . $address . ': ' . $message . PHP_EOL; }); }); - -$loop->run(); diff --git a/src/Factory.php b/src/Factory.php index efee473..add2569 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -6,6 +6,7 @@ use React\Dns\Config\Config as DnsConfig; use React\Dns\Resolver\Factory as DnsFactory; use React\Dns\Resolver\ResolverInterface; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise; use React\Promise\CancellablePromiseInterface; @@ -18,13 +19,20 @@ class Factory /** * - * @param LoopInterface $loop + * This class takes an optional `LoopInterface|null $loop` parameter that can be used to + * pass the event loop instance to use for this object. You can use a `null` value + * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). + * This value SHOULD NOT be given unless you're sure you want to explicitly use a + * given event loop instance. + * + * @param ?LoopInterface $loop * @param ?ResolverInterface $resolver Resolver instance to use. Will otherwise * try to load the system default DNS config or fall back to using * Google's public DNS 8.8.8.8 */ - public function __construct(LoopInterface $loop, ResolverInterface $resolver = null) + public function __construct(LoopInterface $loop = null, ResolverInterface $resolver = null) { + $loop = $loop ?: Loop::get(); if ($resolver === null) { // try to load nameservers from system config or default to Google's public DNS $config = DnsConfig::loadSystemConfigBlocking(); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index ab3d7f7..f4bb6b9 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -2,9 +2,9 @@ namespace React\Tests\Datagram; -use React\Datagram\Socket; -use React\Datagram\Factory; use Clue\React\Block; +use React\Datagram\Factory; +use React\Datagram\Socket; use React\Promise; class FactoryTest extends TestCase @@ -23,6 +23,17 @@ public function setUpFactory() $this->factory = new Factory($this->loop, $this->resolver); } + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $factory = new Factory(); + + $ref = new \ReflectionProperty($factory, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($factory); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } + public function testCreateClient() { $this->resolver->expects($this->never())->method('resolve'); From c85436d5ffc9573e92b37e6319ea63294a5b83ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 8 Jul 2021 17:01:16 +0200 Subject: [PATCH 2/2] Update to stable reactphp/event-loop v1.2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 09566e2..c5b5934 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "require": { "php": ">=5.3", "evenement/evenement": "^3.0 || ^2.0 || ^1.0", - "react/event-loop": "dev-master#78f7f43 as 1.2.0", + "react/event-loop": "^1.2", "react/dns": "^1.7", "react/promise": "~2.1|~1.2" },