Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ composer require utopia-php/console
require_once __DIR__.'/vendor/autoload.php';

use Utopia\Console;
use Utopia\Command;

Console::success('Ready to work!');

Expand All @@ -28,7 +29,11 @@ if ($answer !== 'y') {
}

$output = '';
$exitCode = Console::execute('php -r "echo \"Hello\";"', '', $output, 3);
$stderr = '';
$command = new Command(PHP_BINARY)
->option('-r', 'echo "Hello";');

$exitCode = Console::execute($command, '', $output, $stderr, 3);

Console::log("Command returned {$exitCode} with: {$output}");
```
Expand All @@ -45,17 +50,65 @@ Console::error('Red log'); // stderr

### Execute Commands

`Console::execute()` returns the exit code and writes the combined stdout/stderr output into the third argument. Pass a timeout (in seconds) to stop long-running processes and an optional progress callback to stream intermediate output.
`Console::execute()` returns the exit code and writes stdout and stderr into the referenced output variables. Pass a timeout (in seconds) to stop long-running processes and an optional progress callback to stream intermediate output. Prefer `Utopia\Command` or argv arrays when you want structured command building.

```php
$command = new Command(PHP_BINARY)
->option('-r', 'fwrite(STDOUT, "success\\n");');

$output = '';
$input = '';
$exitCode = Console::execute('>&1 echo "success"', $input, $output, 3);
$stderr = '';
$exitCode = Console::execute($command, $input, $output, $stderr, 3);

echo $exitCode; // 0
echo $output; // "success\n"
```

### Build Commands

Use `flag()` for switches without a value, `option()` for keys that take a value, and `argument()` for positional arguments.

```php
$command = new Command('tar')
->flag('-cz')
->option('-f', 'archive.tar.gz')
->option('-C', '/tmp/project')
->argument('.');
```

### Compose Commands

Use the static helpers when you need shell operators such as pipes, `&&`, `||`, grouping, or redirects.

```php
$pipeline = Command::pipe(
new Command('ps')->flag('-ef'),
new Command('grep')->argument('php-fpm'),
new Command('wc')->flag('-l'),
);

$deploy = Command::and(
Command::group(
Command::or(
new Command('build'),
new Command('build:fallback'),
)
),
new Command('publish'),
);

$logs = Command::appendStdout(
Command::pipe(
new Command('cat')->argument('app.log'),
new Command('grep')->argument('ERROR'),
),
'errors.log',
);
```

Plain commands execute in argv mode. Composed, grouped, and redirected commands execute through shell syntax.

### Create a Daemon

Use `Console::loop()` to build daemons without tight loops. The helper sleeps between iterations and periodically triggers garbage collection.
Expand All @@ -72,7 +125,7 @@ Console::loop(function () {

## System Requirements

Utopia Console requires PHP 7.4 or later. We recommend using the latest PHP version whenever possible.
Utopia Console requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

## License

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"psr-4": {"Utopia\\": "src/"}
},
"require": {
"php": ">=8.0"
"php": ">=8.0",
"utopia-php/validators": "^0.1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
Expand Down
144 changes: 81 additions & 63 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading