PHP Класс 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 '='.
Автор: José Nahuel Cuesta Luengo ([email protected])
Наследование: implements Clinner\Command\CommandInterface, implements Clinner\Command\PipingCommandInterface, implements PipeableCommandInterface
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
_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.

Описание методов

__construct() публичный Метод

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() публичный Метод

Get the string representation of this command.
public __toString ( ) : string
Результат string

_run() защищенный Метод

Return the exit code for such execution.
protected _run ( string $input ) : integer
$input string Input to this command.
Результат integer

create() публичный статический Метод

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.
Результат Command

fromString() публичный статический Метод

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.
Результат Clinner\Command\CommandInterface

getArguments() публичный Метод

Get the arguments for this command as a ValueHolder.
public getArguments ( ) : ValueHolder
Результат Clinner\ValueHolder

getErrorOutput() публичный Метод

Get the error output for this command.
public getErrorOutput ( ) : string
Результат string

getExitCode() публичный Метод

Get the exit code for this command.
public getExitCode ( ) : integer
Результат integer

getName() публичный Метод

Get the name of this command.
public getName ( ) : string
Результат string

getOption() публичный Метод

Get a single option value for this command, optionally providing a default value for it.
См. также: 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.
Результат mixed

getOptions() публичный Метод

Get the options for this command as a ValueHolder.
public getOptions ( ) : ValueHolder
Результат Clinner\ValueHolder

getOutput() публичный Метод

Get this command's output.
public getOutput ( ) : string
Результат string

getOutputAsArray() публичный Метод

If no output has been generated by this command, an empty array will be returned.
См. также: explode
public getOutputAsArray ( string $delimiter = ' ' ) : array
$delimiter string The boundary string.
Результат array

getPipedCommand() публичный Метод

Get the command piped to this one, if any.
public getPipedCommand ( ) : Clinner\Command\PipeableCommandInterface
Результат Clinner\Command\PipeableCommandInterface

hasPipedCommand() публичный Метод

Answer whether this command has a command piped to it.
public hasPipedCommand ( ) : boolean
Результат boolean

parse() защищенный статический Метод

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.
Результат Clinner\Command\CommandInterface

pipe() публичный Метод

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.
Результат Clinner\Command\PipingCommandInterface This instance, for a fluent API.

run() публичный Метод

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.
Результат Command This instance, for a fluent API.

setArguments() публичный Метод

$arguments might either be an array or a ValueHolder.
См. также: Clinner\ValueHolder::create()
public setArguments ( ValueHolder | array $arguments ) : Command
$arguments Clinner\ValueHolder | array The arguments for this command.
Результат Command This instance, for a fluent API.

setName() публичный Метод

Set the name of this command to $name.
public setName ( string $name ) : Command
$name string The name to set.
Результат Command This instance, for a fluent API.

setOption() публичный Метод

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.
Результат Command This instance, for a fluent API.

setOptions() публичный Метод

$options might either be an array or a ValueHolder.
См. также: Clinner\ValueHolder::create()
public setOptions ( ValueHolder | array $options ) : Command
$options Clinner\ValueHolder | array The options for this command.
Результат Command This instance, for a fluent API.

toCommandString() публичный Метод

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.
Результат string