diff --git a/composer.json b/composer.json index 3a12889..af558c8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=5.3", "clue/redis-protocol": "0.3.*", "evenement/evenement": "^3.0 || ^2.0 || ^1.0", - "react/event-loop": "^1.0 || ^0.5", + "react/event-loop": "dev-global-event-loop-accessor-part-four as 1.2.0", "react/promise": "^2.0 || ^1.1", "react/promise-timer": "^1.5", "react/socket": "^1.1" @@ -28,5 +28,11 @@ }, "autoload-dev": { "psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/wyrihaximus-labs/event-loop" + } + ] } diff --git a/examples/cli.php b/examples/cli.php index f4dd537..5ac9d48 100644 --- a/examples/cli.php +++ b/examples/cli.php @@ -2,23 +2,23 @@ use Clue\React\Redis\Client; use Clue\React\Redis\Factory; +use React\EventLoop\Loop; use React\Promise\PromiseInterface; require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); echo '# connecting to redis...' . PHP_EOL; -$factory->createClient('localhost')->then(function (Client $client) use ($loop) { +$factory->createClient('localhost')->then(function (Client $client) { echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL; - $loop->addReadStream(STDIN, function () use ($client, $loop) { + Loop::get()->addReadStream(STDIN, function () use ($client) { $line = fgets(STDIN); if ($line === false || $line === '') { echo '# CTRL-D -> Ending connection...' . PHP_EOL; - $loop->removeReadStream(STDIN); + Loop::get()->removeReadStream(STDIN); return $client->end(); } @@ -43,10 +43,10 @@ }); }); - $client->on('close', function() use ($loop) { + $client->on('close', function() { echo '## DISCONNECTED' . PHP_EOL; - $loop->removeReadStream(STDIN); + Loop::get()->removeReadStream(STDIN); }); }, function (Exception $error) { echo 'CONNECTION ERROR: ' . $error->getMessage() . PHP_EOL; @@ -56,4 +56,4 @@ exit(1); }); -$loop->run(); +Loop::get()->run(); diff --git a/examples/incr.php b/examples/incr.php index 0eaaa32..0034d87 100644 --- a/examples/incr.php +++ b/examples/incr.php @@ -1,11 +1,11 @@ createLazyClient('localhost'); $client->incr('test'); @@ -22,4 +22,4 @@ $client->end(); -$loop->run(); +Loop::get()->run(); diff --git a/examples/publish.php b/examples/publish.php index 4da3c17..f7f98e5 100644 --- a/examples/publish.php +++ b/examples/publish.php @@ -1,11 +1,11 @@ end(); -$loop->run(); +Loop::get()->run(); diff --git a/examples/subscribe.php b/examples/subscribe.php index 3dedae8..a8fe218 100644 --- a/examples/subscribe.php +++ b/examples/subscribe.php @@ -1,11 +1,11 @@ on('unsubscribe', function ($channel) use ($client, $loop) { +$client->on('unsubscribe', function ($channel) use ($client) { echo 'Unsubscribed from ' . $channel . PHP_EOL; - $loop->addPeriodicTimer(2.0, function ($timer) use ($client, $channel, $loop){ - $client->subscribe($channel)->then(function () use ($timer, $loop) { + Loop::get()->addPeriodicTimer(2.0, function ($timer) use ($client, $channel){ + $client->subscribe($channel)->then(function () use ($timer) { echo 'Now subscribed again' . PHP_EOL; - $loop->cancelTimer($timer); + Loop::get()->cancelTimer($timer); }, function (Exception $e) { echo 'Unable to subscribe again: ' . $e->getMessage() . PHP_EOL; }); }); }); -$loop->run(); +Loop::get()->run(); diff --git a/src/Factory.php b/src/Factory.php index ce95c41..27c6a1e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -3,6 +3,7 @@ namespace Clue\React\Redis; use Clue\Redis\Protocol\Factory as ProtocolFactory; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Deferred; use React\Promise\Timer\TimeoutException; @@ -23,8 +24,12 @@ class Factory * Should be `null` in order to use default Connector. * @param ProtocolFactory|null $protocol */ - public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ProtocolFactory $protocol = null) + public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ProtocolFactory $protocol = null) { + if ($loop === null) { + $loop = Loop::get(); + } + if ($connector === null) { $connector = new Connector($loop); }