A fluent PHP API for building and executing shell commands programmatically, for taking them under control, or in other words - commandeer them.
Install it into your project with composer:
composer require posternak/commandeerCommandeer provides a type-safe, fluent API for constructing and executing shell commands in PHP, replacing error-prone string concatenation with chainable method calls.
The foundation class that executes commands and captures results:
use Posternak\Commandeer\ShellCommand;
$command = new ShellCommand('git status');
$command->run();
if ($command->succeeded()) {
$output = $command->getOutput(); // array of lines
}API:
run(): self- Execute the commandsucceeded(): bool- Check if exit code was 0getOutput(): array- Get output linesgetCommand(): string- Get command string
The Cmd class provides a universal interface for building any shell command, it's the fluent API. To construct a command:
- Start with the
Cmdclass. - Call a static method whose name matches your desired executable (e.g.,
Cmd::docker()for thedockerexecutable). - Chain additional methods for CLI options and arguments, passing data as method parameters.
Method names are automatically converted to command-line syntax (underscores become dashes), and method arguments become command parameters.
Examples:
use Posternak\Commandeer\Builders\Cmd;
// docker ps -a
Cmd::docker()->ps()->_a()->run();
// kubectl get pods -o json
Cmd::kubectl()->get('pods')->_o('json')->run();
// npm install
Cmd::npm()->install()->run();
// git log --pretty oneline
Cmd::git()->log()->pretty('oneline')->run();API:
Cmd::{executable}()- Start building a command for any executable->{method}()- Add command arguments (underscores become dashes)->run()- Execute the command->getCommand()- Preview command string without executing
For commonly-used tools, predefined builders eliminate the need to specify the executable name:
use Posternak\Commandeer\Builders\Git;
use Posternak\Commandeer\Builders\Composer;
// Instead of Cmd::git()
Git::status()->porcelain()->run();
// Instead of Cmd::composer()
Composer::require('vendor/package')->run();Available builders: Git, Composer, Rector, PHPStan, PHPCsFixer, Php, Artisan
The Git builder includes shortcuts for common operations:
Git::addEverything(); // git add .
Git::commitWithMessage('feat: add feature'); // git commit -m "..."
Git::pushToOrigin('main'); // git push origin mainMIT