From cc97aabea0f8263762f414f873098dfd0c840d62 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 14 Nov 2022 10:37:15 +0100 Subject: [PATCH] Clean up event loop from client Fully relying on Loop::get() from here on, as such bumped the react/http and react/event-loop to compatible versions. --- CHANGELOG.md | 11 +++++++++++ composer.json | 4 ++-- src/Client.php | 16 +--------------- src/ReactFactory.php | 13 +------------ tests/ReactFactoryTest.php | 15 ++------------- 5 files changed, 17 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7f62f7..afe3e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,8 +25,19 @@ async(static function () { })(); ``` +Another major change in this release is that you no longer inject the event loop into the client. It now +only uses the global loop accessor. This ensures the same event loop is used everywhere and makes creating +the client a bit simpler: + +```php +use Http\Adapter\React\ReactFactory; + +$reactHttp = ReactFactory::buildHttpClient(); +``` + ### Changed +- Removed injecting of the event loop and fully switched to using the global loop accessor (`Loop::get()`) - Use PHP 8.1 fibers as async mechanism. - Detect supported PHP versions in range during CI instead of hardcoding them. diff --git a/composer.json b/composer.json index 2d6bfa7..2cb36d3 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,8 @@ "require": { "php": "^8.1", "php-http/httplug": "^2.0", - "react/http": "^1.0", - "react/event-loop": "^1.2", + "react/http": "^1.8", + "react/event-loop": "^1.3", "php-http/discovery": "^1.0", "react/async": "^4" }, diff --git a/src/Client.php b/src/Client.php index 002ea8b..4287ccc 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,6 @@ use Http\Client\HttpClient; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use React\EventLoop\LoopInterface; use React\Http\Browser as ReactBrowser; /** @@ -23,26 +22,13 @@ class Client implements HttpClient, HttpAsyncClient */ private $client; - /** - * React event loop. - * - * @var LoopInterface - */ - private $loop; - /** * Initialize the React client. */ public function __construct( - LoopInterface $loop = null, ReactBrowser $client = null ) { - if (null !== $client && null === $loop) { - throw new \RuntimeException('You must give a LoopInterface instance with the Client'); - } - - $this->loop = $loop ?: ReactFactory::buildEventLoop(); - $this->client = $client ?: ReactFactory::buildHttpClient($this->loop); + $this->client = $client ?: ReactFactory::buildHttpClient(); } /** diff --git a/src/ReactFactory.php b/src/ReactFactory.php index e882326..10059a6 100644 --- a/src/ReactFactory.php +++ b/src/ReactFactory.php @@ -2,8 +2,6 @@ namespace Http\Adapter\React; -use React\EventLoop\Loop; -use React\EventLoop\LoopInterface; use React\Http\Browser; use React\Socket\ConnectorInterface; @@ -14,14 +12,6 @@ */ class ReactFactory { - /** - * Build a react Event Loop. - */ - public static function buildEventLoop(): LoopInterface - { - return Loop::get(); - } - /** * Build a React Http Client. * @@ -29,10 +19,9 @@ public static function buildEventLoop(): LoopInterface * behaviour */ public static function buildHttpClient( - LoopInterface $loop, ConnectorInterface $connector = null ): Browser { - return (new Browser($loop, $connector)) + return (new Browser($connector)) ->withRejectErrorResponse(false) ->withFollowRedirects(false); } diff --git a/tests/ReactFactoryTest.php b/tests/ReactFactoryTest.php index f0f7b03..d41c25b 100644 --- a/tests/ReactFactoryTest.php +++ b/tests/ReactFactoryTest.php @@ -4,7 +4,6 @@ use Http\Adapter\React\ReactFactory; use PHPUnit\Framework\TestCase; -use React\EventLoop\LoopInterface; use React\Http\Browser; use React\Socket\ConnectorInterface; @@ -16,27 +15,17 @@ */ class ReactFactoryTest extends TestCase { - /** - * @var \React\EventLoop\LoopInterface - */ - private $loop; - - protected function setUp(): void - { - $this->loop = $this->getMockBuilder(LoopInterface::class)->getMock(); - } - public function testBuildHttpClientWithConnector() { /** @var ConnectorInterface $connector */ $connector = $this->getMockBuilder(ConnectorInterface::class)->getMock(); - $client = ReactFactory::buildHttpClient($this->loop, $connector); + $client = ReactFactory::buildHttpClient($connector); $this->assertInstanceOf(Browser::class, $client); } public function testBuildHttpClientWithoutConnector() { - $client = ReactFactory::buildHttpClient($this->loop); + $client = ReactFactory::buildHttpClient(); $this->assertInstanceOf(Browser::class, $client); } }