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
파일 보기 프로젝트 열기: ncuesta/clinner 1 사용 예제들

공개 메소드들

메소드 설명
__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