pipes_execute function

  1. /**
  2.  * Main pipe callback. Executes an array of steps.
  3.  * @param $steps
  4.  *   Either an array of steps, or the id of a pipe to load.
  5.  * @param $reorder
  6.  *   Defaults to FALSE. If set to TRUE, will cause the steps to be re-ordered.
  7.  * @return
  8.  *   Return value of the pipe. Unknown.
  9.  */
  10. function pipes_execute($steps, $reorder = FALSE) {
  11.   if (is_numeric($steps)) {
  12.     // It's a pid, so load the pipe.
  13.     $steps = pipes_load($steps);
  14.   }
  15.   // Convert to an array of necessary.
  16.   $steps = array($steps);
  17.   // Reorder the pipes if called for.
  18.   if ($reorder) {
  19.     $steps = pipes_order_steps($steps);
  20.   }
  21.   // Trim irrelevant data from the steps array in order to protect it during foreach() loop.
  22.   pipes_trim_steps($steps);
  23.  
  24.   $values = array();
  25.   $advance = TRUE;
  26.   reset($steps);
  27.   list($sid, $info) = each($steps);
  28.  
  29.   // Cycle through each of the steps in the pipe.
  30.   while ($sid) {
  31.     foreach ($info['step_data']['defaults'] as $key => $var) {
  32.       if (is_int($var)) {
  33.         $info['step_data']['defaults'][$key] = arg($var);
  34.       }
  35.     }
  36.     $value = call_user_func_array($info['class'], ($values[$sid] + $info['settings'] + $info['step_data']['defaults']));
  37.     $result = FALSE;
  38.     if (isset($info['step_data']['decision'])) {
  39.       foreach ($info['step_data']['decision'] as $evaluator => $doer) {
  40.         switch ($evaluator) {
  41.           // empty() and isset() are language constructs, not functions, so they cannot be called the same way.
  42.           case 'empty':
  43.             $result = empty($value);
  44.             break;
  45.           case 'isset':
  46.             $result = isset($value);
  47.             break;
  48.           default:
  49.             $result = $evaluator($value);
  50.             break;
  51.         }
  52.         if ($result) {
  53.           // First, determine whether $doer is a function or a value.
  54.           $matches = array();
  55.           preg_match('/(\w+)(\(\))$/', $doer, $matches);
  56.           if ($matches[2] == '()') {
  57.             // It's a function.
  58.             $function = $matches[1];
  59.             // Pass it all the information it wants.
  60.             $doer = $function($value, $steps, $values);
  61.           }
  62.           // Now it's definitely a value, so could either be (1) the sid of a step to go to, or (2) a return value.
  63.           if (is_int($doer)) {
  64.             // It's an sid. Set the appropriate step as the current step.
  65.             reset($steps);
  66.             while ((list($sid, $info) = each($steps)) && $sid != $doer);
  67.             $advance = FALSE;
  68.           }
  69.           else {
  70.             // It's just a simple value to return.
  71.             return $doer;
  72.           }
  73.           if ($advance) {
  74.             // We need to advance the array.
  75.             reset($steps);
  76.             list($sid, $info) = each($steps);
  77.           }
  78.           break;
  79.         }
  80.       }
  81.     }
  82.     // Terminate if this is a terminating step.
  83.     if (isset($info['terminate'])) {
  84.       if (!$info['terminate']) {
  85.         return $value;
  86.       }
  87.     }
  88.     if (!is_null($info['destination'])) {
  89.       foreach ($info['destination'] as $destination_data) {
  90.         $values[$destination_data['step']][$destination_data['param']] = $value;
  91.       }
  92.     }
  93.   }
  94.  
  95.   // Return final value, if we reach here.
  96.   return $value;
  97. }