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

Improve documentation and examples #20

Merged
merged 1 commit into from
Feb 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# clue/reactphp-shell

[![CI status](https://github.com/clue/reactphp-shell/workflows/CI/badge.svg)](https://github.com/clue/reactphp-shell/actions)
[![CI status](https://github.com/clue/reactphp-shell/actions/workflows/ci.yml/badge.svg)](https://github.com/clue/reactphp-shell/actions)
[![installs on Packagist](https://img.shields.io/packagist/dt/clue/shell-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/shell-react)

Run async commands within any interactive shell command, built on top of [ReactPHP](https://reactphp.org/).
Expand All @@ -13,57 +13,65 @@ Once [installed](#install), you can use the following code to run an interactive
bash shell and issue some commands within:

```php
<?php

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

$launcher = new Clue\React\Shell\ProcessLauncher();

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

$shell->execute('echo -n $USER')->then(function ($result) {
var_dump('current user', $result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->execute('env | sort | head -n10')->then(function ($env) {
var_dump('env', $env);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->end();
```

See also the [examples](examples):
See also the [examples](examples/):

* [Run shell commands within a bash shell](examples/bash.php)
* [Run PHP code within an interactive PHP shell](examples/php.php)
* [Run shell commands within a docker container](examples/docker.php)

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
The recommended way to install this library is [through Composer](https://getcomposer.org/).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)

This will install the latest supported version:

```bash
$ composer require clue/shell-react:^0.2
composer require clue/shell-react:^0.2
```

See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
It's *highly recommended to use PHP 7+* for this project.
It's *highly recommended to use the latest supported PHP version* for this project.

## Tests

To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org):
dependencies [through Composer](https://getcomposer.org/):

```bash
$ composer install
composer install
```

To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
vendor/bin/phpunit
```

## License
Expand Down
8 changes: 5 additions & 3 deletions examples/bash.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php

use Clue\React\Shell\ProcessLauncher;

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

$launcher = new ProcessLauncher();
$launcher = new Clue\React\Shell\ProcessLauncher();

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

$shell->execute('echo -n $USER')->then(function ($result) {
var_dump('current user', $result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->execute('env | sort | head -n10')->then(function ($env) {
var_dump('env', $env);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->end();
8 changes: 5 additions & 3 deletions examples/docker.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php

use Clue\React\Shell\ProcessLauncher;

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

$launcher = new ProcessLauncher();
$launcher = new Clue\React\Shell\ProcessLauncher();

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

$shell->execute('id')->then(function ($result) {
var_dump('current user', $result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->execute('env')->then(function ($env) {
var_dump('env', $env);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->end();
6 changes: 3 additions & 3 deletions examples/php.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

use Clue\React\Shell\ProcessLauncher;

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

$launcher = new ProcessLauncher();
$launcher = new Clue\React\Shell\ProcessLauncher();

$shell = $launcher->createDeferredShell('php -a');
$shell->setBounding("echo '{{ bounding }}';");
Expand All @@ -19,6 +17,8 @@
CODE
)->then(function ($output) {
echo 'Program output: ' . PHP_EOL . $output . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$shell->end();