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
Afficher le fichier Open project: ncuesta/clinner Class Usage Examples

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

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 méthode

Get the string representation of this command.
public __toString ( ) : string
Résultat string

_run() protected méthode

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

create() public static méthode

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.
Résultat Command

fromString() public static méthode

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.
Résultat Clinner\Command\CommandInterface

getArguments() public méthode

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

getErrorOutput() public méthode

Get the error output for this command.
public getErrorOutput ( ) : string
Résultat string

getExitCode() public méthode

Get the exit code for this command.
public getExitCode ( ) : integer
Résultat integer

getName() public méthode

Get the name of this command.
public getName ( ) : string
Résultat string

getOption() public méthode

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.
Résultat mixed

getOptions() public méthode

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

getOutput() public méthode

Get this command's output.
public getOutput ( ) : string
Résultat string

getOutputAsArray() public méthode

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.
Résultat array

getPipedCommand() public méthode

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

hasPipedCommand() public méthode

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

parse() protected static méthode

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.
Résultat Clinner\Command\CommandInterface

pipe() public méthode

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.
Résultat Clinner\Command\PipingCommandInterface This instance, for a fluent API.

run() public méthode

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.
Résultat Command This instance, for a fluent API.

setArguments() public méthode

$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.
Résultat Command This instance, for a fluent API.

setName() public méthode

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

setOption() public méthode

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.
Résultat Command This instance, for a fluent API.

setOptions() public méthode

$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.
Résultat Command This instance, for a fluent API.

toCommandString() public méthode

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.
Résultat string