PHP Class Clinner\Command\Command

Command abstraction class that allows running any command with a given set of arguments. Usage examples: Run ls -l -a in the current directory $command = new \Clinner\Command\Command( 'ls', array('-l', '-a') ); $command->run(); echo $command->getExitCode(); echo $command->getOutput(); This example is equivalent to the one above $command = \Clinner\Command\Command::create( 'ls', array('-l', '-a') ) ->run(); echo $command->getExitCode(); echo $command->getOutput(); You can also pipe commands, just like in a command-line interface: use \Clinner\Command\Command; $grepCommand = Command::create( 'grep', array('-v' => '^#'), array('delimiter' => ' ') ); $cutCommand = Command::create( 'cut', array('-d' => ':', '-f' => 1), array('delimiter' => '') ); $systemUsers = Command::create( 'cat', array('/etc/passwd') ) ->pipe( $grepCommand->pipe( $cutCommand ) ) ->run() ->getOutputAsArray("\n"); This class may take the following options: * delimiter (string): A string that will be put in key-value pairs of arguments to separate the key from its value. Defaults to '='.
Author: José Nahuel Cuesta Luengo ([email protected])
Inheritance: implements Clinner\Command\CommandInterface, implements Clinner\Command\PipingCommandInterface, implements PipeableCommandInterface
Datei anzeigen Open project: ncuesta/clinner Class Usage Examples

Public Methods

Method Description
__construct ( string $name, array | ValueHolder $arguments = [], array | ValueHolder $options = [] ) Constructor.
__toString ( ) : string Get the string representation of this command.
create ( string $name, array | ValueHolder $arguments = [], array | ValueHolder $options = [] ) : Command Factory method for creating new Commands and allowing to take advantage of the fluent API provided by this class.
fromString ( string $commandString ) : Clinner\Command\CommandInterface Factory method for creating new Commands from their string representation, as if they were being run from the command line. If no valid commands are found, a NullCommand will be returned.
getArguments ( ) : ValueHolder Get the arguments for this command as a ValueHolder.
getErrorOutput ( ) : string Get the error output for this command.
getExitCode ( ) : integer Get the exit code for this command.
getName ( ) : string Get the name of this command.
getOption ( string $name, mixed $default = null ) : mixed Get a single option value for this command, optionally providing a default value for it.
getOptions ( ) : ValueHolder Get the options for this command as a ValueHolder.
getOutput ( ) : string Get this command's output.
getOutputAsArray ( string $delimiter = ' ' ) : array Get the output of this command as an array, splitting it with a given $delimiter.
getPipedCommand ( ) : Clinner\Command\PipeableCommandInterface Get the command piped to this one, if any.
hasPipedCommand ( ) : boolean Answer whether this command has a command piped to it.
pipe ( PipeableCommandInterface $anotherCommand, boolean $appendToPipe = true ) : Clinner\Command\PipingCommandInterface Pipe $anotherCommand to this one, so that this command's output is directly sent to $anotherCommand's standard input.
run ( string $input = null ) : Command Run this command with the given $input.
setArguments ( ValueHolder | array $arguments ) : Command Set this command's arguments as a whole.
setName ( string $name ) : Command Set the name of this command to $name.
setOption ( string $name, mixed $value ) : Command Set a single option for this command.
setOptions ( ValueHolder | array $options ) : Command Set this command's options as a whole.
toCommandString ( boolean $includePiped = false ) : string Get a string representation of this command with its arguments, as if it would be written in a command-line interface when run.

Protected Methods

Method Description
_run ( string $input ) : integer Actually run this command and its piped commands chain, if applicable.
parse ( string $commandString, array $options = [] ) : Clinner\Command\CommandInterface Parse a command from a string representation and return the resulting object.

Method Details

__construct() public method

Constructor.
public __construct ( string $name, array | ValueHolder $arguments = [], array | ValueHolder $options = [] )
$name string The name of the command.
$arguments array | Clinner\ValueHolder (Optional) arguments for the command.
$options array | Clinner\ValueHolder (Optional) options for the command.

__toString() public method

Get the string representation of this command.
public __toString ( ) : string
return string

_run() protected method

Return the exit code for such execution.
protected _run ( string $input ) : integer
$input string Input to this command.
return integer

create() public static method

Factory method for creating new Commands and allowing to take advantage of the fluent API provided by this class.
public static create ( string $name, array | ValueHolder $arguments = [], array | ValueHolder $options = [] ) : Command
$name string The name of the command.
$arguments array | Clinner\ValueHolder (Optional) arguments for the command.
$options array | Clinner\ValueHolder (Optional) options for the command.
return Command

fromString() public static method

For example: $command = Command::fromString('cat /etc/hosts | grep localhost'); Is roughly equivalent to: $command = Command::create('cat', array('/etc/hosts')) ->pipe(Command::create('grep', array('localhost')));
public static fromString ( string $commandString ) : Clinner\Command\CommandInterface
$commandString string The command string to parse.
return Clinner\Command\CommandInterface

getArguments() public method

Get the arguments for this command as a ValueHolder.
public getArguments ( ) : ValueHolder
return Clinner\ValueHolder

getErrorOutput() public method

Get the error output for this command.
public getErrorOutput ( ) : string
return string

getExitCode() public method

Get the exit code for this command.
public getExitCode ( ) : integer
return integer

getName() public method

Get the name of this command.
public getName ( ) : string
return string

getOption() public method

Get a single option value for this command, optionally providing a default value for it.
See also: Clinner\ValueHolder::get()
public getOption ( string $name, mixed $default = null ) : mixed
$name string The name of the option.
$default mixed The default value for the option, in case it isn't set.
return mixed

getOptions() public method

Get the options for this command as a ValueHolder.
public getOptions ( ) : ValueHolder
return Clinner\ValueHolder

getOutput() public method

Get this command's output.
public getOutput ( ) : string
return string

getOutputAsArray() public method

If no output has been generated by this command, an empty array will be returned.
See also: explode
public getOutputAsArray ( string $delimiter = ' ' ) : array
$delimiter string The boundary string.
return array

getPipedCommand() public method

Get the command piped to this one, if any.
public getPipedCommand ( ) : Clinner\Command\PipeableCommandInterface
return Clinner\Command\PipeableCommandInterface

hasPipedCommand() public method

Answer whether this command has a command piped to it.
public hasPipedCommand ( ) : boolean
return boolean

parse() protected static method

If no valid command is found, a NullCommand will be returned.
protected static parse ( string $commandString, array $options = [] ) : Clinner\Command\CommandInterface
$commandString string The command string to parse.
$options array (Optional) options for the command.
return Clinner\Command\CommandInterface

pipe() public method

Pipe $anotherCommand to this one, so that this command's output is directly sent to $anotherCommand's standard input.
public pipe ( PipeableCommandInterface $anotherCommand, boolean $appendToPipe = true ) : Clinner\Command\PipingCommandInterface
$anotherCommand PipeableCommandInterface The command to pipe.
$appendToPipe boolean Whether $anotherCommand will be appended to the currently piped commands (TRUE) or if it will be added after this command, rearranging the commands pipe to include it.
return Clinner\Command\PipingCommandInterface This instance, for a fluent API.

run() public method

If this command has any other command piped to it, the other command will also be run as well, with this command's output as its input.
public run ( string $input = null ) : Command
$input string (Optional) input for this command.
return Command This instance, for a fluent API.

setArguments() public method

$arguments might either be an array or a ValueHolder.
See also: Clinner\ValueHolder::create()
public setArguments ( ValueHolder | array $arguments ) : Command
$arguments Clinner\ValueHolder | array The arguments for this command.
return Command This instance, for a fluent API.

setName() public method

Set the name of this command to $name.
public setName ( string $name ) : Command
$name string The name to set.
return Command This instance, for a fluent API.

setOption() public method

Set a single option for this command.
public setOption ( string $name, mixed $value ) : Command
$name string Name of the option to set.
$value mixed Value for that option.
return Command This instance, for a fluent API.

setOptions() public method

$options might either be an array or a ValueHolder.
See also: Clinner\ValueHolder::create()
public setOptions ( ValueHolder | array $options ) : Command
$options Clinner\ValueHolder | array The options for this command.
return Command This instance, for a fluent API.

toCommandString() public method

Get a string representation of this command with its arguments, as if it would be written in a command-line interface when run.
public toCommandString ( boolean $includePiped = false ) : string
$includePiped boolean (Optional) indicates whether the resulting string will include any piped command to this one. Defaults to FALSE.
return string