You are here

function httprl_run_function in HTTP Parallel Request & Threading Library 6

Same name and namespace in other branches
  1. 7 httprl.module \httprl_run_function()

Run a single function.

Parameters

string $function: Name of function to run.

array $input_args: list of arguments to pass along to the function.

1 call to httprl_run_function()
httprl_async_page in ./httprl.async.inc
Menu Callback; run given function.

File

./httprl.module, line 3174
HTTP Parallel Request Library module.

Code

function httprl_run_function($function, &$input_args) {

  // Pass by reference trick for call_user_func_array().
  $args = array();
  foreach ($input_args as &$arg) {
    $args[] =& $arg;
  }

  // Capture anything printed out.
  ob_start();

  // Start to capture errors.
  $track_errors = ini_set('track_errors', '1');
  $php_errormsg = '';

  // Run function.
  $return = NULL;

  // Do not let an exception cause issues.
  try {
    if (function_exists($function)) {
      $return = call_user_func_array($function, $args);
    }
    else {
      $php_errormsg = 'Recoverable Fatal error: Call to undefined function ' . $function . '()';
    }
  } catch (Exception $e) {
    $php_errormsg = $e;
  }
  $printed = ob_get_contents();
  ob_end_clean();

  // Create data array.
  $data = array(
    'return' => $return,
    'args' => $args,
    'printed' => $printed,
  );

  // Set any errors if any where thrown.
  if (!empty($php_errormsg)) {
    $data['error'] = $php_errormsg;
    ini_set('track_errors', $track_errors);
    watchdog('httprl', 'Error thrown in httprl_run_function(). <br /> @error', array(
      '@error' => $php_errormsg,
    ), WATCHDOG_ERROR);
  }
  return $data;
}