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. |
return | Clinner\Command\CommandInterface |
/** * @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); }
<?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";