digipolisgent/command-builder
PHP command builder to compose complex shell command strings fluently. Add flags/arguments, pipe output, and chain onSuccess/onFailure blocks to build conditional command groups for safe execution and readable CLI scripting.
Installation:
composer require digipolisgent/command-builder
Add to composer.json under require if not using Composer directly.
First Use Case: Build a simple command chain:
use DigipolisGent\CommandBuilder\CommandBuilder;
$builder = CommandBuilder::create('ls')
->addArgument('path/to/dir')
->pipeOutputTo('grep')
->addArgument('pattern');
echo $builder; // Outputs: `ls path/to/dir | grep pattern`
Where to Look First:
CommandBuilder class: Core builder methods (create(), addArgument(), addFlag(), etc.).README.md: Example workflows (e.g., success/failure handling).tests/: Real-world usage patterns (if tests exist).Basic Command Chaining:
CommandBuilder::create('git')
->addArgument('pull')
->pipeOutputTo('grep')
->addArgument('error');
Use case: Log parsing or error detection.
Conditional Execution:
CommandBuilder::create('phpunit')
->onSuccess('notify')
->addArgument('--success')
->onFailure('notify')
->addArgument('--failure');
Use case: CI/CD notifications or rollback triggers.
Nested Commands:
CommandBuilder::create('mkdir')
->addArgument('temp')
->onSuccess(CommandBuilder::create('cp')
->addArgument('file.txt')
->pipeOutputTo('temp/')
);
Use case: Multi-step operations (e.g., setup + action).
Flag/Option Handling:
CommandBuilder::create('docker')
->addFlag('rm')
->addFlag('f') // Force
->addArgument('container');
Use case: CLI tools with optional flags (e.g., docker-compose).
Environment Integration:
Combine with Laravel’s Artisan or Process facade:
$command = CommandBuilder::create('php')
->addArgument('artisan')
->addArgument('migrate')
->getCommand(); // Returns raw string
Process::run($command);
Output Escaping:
addArgument("file path")) may break.addArgument('file path') (single quotes in output) or escape manually:
->addArgument("file\\ path"); // Escaped for shell
Nested Command Delimiters:
onSuccess/onFailure can confuse shell parsing.No Direct Process Execution:
Process or exec().Deprecated Methods:
Inspect Raw Output:
echo $builder->getCommand(); // Debug the generated shell string.
Validate Syntax:
echo "ls -a | grep test" | bash (sanity check).Logging:
Log::debug('Generated command:', ['command' => $builder->getCommand()]);
Custom Command Classes:
Extend CommandBuilder for domain-specific methods:
class ArtisanCommandBuilder extends CommandBuilder {
public function migrate(): self {
return $this->addArgument('migrate');
}
}
Dynamic Argument Injection:
Use Laravel’s Str or Arr helpers to build arguments dynamically:
$args = collect(['--env=local', '--force']);
$builder->addArguments($args->toArray());
Integration with Laravel Tasks: Wrap in a service class for reusability:
class DeploymentCommand {
public function build(): CommandBuilder {
return CommandBuilder::create('deploy')
->addFlag('prod')
->onFailure('slack')
->addArgument('--alert');
}
}
Environment Variables:
Replace placeholders with Laravel’s .env:
->addArgument(env('APP_STORAGE_PATH'));
How can I help you explore Laravel packages today?