Skip to content

Commit

Permalink
Merge pull request #16 from SimonFrings/loop
Browse files Browse the repository at this point in the history
Simplify usage by supporting new default loop
  • Loading branch information
clue committed Aug 3, 2021
2 parents 76e1caa + c78fbbf commit 53d2954
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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

$shell->end();

$loop->run();
```

See also the [examples](examples):
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 1 addition & 5 deletions examples/bash.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use React\EventLoop\Factory;
use Clue\React\Shell\ProcessLauncher;

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

$loop = Factory::create();
$launcher = new ProcessLauncher($loop);
$launcher = new ProcessLauncher();

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

Expand All @@ -19,5 +17,3 @@
});

$shell->end();

$loop->run();
6 changes: 1 addition & 5 deletions examples/docker.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use React\EventLoop\Factory;
use Clue\React\Shell\ProcessLauncher;

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

$loop = Factory::create();
$launcher = new ProcessLauncher($loop);
$launcher = new ProcessLauncher();

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

Expand All @@ -19,5 +17,3 @@
});

$shell->end();

$loop->run();
6 changes: 1 addition & 5 deletions examples/php.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use React\EventLoop\Factory;
use Clue\React\Shell\ProcessLauncher;

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

$loop = Factory::create();
$launcher = new ProcessLauncher($loop);
$launcher = new ProcessLauncher();

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

$shell->end();

$loop->run();
15 changes: 13 additions & 2 deletions src/ProcessLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/ProcessLauncherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 53d2954

Please sign in to comment.