You are here

public static function EasyRdf_Utils::execCommandPipe in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Utils.php \EasyRdf_Utils::execCommandPipe()

Execute a command as a pipe

The proc_open() function is used to open a pipe to a a command line process, writing $input to STDIN, returning STDOUT and throwing an exception if anything is written to STDERR or the process returns non-zero.

Parameters

string $command The command to execute:

array $args Optional list of arguments to pass to the command:

string $input Optional buffer to send to the command:

string $dir Path to directory to run command in (defaults to /tmp):

Return value

string The result of the command, printed to STDOUT

3 calls to EasyRdf_Utils::execCommandPipe()
EasyRdf_Parser_Rapper::parse in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Rapper.php
Parse an RDF document into an EasyRdf_Graph
EasyRdf_Serialiser_GraphViz::renderImage in vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/GraphViz.php
Internal function to render a graph into an image
EasyRdf_Serialiser_Rapper::serialise in vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/Rapper.php
Serialise an EasyRdf_Graph to the RDF format of choice.

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Utils.php, line 233

Class

EasyRdf_Utils
Class containing static utility functions

Code

public static function execCommandPipe($command, $args = null, $input = null, $dir = null) {
  $descriptorspec = array(
    0 => array(
      'pipe',
      'r',
    ),
    1 => array(
      'pipe',
      'w',
    ),
    2 => array(
      'pipe',
      'w',
    ),
  );

  // Use the system tmp directory by default
  if (!$dir) {
    $dir = sys_get_temp_dir();
  }
  if (is_array($args)) {
    $fullCommand = implode(' ', array_map('escapeshellcmd', array_merge(array(
      $command,
    ), $args)));
  }
  else {
    $fullCommand = escapeshellcmd($command);
    if ($args) {
      $fullCommand .= ' ' . escapeshellcmd($args);
    }
  }
  $process = proc_open($fullCommand, $descriptorspec, $pipes, $dir);
  if (is_resource($process)) {

    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // 2 => readable handle connected to child stderr
    if ($input) {
      fwrite($pipes[0], $input);
    }
    fclose($pipes[0]);
    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    $error = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $returnValue = proc_close($process);
    if ($returnValue) {
      throw new EasyRdf_Exception("Error while executing command {$command}: " . $error);
    }
  }
  else {
    throw new EasyRdf_Exception("Failed to execute command {$command}");
  }
  return $output;
}