You are here

protected function ConfigDiffer::format in Configuration Update Manager 8

Formats config for showing differences.

To compute differences, we need to separate the config into lines and use line-by-line differencer. The obvious way to split into lines is:

explode("\n", Yaml::encode($config));

But this would highlight meaningless differences due to the often different order of config files, and also loses the indentation and context of the config hierarchy when differences are computed, making the difference difficult to interpret.

So, what we do instead is to take the YAML hierarchy and format it so that the hierarchy is shown on each line. So, if you're in element $config['foo']['bar'] and the value is 'value', you will see 'foo::bar : value'.

Parameters

array $config: Config array to format. Normalize it first if you want to do diffs.

string $prefix: (optional) When called recursively, the prefix to put on each line. Omit when initially calling this function.

Return value

string[] Array of config lines formatted so that a line-by-line diff will show the context in each line, and meaningful differences will be computed.

See also

ConfigDiffer::normalize()

ConfigDiffer::$hierarchyPrefix

ConfigDiffer::$valuePrefix

1 call to ConfigDiffer::format()
ConfigDiffer::diff in src/ConfigDiffer.php
Calculates differences between config.

File

src/ConfigDiffer.php, line 161

Class

ConfigDiffer
Provides methods related to config differences.

Namespace

Drupal\config_update

Code

protected function format(array $config, $prefix = '') {
  $lines = [];
  foreach ($config as $key => $value) {
    $section_prefix = $prefix ? $prefix . $this->hierarchyPrefix . $key : $key;
    if (is_array($value)) {
      $lines[] = $section_prefix;
      $newlines = $this
        ->format($value, $section_prefix);
      foreach ($newlines as $line) {
        $lines[] = $line;
      }
    }
    else {
      $lines[] = $section_prefix . $this->valuePrefix . Yaml::encode($value);
    }
  }
  return $lines;
}