function httprl_run_array in HTTP Parallel Request & Threading Library 7
Same name and namespace in other branches
- 6 httprl.module \httprl_run_array()
Run multiple functions or methods independently or chained.
Example for running a Drupal 6 Database query.
// Run 2 queries and get it's result.
$max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
$min = db_result(db_query('SELECT MIN(wid) FROM {watchdog}'));
echo $max . ' ' . $min;
// Doing the same thing as above but with a set of arrays.
$max = '';
$min = '';
$args = array(
array(
'type' => 'function',
'call' => 'db_query',
'args' => array(
'SELECT MAX(wid) FROM {watchdog}',
),
),
array(
'type' => 'function',
'call' => 'db_result',
'args' => array(
'last' => NULL,
),
'return' => &$max,
),
array(
'type' => 'function',
'call' => 'db_query',
'args' => array(
'SELECT MIN(wid) FROM {watchdog}',
),
),
array(
'type' => 'function',
'call' => 'db_result',
'args' => array(
'last' => NULL,
),
'return' => &$min,
),
);
httprl_run_array($args);
echo $max . ' ' . $min;
Example for running a Drupal 7 Database query.
// Run a query and get it's result.
$min = db_select('watchdog', 'w')
->fields('w', array(
'wid',
))
->orderBy('wid', 'DESC')
->range(999, 1)
->execute()
->fetchField();
echo $min;
// Doing the same thing as above but with a set of arrays.
$min = '';
$args = array(
array(
'type' => 'function',
'call' => 'db_select',
'args' => array(
'watchdog',
'w',
),
),
array(
'type' => 'method',
'call' => 'fields',
'args' => array(
'w',
array(
'wid',
),
),
),
array(
'type' => 'method',
'call' => 'orderBy',
'args' => array(
'wid',
'DESC',
),
),
array(
'type' => 'method',
'call' => 'range',
'args' => array(
999,
1,
),
),
array(
'type' => 'method',
'call' => 'execute',
'args' => array(),
),
array(
'type' => 'method',
'call' => 'fetchField',
'args' => array(),
'return' => &$min,
),
);
httprl_run_array($args);
echo $min;
Parameters
array $array: 2 dimensional array array( array( 'type' => function or method 'call' => function name or name of object method 'args' => array( List of arguments to pass in. If you set the key to last, the return value of the last thing ran will be put in this place. 'last' => NULL ), 'return' => what was returned from this call. 'printed' => what was printed from this call. 'error' => any errors that might have occurred. 'last' => set the last variable to anything. ) )
1 call to httprl_run_array()
- httprl_async_page in ./
httprl.async.inc - Menu Callback; run given function.
File
- ./
httprl.module, line 3153 - HTTP Parallel Request Library module.
Code
function httprl_run_array(&$array) {
$last = NULL;
foreach ($array as &$data) {
// Skip if no type is set.
if (!isset($data['type'])) {
continue;
}
// Set the last variable if so desired.
if (isset($data['last'])) {
$last = $data['last'];
}
// Replace the last key with the last thing that has been returned.
if (isset($data['args']) && array_key_exists('last', $data['args'])) {
$data['args']['last'] = $last;
$data['args'] = array_values($data['args']);
}
// Capture output if requested.
if (array_key_exists('printed', $data)) {
ob_start();
}
// Pass by reference trick for call_user_func_array().
$args = array();
if (isset($data['args']) && is_array($data['args'])) {
foreach ($data['args'] as &$arg) {
$args[] =& $arg;
}
}
// Start to capture errors.
$track_errors = ini_set('track_errors', '1');
$php_errormsg = '';
// Call a function or a method.
switch ($data['type']) {
case 'function':
if (function_exists($data['call'])) {
$last = call_user_func_array($data['call'], $args);
}
else {
$php_errormsg = 'Recoverable Fatal error: Call to undefined function ' . $data['call'] . '()';
}
break;
case 'method':
if (method_exists($last, $data['call'])) {
$last = call_user_func_array(array(
$last,
$data['call'],
), $args);
}
else {
$php_errormsg = 'Recoverable Fatal error: Call to undefined method ' . get_class($last) . '::' . $data['call'] . '()';
}
break;
}
// 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_array(). <br /> @error', array(
'@error' => $php_errormsg,
), WATCHDOG_ERROR);
}
// End capture.
if (array_key_exists('printed', $data)) {
$data['printed'] = ob_get_contents();
ob_end_clean();
}
// Set what was returned from each call.
if (array_key_exists('return', $data)) {
$data['return'] = $last;
}
}
return array(
'args' => array(
$array,
),
);
}