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 (nahuelcuestaluengo@gmail.com)
Inheritance: implements Clinner\Command\CommandInterface, implements Clinner\Command\PipingCommandInterface, implements PipeableCommandInterface
示例#1
0
 /**
  * @covers \Clinner\Command\Command::fromString
  * @covers \Clinner\Command\Command::parse
  */
 public function testStaticFromStringManyCommands()
 {
     $commandString = 'cat ~/test.html | tr -s " " | cut -d: -f2 | wc';
     $commandsCount = 4;
     $command = Command::fromString($commandString);
     $this->assertInstanceOf('\\Clinner\\Command\\Command', $command);
     // Test that the inverse function also works
     $this->assertEquals($commandString, $command->toCommandString(true));
     $count = 0;
     while (null !== $command) {
         $count++;
         $command = $command->getPipedCommand();
     }
     $this->assertEquals($commandsCount, $count);
 }
示例#2
0
 /**
  * Pygmentize $code using a local binary.
  *
  * @param  string $language The language in which $code is written.
  * @param  string $code     The code to pygmentize.
  *
  * @return string
  */
 protected function runLocally($language, $code)
 {
     $args = array('-l' => $language, '-f' => 'html');
     $opts = array('delimiter' => ' ');
     $response = Command::create(self::PYGMENTS_BINARY_NAME, $args, $opts)->run($code)->getOutput();
     return $response;
 }
示例#3
0
文件: test.php 项目: ncuesta/clinner
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Clinner\Command\Command;
use Clinner\Command\Callback;
$callbackCommand = new Callback(function ($input) {
    foreach (explode("\n", $input) as $line) {
        if (false !== strchr($line, 'x')) {
            echo "{$line}\n";
        }
    }
});
$systemUsersCommand = Command::create('cat', array('/etc/passwd'))->pipe(Command::create('grep', array('-v' => '^#'), array('delimiter' => ' ')))->pipe(Command::create('cut', array('-d' => ':', '-f' => 1), array('delimiter' => '')))->pipe($callbackCommand);
echo $systemUsersCommand->toCommandString(true) . "\n";
echo "\n";
$systemUsers = $systemUsersCommand->run()->getOutputAsArray("\n");
print_r($systemUsers);
echo "\n";
$testCommandString = 'cat /etc/hosts | grep localhost | tr -s " \\t" -';
$testCommandFromString = Command::fromString($testCommandString);
echo $testCommandFromString->toCommandString(true) . "\n";
echo $testCommandFromString->run()->getOutput() . "\n";