You are here

class ReversibleConfigDiffer in Update helper 8

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

Overwrite of config updater differ.

Normalization is changed so that it can be 2-way normalization, not 1-way. Also format is adjusted to better supports converting from/to config array.

TODO:

  • (de)normalization should be solved properly. It does not support option with multiple assoc arrays in array. In Yaml empty line with '-' and then parameters after it.

@package Drupal\update_helper

Hierarchy

Expanded class hierarchy of ReversibleConfigDiffer

1 string reference to 'ReversibleConfigDiffer'
update_helper.services.yml in ./update_helper.services.yml
update_helper.services.yml
1 service uses ReversibleConfigDiffer
update_helper.config_differ in ./update_helper.services.yml
Drupal\update_helper\ReversibleConfigDiffer

File

src/ReversibleConfigDiffer.php, line 21

Namespace

Drupal\update_helper
View source
class ReversibleConfigDiffer extends ConfigDiffer {

  /**
   * Config diff transformer service.
   *
   * @var \Drupal\update_helper\ConfigDiffTransformer
   */
  protected $configDiffTransformer;

  /**
   * ConfigDiffer constructor.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   String translation service.
   * @param \Drupal\update_helper\ConfigDiffTransformer $config_diff_transformer
   *   Configuration transformer for diffing.
   * @param string[] $ignore
   *   Config components to ignore.
   * @param string $hierarchy_prefix
   *   Prefix to use in diffs for array hierarchy.
   * @param string $value_prefix
   *   Prefix to use in diffs for array value.
   */
  public function __construct(TranslationInterface $translation, ConfigDiffTransformer $config_diff_transformer, array $ignore = [
    'uuid',
    '_core',
  ], $hierarchy_prefix = '::', $value_prefix = ' : ') {
    parent::__construct($translation, $ignore, $hierarchy_prefix, $value_prefix);
    $this->configDiffTransformer = $config_diff_transformer;
  }

  /**
   * Strip some generic fields (uuid, _core).
   *
   * @param mixed $data
   *   Configuration array.
   *
   * @return mixed
   *   Returns stripped configuration.
   */
  public function stripIgnore($data) {
    foreach ($this->ignore as $element) {
      unset($data[$element]);
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  protected function normalize($config) {

    // Recursively normalize remaining elements, if they are arrays.
    foreach ($config as $key => $value) {
      if (is_array($value)) {
        $config[$key] = $this
          ->normalize($value);
      }
    }

    // Sort and return.
    ksort($config);
    return $config;
  }

  /**
   * {@inheritdoc}
   */
  protected function format(array $config, $prefix = '') {
    return $this->configDiffTransformer
      ->transform($config, $prefix);
  }

  /**
   * {@inheritdoc}
   */
  public function same($source, $target) {
    $source = $this
      ->stripIgnore($source);
    $target = $this
      ->stripIgnore($target);
    return parent::same($source, $target);
  }

  /**
   * {@inheritdoc}
   */
  public function diff($source, $target) {
    $source = $this
      ->stripIgnore($source);
    $target = $this
      ->stripIgnore($target);
    return parent::diff($source, $target);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigDiffer::$hierarchyPrefix protected property Prefix to use to indicate config hierarchy.
ConfigDiffer::$ignore protected property List of elements to ignore on top level when comparing config.
ConfigDiffer::$valuePrefix protected property Prefix to use to indicate config values.
ConfigDiffer::normalizeArray protected function Recursively sorts an array by key, and removes empty arrays.
ReversibleConfigDiffer::$configDiffTransformer protected property Config diff transformer service.
ReversibleConfigDiffer::diff public function Calculates differences between config. Overrides ConfigDiffer::diff
ReversibleConfigDiffer::format protected function Formats config for showing differences. Overrides ConfigDiffer::format
ReversibleConfigDiffer::normalize protected function Normalizes config for comparison. Overrides ConfigDiffer::normalize
ReversibleConfigDiffer::same public function Decides if two configuration arrays are considered to be the same. Overrides ConfigDiffer::same
ReversibleConfigDiffer::stripIgnore public function Strip some generic fields (uuid, _core).
ReversibleConfigDiffer::__construct public function ConfigDiffer constructor. Overrides ConfigDiffer::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.