You are here

protected static function AutoloadCache::exportVariable in Autoload 7.2

Export a variable as a well-formatted (in terms of Drupal CS) string.

Parameters

mixed $var: Variable to dump into a string.

int $prefix: A number of spaces to put before the code.

bool $init: Internal variable to handle recursion.

Return value

string Dumped variable.

1 call to AutoloadCache::exportVariable()
AutoloadCache::updateFile in ./autoload.cache.inc
Dumps the autoloading class map to the file.

File

./autoload.cache.inc, line 306
Autoload cache controller.

Class

AutoloadCache
Class AutoloadCache.

Code

protected static function exportVariable($var, $prefix = 0, $init = TRUE) {
  if (is_array($var)) {
    if (empty($var)) {
      $output = 'array()';
    }
    else {
      $output = "array(\n";
      foreach ($var as $key => $value) {

        // Using normal "var_export()" on the key to ensure correct quoting.
        $output .= '  ' . var_export($key, TRUE) . ' => ' . call_user_func(__METHOD__, $value, 2, FALSE) . ",\n";
      }
      $output .= ')';
    }
  }
  elseif (is_bool($var)) {
    $output = $var ? 'TRUE' : 'FALSE';
  }
  elseif (is_int($var)) {
    $output = intval($var);
  }
  elseif (is_numeric($var)) {
    $floatval = floatval($var);
    if (is_string($var) && (string) $floatval !== $var) {

      // Do not convert a string to a number if the string
      // representation of that number is not identical to the
      // original value.
      $output = var_export($var, TRUE);
    }
    else {
      $output = $floatval;
    }
  }
  elseif (is_string($var) && strpos($var, "\n") !== FALSE) {

    // Replace line breaks in strings with a token for replacement
    // at the very end. This protects whitespace in strings from
    // unintentional indentation.
    $output = var_export(str_replace("\n", '***BREAK***', $var), TRUE);
  }
  else {
    $output = var_export($var, TRUE);
  }
  if ($prefix > 0) {
    $output = str_replace("\n", "\n" . str_repeat(' ', $prefix), $output);
  }
  if ($init) {
    $output = str_replace('***BREAK***', "\n", $output);
  }
  return $output;
}