You are here

public function StoragePhp::export in Configuration Management 7.2

Adapted from CTools ctools_var_export().

This is a replacement for var_export(), allowing us to more nicely format exports. It will recurse down into arrays and will try to properly export bools when it can, though PHP has a hard time with this since they often end up as strings or ints.

Overrides Storage::export

3 calls to StoragePhp::export()
StorageCtools::getDataToSave in lib/Drupal/configuration/Storage/StorageCtools.php
Saves the configuration object into the DataStore.
StorageEntityApi::getDataToSave in lib/Drupal/configuration/Storage/StorageEntityApi.php
Saves the configuration object into the DataStore.
StoragePhp::getDataToSave in lib/Drupal/configuration/Storage/StoragePhp.php
Saves the configuration object into the DataStore.

File

lib/Drupal/configuration/Storage/StoragePhp.php, line 25
Definition of Drupal\configuration\Storage\StoragePhp.

Class

StoragePhp

Namespace

Drupal\configuration\Storage

Code

public function export($var, $prefix = '') {
  if (is_array($var)) {
    if (empty($var)) {
      $output = 'array()';
    }
    else {
      ksort($var);
      $output = "array(\n";
      foreach ($var as $key => $value) {
        $output .= $prefix . "  " . $this
          ->export($key) . " => " . $this
          ->export($value, $prefix . '  ') . ",\n";
      }
      $output .= $prefix . ')';
    }
  }
  elseif (is_object($var) && get_class($var) === 'stdClass') {

    // var_export() will export stdClass objects using an undefined
    // magic method __set_state() leaving the export broken. This
    // workaround avoids this by casting the object as an array for
    // export and casting it back to an object when evaluated.
    $output = '(object) ' . $this
      ->export((array) $var, $prefix);
  }
  elseif (is_bool($var)) {
    $output = $var ? 'TRUE' : 'FALSE';
  }
  else {
    $output = var_export($var, TRUE);
  }
  return $output;
}