php-shellcommand provides a simple object oriented interface to execute shell commands.
- Catches
stdOut,stdErrandexitCode - Handle argument escaping
- Pass environment vars and other options to
proc_open()
<?php
use mikehaertl\shellcommand\Command;
// Basic example
$command = new Command('/usr/local/bin/mycommand -a -b');
if ($command->execute()) {
echo $command->getOutput();
} else {
echo $command->getError();
$exitCode = $command->getExitCode();
}// Create command with options array
$command = new Command(array(
'command' => '/usr/local/bin/mycommand',
// Will be passed as environment variables to the command
'procEnv' => array(
'DEMOVAR' => 'demovalue'
),
// Will be passed as options to proc_open()
'procOptions' => array(
'bypass_shell' => true,
),
));
// Add arguments with correct escaping:
// results in --name='d'\''Artagnan'
$command->addArg('--name=', "d'Artagnan");
// Add argument with several values
// results in --keys key1 key2
$command->addArg('--keys', array('key1','key2')$escapeArgs: Whether to escape any argument passed throughaddArg(). Default istrue.$escapeCommand: Whether to escape the command passed tosetCommand()or the constructor. This is only useful if$escapeArgsisfalse. Default isfalse.$procCwd: The initial working dir passed toproc_open(). Default isnullfor current PHP working dir.$procEnv: An array with environment variables to pass toproc_open(). Default isnullfor none.$procOptions: An array ofother_optionsforproc_open(). Default isnullfor none.
__construct($options = null)$options: either a command string or an options array (seesetOptions())
setOptions($options): Set command options$options: array of name => value options that should be applied to the object. You can also pass options that use a setter, e.g. you can pass acommandoption which will be passed tosetCommand().
setCommand($command): Set command$command: The command or full command string to execute, likegziporgzip -d. You can still calladdArg()to add more arguments to the command. If$escapeCommandwas set totrue, the command gets escaped throughescapeshellcmd().
getCommand(): The command that was set throughsetCommand()or passed to the constructor.getExecCommand(): The full command string to execute.setArgs($args): Set argument as string$args: The command arguments as string. Note, that these will not get escaped!
getArgs(): The command arguments that where set throughsetArgs()oraddArg(), as stringaddArg($key, $value=null, $escape=null): Add argument with correct escaping$key: The argument key to add e.g.--featureor--name=. If the key does not end with and=, the$valuewill be separated by a space, if any. Keys are not escaped unless$valueis null and$escapeistrue.$value: The optional argument value which will get escaped if$escapeArgsis true. An array can be passed to add more than one value for a key, e.g.addArg('--exclude', array('val1','val2'))which will create the option "--exclude 'val1' 'val2'".$escape: If set, this overrides the$escapeArgssetting and enforces escaping/no escaping
getOutput(): The command output as string. Empty if none.getError(): The error message, either stderr or internal message. Empty if none.getStdErr(): The stderr output. Empty if none.getExitCode(): The exit code.getExecuted(): Whether the command was successfully executed.execute(): Executes the command and returnstrueon success,falseotherwhise.

