Skip to content

Clean up event loop from client #58

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
16 changes: 1 addition & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
Expand Down
13 changes: 1 addition & 12 deletions src/ReactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Adapter\React;

use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Http\Browser;
use React\Socket\ConnectorInterface;

Expand All @@ -14,25 +12,16 @@
*/
class ReactFactory
{
/**
* Build a react Event Loop.
*/
public static function buildEventLoop(): LoopInterface
{
return Loop::get();
}

/**
* Build a React Http Client.
*
* @param ConnectorInterface|null $connector only pass this argument if you need to customize DNS
* behaviour
*/
public static function buildHttpClient(
LoopInterface $loop,
ConnectorInterface $connector = null
): Browser {
return (new Browser($loop, $connector))
return (new Browser($connector))
->withRejectErrorResponse(false)
->withFollowRedirects(false);
}
Expand Down
15 changes: 2 additions & 13 deletions tests/ReactFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}