PHPackages                             edrisa/command - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. edrisa/command

ActiveLibrary[CLI &amp; Console](/categories/cli)

edrisa/command
==============

External command runner / executor for PHP

11PHP

Since Oct 4Pushed 3y agoCompare

[ Source](https://github.com/edrisaturay/command)[ Packagist](https://packagist.org/packages/edrisa/command)[ RSS](/packages/edrisa-command/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Edrisa/command
==============

[](#edrisacommand)

[![Build Status](https://camo.githubusercontent.com/77f0b659ef56ffbe079484c4d2e4bc9d163bdac42600fda09fa748aaa0f17d74/68747470733a2f2f7472617669732d63692e6f72672f4564726973612f636f6d6d616e642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Edrisa/command)

External command runner / executor for PHP. This is an object oriented, robust replacement for `exec`, `shell_exec`, the backtick operator and the like.

**This package does not work reliably in Windows due to a lack of complete `proc_open()` support in PHP**

> This package is inspired by .

Running Commands
----------------

[](#running-commands)

At its simplest form, you can execute commands like this:

```
$cmd = Command::factory('ls')->run();
```

### Adding Arguments and Options

[](#adding-arguments-and-options)

Here we are safely adding arguments:

```
use Edrisa\Command\Command;

$cmd = Command::factory('/usr/bin/svn')
    ->option('--username', 'drslump')
    ->option('-r', 'HEAD')
    ->option('log')
    ->argument('http://code.google.com/drslump/trunk')
    ->run();

echo $cmd->getStdOut();
```

### Using a Callback for Incremental Updates

[](#using-a-callback-for-incremental-updates)

Normally all command output is buffered and once the command completes you can access it. By using a callback, the output is buffered until the desired number of bytes is received (see `Command::setReadBuffer(int $bytes)`), then it is passed to your callback function:

```
use Edrisa\Command\Command;

$cmd = Command::factory('ls')
    ->setCallback(function($pipe, $data) {
        // Gets run for every 4096 bytes
        echo $data;
    })
    ->setReadBuffer(4096)
    ->setDirectory('/tmp')
    ->option('-l')
    ->run();
```

Alternately, you can set the second argument for `Command::run(string $stdin, bool $lines)` to `true` to execute your callback once for every line of output:

```
use Edrisa\Command\Command;

$cmd = Command::factory('ls')
    ->setCallback(function($pipe, $data){
        // Gets run for each line of output
        echo $data;
    })
    ->setDirectory('/tmp')
    ->option('-l')
    ->run(null, true);
```

### Streaming large command output

[](#streaming-large-command-output)

The STDOUT and STDERR is collected inside PHP by default. If you have a large amount of data to pass into the command, you should stream it in (see STDIN from a stream below). If you have a large amount of output from the command, you should stream it out using a callback:

```
use Edrisa\Command\Command;

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

$filename = __DIR__.'/../README.md';
$stdin = fopen($filename, 'r');

// This will read README.md and grep for lines containing 'the'
$cmd = Command::factory("grep 'the'")
    ->setCallback(function($pipe, $data) {
        // Change the text to uppercase
        $data = strtoupper($data);

        if ($pipe === Command::STDERR) {
            Command::echoStdErr($data);
        } else {
            echo $data;
        }
    })
    ->run($stdin);

fclose($stdin);
```

### Running a Command without Escaping

[](#running-a-command-without-escaping)

By default, the command passed to `Command::factory(string $command, bool $escape)` is escaped, so characters like `|` and `>` will replaced with `\|` and `\>` respectively. To prevent the command factory from escaping your command, you can pass `true` as the second argument:

```
use Edrisa\Command\Command;

$cmd = Command::factory('grep CRON < /var/log/syslog | head', true)->run();

echo $cmd->getStdOut();
```

### Outputting to STDERR

[](#outputting-to-stderr)

To output content to your `STDERR` there is a helper function `Command::echoStdErr(string $content)`:

```
use Edrisa\Command\Command;

$cmd = Command::factory('grep CRON < /var/log/syslog | head', true)
    ->setCallback(function($pipe,$data) {
        if ($pipe === Command::STDERR) {
            Command::echoStdErr($data);
        } else {
            echo $data;
        }
    })
    ->run();
```

Using STDIN
-----------

[](#using-stdin)

You can provide data for STDIN using a string or a stream resource (like a file handle)

### STDIN from a String

[](#stdin-from-a-string)

```
use Edrisa\Command\Command;

$stdin = "banana
orange
apple
pear
";

$cmd = Command::factory("sort")
    ->run($stdin);

echo $cmd->getStdOut();
```

### STDIN from a Stream

[](#stdin-from-a-stream)

```
use Edrisa\Command\Command;

$filename = __DIR__.'/../README.md';
$stdin = fopen($filename, 'r');

// This will count the number of words in the README.md file
$cmd = Command::factory("wc")
    ->option("--words")
    ->run($stdin);

fclose($stdin);

$words = trim($cmd->getStdOut());
echo "File $filename contains $words words\n";
```

Your system's `STDIN` is also a stream, so you can accept input that is typed on the command line or piped into your script as well:

```
use Edrisa\Command\Command;

echo "Type some words, one per line, then press CTRL-D and they will be sorted:\n";

$cmd = Command::factory("sort")
    // This causes Command to use the real STDIN
    ->run(STDIN);

echo "\n";
echo $cmd->getStdOut();
```

Some more features:

- `StdIn` data can be provided to the process as a parameter to `run()`
- Set environment variables for the process with `setEnv()`
- Second argument to `option()` and argument to `argument()` are automatically escaped.
- Options separator is white space by default, it can be changed by manually setting it as third argument to `option()` or setting a new default with `setOptionSeparator()`.
- The `proc_open` wrapper is exposed as a static method for your convenience `Command::exec()`

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity24

Early-stage or recently created project

 Bus Factor1

Top contributor holds 95.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/95329b757401f7d19594b60cf196fef575ef365863207549dd320a28d03172e7?d=identicon)[edrisaa.turay](/maintainers/edrisaa.turay)

---

Top Contributors

[![kamermans](https://avatars.githubusercontent.com/u/266265?v=4)](https://github.com/kamermans "kamermans (41 commits)")[![edrisaturay](https://avatars.githubusercontent.com/u/21174548?v=4)](https://github.com/edrisaturay "edrisaturay (1 commits)")[![lucor](https://avatars.githubusercontent.com/u/313577?v=4)](https://github.com/lucor "lucor (1 commits)")

### Embed Badge

![Health badge](/badges/edrisa-command/health.svg)

```
[![Health](https://phpackages.com/badges/edrisa-command/health.svg)](https://phpackages.com/packages/edrisa-command)
```

###  Alternatives

[wp-cli/wp-cli

WP-CLI framework

5.0k17.2M320](/packages/wp-cli-wp-cli)[consolidation/annotated-command

Initialize Symfony Console commands from annotated command class methods.

22569.8M19](/packages/consolidation-annotated-command)[chi-teck/drupal-code-generator

Drupal code generator

26947.8M5](/packages/chi-teck-drupal-code-generator)[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24725.8M17](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

12944.1M5.1k](/packages/illuminate-console)[php-tui/php-tui

Comprehensive TUI library heavily influenced by Ratatui

589747.0k6](/packages/php-tui-php-tui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
