Skip to content

Commit c78fbbf

Browse files
committed
Simplify usage by supporting new default loop
1 parent 76e1caa commit c78fbbf

File tree

7 files changed

+31
-24
lines changed

7 files changed

+31
-24
lines changed

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ Once [installed](#install), you can use the following code to run an interactive
1313
bash shell and issue some commands within:
1414

1515
```php
16-
$loop = React\EventLoop\Factory::create();
17-
$launcher = new ProcessLauncher($loop);
16+
$launcher = new Clue\React\Shell\ProcessLauncher();
1817

1918
$shell = $launcher->createDeferredShell('bash');
2019

@@ -27,8 +26,6 @@ $shell->execute('env | sort | head -n10')->then(function ($env) {
2726
});
2827

2928
$shell->end();
30-
31-
$loop->run();
3229
```
3330

3431
See also the [examples](examples):

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
},
1919
"require": {
2020
"php": ">=5.3",
21-
"react/child-process": "^0.5 || ^0.4 || ^0.3",
22-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
23-
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6",
21+
"react/child-process": "^0.6.3",
22+
"react/event-loop": "^1.2",
23+
"react/stream": "^1.2",
2424
"react/promise": "^2.0 || ^1.0"
2525
},
2626
"require-dev": {

examples/bash.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Shell\ProcessLauncher;
54

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

8-
$loop = Factory::create();
9-
$launcher = new ProcessLauncher($loop);
7+
$launcher = new ProcessLauncher();
108

119
$shell = $launcher->createDeferredShell('bash 2>&1');
1210

@@ -19,5 +17,3 @@
1917
});
2018

2119
$shell->end();
22-
23-
$loop->run();

examples/docker.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Shell\ProcessLauncher;
54

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

8-
$loop = Factory::create();
9-
$launcher = new ProcessLauncher($loop);
7+
$launcher = new ProcessLauncher();
108

119
$shell = $launcher->createDeferredShell('docker run -i --rm debian bash');
1210

@@ -19,5 +17,3 @@
1917
});
2018

2119
$shell->end();
22-
23-
$loop->run();

examples/php.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Shell\ProcessLauncher;
54

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

8-
$loop = Factory::create();
9-
$launcher = new ProcessLauncher($loop);
7+
$launcher = new ProcessLauncher();
108

119
$shell = $launcher->createDeferredShell('php -a');
1210
$shell->setBounding("echo '{{ bounding }}';");
@@ -24,5 +22,3 @@
2422
});
2523

2624
$shell->end();
27-
28-
$loop->run();

src/ProcessLauncher.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
namespace Clue\React\Shell;
44

55
use React\ChildProcess\Process;
6+
use React\EventLoop\Loop;
67
use React\EventLoop\LoopInterface;
78
use Clue\React\Shell\DeferredShell;
89
use React\Stream\CompositeStream;
910

1011
class ProcessLauncher
1112
{
13+
/** @var LoopInterface */
1214
private $loop;
1315

14-
public function __construct(LoopInterface $loop)
16+
/**
17+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
18+
* pass the event loop instance to use for this object. You can use a `null` value
19+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
20+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
21+
* given event loop instance.
22+
*
23+
* @param ?LoopInterface $loop
24+
*/
25+
public function __construct(LoopInterface $loop = null)
1526
{
16-
$this->loop = $loop;
27+
$this->loop = $loop ?: Loop::get();
1728
}
1829

1930
/**

tests/ProcessLauncherTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ public function setUpProcessLauncher()
1818
$this->processLauncher = new ProcessLauncher($this->loop);
1919
}
2020

21+
public function testConstructWithoutLoopAssignsLoopAutomatically()
22+
{
23+
$launcher = new ProcessLauncher();
24+
25+
$ref = new \ReflectionProperty($launcher, 'loop');
26+
$ref->setAccessible(true);
27+
$loop = $ref->getValue($launcher);
28+
29+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
30+
}
31+
2132
public function testProcessWillBeStarted()
2233
{
2334
$process = $this->getMockBuilder('React\ChildProcess\Process')->disableOriginalConstructor()->getMock();

0 commit comments

Comments
 (0)