<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; $cmd = Command::factory('sleep 2 && echo "OK"', true)->run(); echo $cmd->getStdOut(); echo "Finished in " . $cmd->getDuration() . " second(s)\n"; echo " (with microseconds: " . $cmd->getDuration(true) . ")\n";
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; echo "Type some words, one per line, then press CTRL-D and they will be sorted:\n"; $cmd = Command::factory("sort")->run(STDIN); echo "\n"; echo $cmd->getStdOut();
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; $filename = __DIR__ . '/../README.md'; $stdin = fopen($filename, 'r'); // This will read README.md and grep for lines containing 'the' $cmd = Command::factory("grep 'the'")->setCallback(function ($pipe, $data) { // Change the text to uppercase $data = strtoupper($data); if ($pipe === Command::STDERR) { Command::echoStdErr($data); } else { echo $data; } })->run($stdin); fclose($stdin);
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; $stdin = "banana\norange\napple\npear\n"; $cmd = Command::factory("sort")->run($stdin); echo $cmd->getStdOut();
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; // We don't want to escape this command since it would break it $no_escape = true; // This example probably only works in real BASH on Linux/UNIX // It will output a sorted list of directories in the $PATH $cmd = Command::factory('IFS=":"; (for DIR in $PATH; do echo $DIR; done) | sort', $no_escape)->run(); echo $cmd->getStdOut(); echo "Done.\n";
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; $cmd = Command::factory('ls')->setCallback(function ($pipe, $data) { echo $data; })->setDirectory('/tmp')->option('-l')->run(); echo "\nCommand '{$cmd}' Finished.\n";
<?php use kamermans\Command\Command; require_once __DIR__ . '/../vendor/autoload.php'; $filename = __DIR__ . '/../README.md'; $stdin = fopen($filename, 'r'); // This will count the number of words in the README.md file $cmd = Command::factory("wc")->option("--words")->run($stdin); fclose($stdin); $words = trim($cmd->getStdOut()); echo "File {$filename} contains {$words} words\n";
// After this many callbacks, stop the command $max_lines = 20; // This will be incremented on each callback $lines = 0; // This is used in setCallback() to break on each line $use_lines = true; // This callback will be called for every line of output $output_callback = function ($pipe, $data) use($max_lines, &$lines) { if ($pipe === Command::STDERR) { if ($data === null) { // We've hit EOF on STDERR return; } // send everything from STDERR to our STDERR Command::echoStdErr("STDERR: " . strlen($data) . "\n"); return; } // We've reached EOF on STDOUT if ($data === null) { return; } echo $data; $lines++; // If we return "false" all pipes will be closed if ($lines >= $max_lines) { return false; } }; // Create the command, assign the callback and run it $cmd = Command::factory('cat /var/log/syslog')->setCallback($output_callback, $use_lines)->run();