Skip to content
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

Simplify usage by supporting new default loop #15

Merged
merged 1 commit into from
Aug 3, 2021
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
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ as defined in [RFC 6763](http://tools.ietf.org/html/rfc6763).
Once [installed](#install), you can use the following code to search all available UPnP devices in your network:

```php
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Clue\React\Ssdp\Client();

$client->search()->then(
function () {
Expand All @@ -47,8 +46,6 @@ $client->search()->then(
echo PHP_EOL;
}
);

$loop->run();
```

See also the [examples](examples).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": ">=5.3",
"clue/multicast-react": "^1.0 || ^0.2",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"react/promise": "^2.0 || ^1.0"
},
"require-dev": {
Expand Down
5 changes: 1 addition & 4 deletions examples/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->search()->then(
function () {
Expand All @@ -20,5 +19,3 @@ function ($progress) {
echo PHP_EOL;
}
);

$loop->run();
25 changes: 18 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@

namespace Clue\React\Ssdp;

use React\EventLoop\LoopInterface;
use Clue\React\Multicast\Factory as MulticastFactory;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use RuntimeException;

class Client
{
const ADDRESS = '239.255.255.250:1900';

/** @var LoopInterface */
private $loop;

/** @var MulticastFactory */
private $multicast;

public function __construct(LoopInterface $loop, MulticastFactory $multicast = null)
/**
* 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 ?MulticastFactory $multicast
*/
public function __construct(LoopInterface $loop = null, MulticastFactory $multicast = null)
{
if ($multicast === null) {
$multicast = new MulticastFactory($loop);
}
$this->loop = $loop;
$this->multicast = $multicast;
$this->loop = $loop ?: Loop::get();
$this->multicast = $multicast ?: new MulticastFactory($this->loop);
}

public function search($searchTarget = 'ssdp:all', $mx = 2)
Expand Down
18 changes: 14 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
namespace Clue\Tests\React\Ssdp;

use Clue\React\Ssdp\Client;
use React\EventLoop\Factory;
use React\EventLoop\Loop;

class ClientTest extends TestCase
{
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$client = new Client();

$ref = new \ReflectionProperty($client, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($client);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}

/**
* @doesNotPerformAssertions
*/
Expand Down Expand Up @@ -52,12 +63,11 @@ interface_exists('React\EventLoop\TimerInterface') ? 'React\EventLoop\TimerInter

public function testSearchTimeout()
{
$loop = Factory::create();
$client = new Client($loop);
$client = new Client();

$promise = $client->search('ssdp:all', 0.01);

$loop->run();
Loop::run();

$promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever());
}
Expand Down