Exemplo n.º 1
0
<?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";
Exemplo n.º 2
0
<?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();
Exemplo n.º 3
0
<?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);
Exemplo n.º 4
0
<?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();
Exemplo n.º 5
0
<?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";
Exemplo n.º 6
0
<?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";
Exemplo n.º 7
0
<?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";
Exemplo n.º 8
0
// 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();