From c78fbbfeabfbbf6b5ff40122eb7e1f5078002524 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Mon, 2 Aug 2021 13:05:22 +0200 Subject: [PATCH] Simplify usage by supporting new default loop --- README.md | 5 +---- composer.json | 6 +++--- examples/bash.php | 6 +----- examples/docker.php | 6 +----- examples/php.php | 6 +----- src/ProcessLauncher.php | 15 +++++++++++++-- tests/ProcessLauncherTest.php | 11 +++++++++++ 7 files changed, 31 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 79c93f0..a45e534 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ Once [installed](#install), you can use the following code to run an interactive bash shell and issue some commands within: ```php -$loop = React\EventLoop\Factory::create(); -$launcher = new ProcessLauncher($loop); +$launcher = new Clue\React\Shell\ProcessLauncher(); $shell = $launcher->createDeferredShell('bash'); @@ -27,8 +26,6 @@ $shell->execute('env | sort | head -n10')->then(function ($env) { }); $shell->end(); - -$loop->run(); ``` See also the [examples](examples): diff --git a/composer.json b/composer.json index dff6470..0de5241 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,9 @@ }, "require": { "php": ">=5.3", - "react/child-process": "^0.5 || ^0.4 || ^0.3", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", - "react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6", + "react/child-process": "^0.6.3", + "react/event-loop": "^1.2", + "react/stream": "^1.2", "react/promise": "^2.0 || ^1.0" }, "require-dev": { diff --git a/examples/bash.php b/examples/bash.php index 5bd971f..e75cc80 100644 --- a/examples/bash.php +++ b/examples/bash.php @@ -1,12 +1,10 @@ createDeferredShell('bash 2>&1'); @@ -19,5 +17,3 @@ }); $shell->end(); - -$loop->run(); diff --git a/examples/docker.php b/examples/docker.php index 650ba09..8f706e4 100644 --- a/examples/docker.php +++ b/examples/docker.php @@ -1,12 +1,10 @@ createDeferredShell('docker run -i --rm debian bash'); @@ -19,5 +17,3 @@ }); $shell->end(); - -$loop->run(); diff --git a/examples/php.php b/examples/php.php index a2a70f7..5554a48 100644 --- a/examples/php.php +++ b/examples/php.php @@ -1,12 +1,10 @@ createDeferredShell('php -a'); $shell->setBounding("echo '{{ bounding }}';"); @@ -24,5 +22,3 @@ }); $shell->end(); - -$loop->run(); diff --git a/src/ProcessLauncher.php b/src/ProcessLauncher.php index adeddaa..27ce977 100644 --- a/src/ProcessLauncher.php +++ b/src/ProcessLauncher.php @@ -3,17 +3,28 @@ namespace Clue\React\Shell; use React\ChildProcess\Process; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use Clue\React\Shell\DeferredShell; use React\Stream\CompositeStream; class ProcessLauncher { + /** @var LoopInterface */ private $loop; - public function __construct(LoopInterface $loop) + /** + * 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 + */ + public function __construct(LoopInterface $loop = null) { - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); } /** diff --git a/tests/ProcessLauncherTest.php b/tests/ProcessLauncherTest.php index 733508f..364de12 100644 --- a/tests/ProcessLauncherTest.php +++ b/tests/ProcessLauncherTest.php @@ -18,6 +18,17 @@ public function setUpProcessLauncher() $this->processLauncher = new ProcessLauncher($this->loop); } + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $launcher = new ProcessLauncher(); + + $ref = new \ReflectionProperty($launcher, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($launcher); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } + public function testProcessWillBeStarted() { $process = $this->getMockBuilder('React\ChildProcess\Process')->disableOriginalConstructor()->getMock();