Skip to content

Commit cc97aab

Browse files
committed
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.
1 parent 554b31d commit cc97aab

File tree

5 files changed

+17
-42
lines changed

5 files changed

+17
-42
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ async(static function () {
2525
})();
2626
```
2727

28+
Another major change in this release is that you no longer inject the event loop into the client. It now
29+
only uses the global loop accessor. This ensures the same event loop is used everywhere and makes creating
30+
the client a bit simpler:
31+
32+
```php
33+
use Http\Adapter\React\ReactFactory;
34+
35+
$reactHttp = ReactFactory::buildHttpClient();
36+
```
37+
2838
### Changed
2939

40+
- Removed injecting of the event loop and fully switched to using the global loop accessor (`Loop::get()`)
3041
- Use PHP 8.1 fibers as async mechanism.
3142
- Detect supported PHP versions in range during CI instead of hardcoding them.
3243

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"require": {
1414
"php": "^8.1",
1515
"php-http/httplug": "^2.0",
16-
"react/http": "^1.0",
17-
"react/event-loop": "^1.2",
16+
"react/http": "^1.8",
17+
"react/event-loop": "^1.3",
1818
"php-http/discovery": "^1.0",
1919
"react/async": "^4"
2020
},

src/Client.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Http\Client\HttpClient;
77
use Psr\Http\Message\RequestInterface;
88
use Psr\Http\Message\ResponseInterface;
9-
use React\EventLoop\LoopInterface;
109
use React\Http\Browser as ReactBrowser;
1110

1211
/**
@@ -23,26 +22,13 @@ class Client implements HttpClient, HttpAsyncClient
2322
*/
2423
private $client;
2524

26-
/**
27-
* React event loop.
28-
*
29-
* @var LoopInterface
30-
*/
31-
private $loop;
32-
3325
/**
3426
* Initialize the React client.
3527
*/
3628
public function __construct(
37-
LoopInterface $loop = null,
3829
ReactBrowser $client = null
3930
) {
40-
if (null !== $client && null === $loop) {
41-
throw new \RuntimeException('You must give a LoopInterface instance with the Client');
42-
}
43-
44-
$this->loop = $loop ?: ReactFactory::buildEventLoop();
45-
$this->client = $client ?: ReactFactory::buildHttpClient($this->loop);
31+
$this->client = $client ?: ReactFactory::buildHttpClient();
4632
}
4733

4834
/**

src/ReactFactory.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Http\Adapter\React;
44

5-
use React\EventLoop\Loop;
6-
use React\EventLoop\LoopInterface;
75
use React\Http\Browser;
86
use React\Socket\ConnectorInterface;
97

@@ -14,25 +12,16 @@
1412
*/
1513
class ReactFactory
1614
{
17-
/**
18-
* Build a react Event Loop.
19-
*/
20-
public static function buildEventLoop(): LoopInterface
21-
{
22-
return Loop::get();
23-
}
24-
2515
/**
2616
* Build a React Http Client.
2717
*
2818
* @param ConnectorInterface|null $connector only pass this argument if you need to customize DNS
2919
* behaviour
3020
*/
3121
public static function buildHttpClient(
32-
LoopInterface $loop,
3322
ConnectorInterface $connector = null
3423
): Browser {
35-
return (new Browser($loop, $connector))
24+
return (new Browser($connector))
3625
->withRejectErrorResponse(false)
3726
->withFollowRedirects(false);
3827
}

tests/ReactFactoryTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Http\Adapter\React\ReactFactory;
66
use PHPUnit\Framework\TestCase;
7-
use React\EventLoop\LoopInterface;
87
use React\Http\Browser;
98
use React\Socket\ConnectorInterface;
109

@@ -16,27 +15,17 @@
1615
*/
1716
class ReactFactoryTest extends TestCase
1817
{
19-
/**
20-
* @var \React\EventLoop\LoopInterface
21-
*/
22-
private $loop;
23-
24-
protected function setUp(): void
25-
{
26-
$this->loop = $this->getMockBuilder(LoopInterface::class)->getMock();
27-
}
28-
2918
public function testBuildHttpClientWithConnector()
3019
{
3120
/** @var ConnectorInterface $connector */
3221
$connector = $this->getMockBuilder(ConnectorInterface::class)->getMock();
33-
$client = ReactFactory::buildHttpClient($this->loop, $connector);
22+
$client = ReactFactory::buildHttpClient($connector);
3423
$this->assertInstanceOf(Browser::class, $client);
3524
}
3625

3726
public function testBuildHttpClientWithoutConnector()
3827
{
39-
$client = ReactFactory::buildHttpClient($this->loop);
28+
$client = ReactFactory::buildHttpClient();
4029
$this->assertInstanceOf(Browser::class, $client);
4130
}
4231
}

0 commit comments

Comments
 (0)