You are here

class ConfigDiffTransformer in Update helper 8

Same name and namespace in other branches
  1. 2.x src/ConfigDiffTransformer.php \Drupal\update_helper\ConfigDiffTransformer

Config transformer for configuration diffing.

@package Drupal\update_helper

Hierarchy

Expanded class hierarchy of ConfigDiffTransformer

1 file declares its use of ConfigDiffTransformer
ConfigDiffTransformerTest.php in tests/src/Unit/ConfigDiffTransformerTest.php
1 string reference to 'ConfigDiffTransformer'
update_helper.services.yml in ./update_helper.services.yml
update_helper.services.yml
1 service uses ConfigDiffTransformer
update_helper.config_diff_transformer in ./update_helper.services.yml
Drupal\update_helper\ConfigDiffTransformer

File

src/ConfigDiffTransformer.php, line 10

Namespace

Drupal\update_helper
View source
class ConfigDiffTransformer {

  /**
   * Prefix to use to indicate config hierarchy.
   *
   * @var string
   *
   * @see ReversibleConfigDiffer::format().
   */
  protected $hierarchyPrefix = '::';

  /**
   * Prefix to use to indicate config values.
   *
   * @var string
   *
   * @see ReversibleConfigDiffer::format().
   */
  protected $valuePrefix = ' : ';

  /**
   * {@inheritdoc}
   */
  public function transform($config, $prefix = '') {
    $lines = [];
    $associative_config = array_keys($config) !== range(0, count($config) - 1);
    foreach ($config as $key => $value) {
      if (!$associative_config) {
        $key = '-';
      }
      $section_prefix = $prefix ? $prefix . $this->hierarchyPrefix . $key : $key;
      if (is_array($value) && !empty($value)) {
        $lines[] = $section_prefix;
        $new_lines = $this
          ->transform($value, $section_prefix);
        foreach ($new_lines as $line) {
          $lines[] = $line;
        }
      }
      else {
        $lines[] = $section_prefix . $this->valuePrefix . $this
          ->stringifyValue($value);
      }
    }
    return $lines;
  }

  /**
   * Reverse transformation of diff.
   *
   * @param array $config_string_lines
   *   String configuration lines.
   *
   * @return array
   *   Nested configuration array.
   */
  public function reverseTransform(array $config_string_lines) {
    $result = [];
    foreach ($config_string_lines as $row) {
      $key_value = explode(' : ', $row);
      $key_path = explode('::', $key_value[0]);
      $last_key = array_pop($key_path);
      $current_element =& $result;
      foreach ($key_path as $key) {
        if ($key === '-') {
          $key = count($current_element) - 1;
        }
        elseif (!isset($current_element[$key])) {
          $current_element[$key] = [];
        }
        $current_element =& $current_element[$key];
      }
      $value = [];
      if (count($key_value) === 2) {
        $value = $this
          ->unstringifyValue($key_value[1]);
      }
      if ($last_key === '-') {
        $current_element[] = $value;
      }
      else {
        $current_element[$last_key] = $value;
      }
    }
    return $result;
  }

  /**
   * Get string representation of value in format that it can be un-serialized.
   *
   * @param mixed $value
   *   Value that should be serialized.
   *
   * @return string
   *   Return string representation of value.
   */
  protected function stringifyValue($value) {
    return serialize($value);
  }

  /**
   * Get correct value from string representation of it.
   *
   * @param string $value
   *   String value.
   *
   * @return mixed
   *   Returns value.
   */
  protected function unstringifyValue($value) {
    return unserialize($value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigDiffTransformer::$hierarchyPrefix protected property Prefix to use to indicate config hierarchy.
ConfigDiffTransformer::$valuePrefix protected property Prefix to use to indicate config values.
ConfigDiffTransformer::reverseTransform public function Reverse transformation of diff.
ConfigDiffTransformer::stringifyValue protected function Get string representation of value in format that it can be un-serialized.
ConfigDiffTransformer::transform public function
ConfigDiffTransformer::unstringifyValue protected function Get correct value from string representation of it.