You are here

function features_var_export in Features 7.2

Same name and namespace in other branches
  1. 6 features.export.inc \features_var_export()
  2. 7 features.export.inc \features_var_export()

Alternative to var_export(), with a more pleasant output format.

The function is recursive, some parameters should only be provided on recursive calls.

The function is inspired/adapted from views_var_export().

Parameters

mixed $var: The value to export.

string $prefix: (recursive) Prefix for indentation.

bool $init: (recursive) TRUE, if this is the first level of recursion.

int $count: (recursive) The recursion depth. Starts with 0.

Return value

string A php statement whose return value is $var.

See also

\views_var_export()

\var_export()

16 calls to features_var_export()
contact_categories_features_export_render in includes/features.contact.inc
Implements hook_features_export_render().
features_detect_overrides in ./features.export.inc
Detect differences between DB and code components of a feature.
features_get_signature in ./features.export.inc
Gets an md5 signature for a the state of an object in code or database.
features_translatables_export in ./features.export.inc
Creates PHP code with t() statements for given translatable strings.
field_base_features_export_render in includes/features.field.inc
Implements hook_features_export_render().

... See full list

File

./features.export.inc, line 759
Contains functions that export configuration into feature modules.

Code

function features_var_export($var, $prefix = '', $init = TRUE, $count = 0) {
  if ($count > 50) {
    watchdog('features', 'Recursion depth reached in features_var_export', array());
    return '';
  }
  if (is_object($var)) {
    $output = method_exists($var, 'export') ? $var
      ->export() : features_var_export((array) $var, '', FALSE, $count + 1);
  }
  elseif (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) . " => " . features_var_export($value, '  ', FALSE, $count + 1) . ",\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.
    $var = str_replace("\n", "***BREAK***", $var);
    $output = var_export($var, TRUE);
  }
  else {
    $output = var_export($var, TRUE);
  }
  if ($prefix) {
    $output = str_replace("\n", "\n{$prefix}", $output);
  }
  if ($init) {
    $output = str_replace("***BREAK***", "\n", $output);
  }
  return $output;
}