You are here

private static function cf_error::p_generate_backtrace in Common Functionality 7.2

Safely generates backtrace string.

Alternate approachs use print_r() or var_export(), but these are prone to memory recursion problems.

This has a maximum recursion of 3.

Parameters

array $backtrace: A backtrace array to convert to a string.

Return value

string The generated string.

1 call to cf_error::p_generate_backtrace()
cf_error::p_print_message in modules/cf_error/classes/cf_error.php
Prints error messages to the screen.

File

modules/cf_error/classes/cf_error.php, line 1602
Provides the derror exception class.

Class

cf_error

Code

private static function p_generate_backtrace(&$backtrace, $depth = 0) {
  $string = "";
  $length = $depth * 4 + 2;
  if ($depth == 0) {
    $string = "\n";
    foreach ($backtrace as $key => &$value) {
      $string .= str_repeat(' ', 2) . '[' . $key . '] => Array ' . "\n";
      $string .= str_repeat(' ', 4) . '(' . "\n";
      $string .= self::p_generate_backtrace($value, $depth + 1);
      $string .= str_repeat(' ', 4) . ')' . "\n";
    }
  }
  else {
    foreach ($backtrace as $key => &$value) {
      if (is_array($value)) {
        if ($depth + 1 > 4) {
          $string .= str_repeat(' ', $length) . '[' . $key . '] => Array (not displayed)' . "\n";
        }
        else {
          $string .= str_repeat(' ', $length) . '[' . $key . '] => Array ' . "\n";
          $string .= str_repeat(' ', $length + 2) . '(' . "\n";
          $string .= self::p_generate_backtrace($value, $depth + 1);
          $string .= str_repeat(' ', $length + 2) . ')' . "\n";
        }
      }
      elseif (is_object($value)) {
        if ($depth + 1 > 4) {
          $string .= str_repeat(' ', $length) . '[' . $key . '] => Object (not displayed)' . "\n";
        }
        else {
          $object = (array) get_object_vars($value);
          $string .= str_repeat(' ', $length) . '[' . $key . '] => Object ' . "\n";
          $string .= str_repeat(' ', $length + 2) . '(' . "\n";
          $string .= self::p_generate_backtrace($object, $depth + 1);
          $string .= str_repeat(' ', $length + 2) . ')' . "\n";
        }
      }
      elseif (is_bool($value)) {
        $string .= str_repeat(' ', $length) . '[' . $key . '] => ' . ($value ? "TRUE" : "FALSE") . "\n";
      }
      else {
        $string .= str_repeat(' ', $length) . '[' . $key . '] => ' . print_r($value, TRUE) . "\n";
      }
    }
  }
  return $string;
}