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 | |
||
$name | string | The name of the command. |
$arguments | array | |
(Optional) arguments for the command. |
$options | array | |
(Optional) options for the command. |
return |
/** * @covers \Clinner\Command\Command::create */ public function testStaticCreateWithArgsAndOpts() { $name = 'ls'; $args = array('first' => 'value', 'second' => 'second value'); $opts = array('some_option' => 'some value'); $command = Command::create($name, $args, $opts); $this->assertInstanceOf('\\Clinner\\Command\\Command', $command); $this->assertAttributeEquals($name, '_name', $command); $this->assertAttributeInstanceOf('\\Clinner\\ValueHolder', '_arguments', $command); $this->assertAttributeInstanceOf('\\Clinner\\ValueHolder', '_options', $command); $this->assertAttributeEmpty('_next', $command); $this->assertAttributeEmpty('_exitCode', $command); $this->assertAttributeEmpty('_output', $command); }
/** * 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; }
<?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";