You are here

public function ConfigManager::diff in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Config/ConfigManager.php \Drupal\Core\Config\ConfigManager::diff()
  2. 10 core/lib/Drupal/Core/Config/ConfigManager.php \Drupal\Core\Config\ConfigManager::diff()

Creates a Diff object using the config data from the two storages.

@todo Make renderer injectable

Parameters

\Drupal\Core\Config\StorageInterface $source_storage: The storage to diff configuration from.

\Drupal\Core\Config\StorageInterface $target_storage: The storage to diff configuration to.

string $source_name: The name of the configuration object in the source storage to diff.

string $target_name: (optional) The name of the configuration object in the target storage. If omitted, the source name is used.

string $collection: (optional) The configuration collection name. Defaults to the default collection.

Return value

\Drupal\Component\Diff\Diff A Diff object using the config data from the two storages.

Overrides ConfigManagerInterface::diff

See also

\Drupal\Core\Diff\DiffFormatter

File

core/lib/Drupal/Core/Config/ConfigManager.php, line 176

Class

ConfigManager
The ConfigManager provides helper functions for the configuration system.

Namespace

Drupal\Core\Config

Code

public function diff(StorageInterface $source_storage, StorageInterface $target_storage, $source_name, $target_name = NULL, $collection = StorageInterface::DEFAULT_COLLECTION) {
  if ($collection != StorageInterface::DEFAULT_COLLECTION) {
    $source_storage = $source_storage
      ->createCollection($collection);
    $target_storage = $target_storage
      ->createCollection($collection);
  }
  if (!isset($target_name)) {
    $target_name = $source_name;
  }

  // The output should show configuration object differences formatted as YAML.
  // But the configuration is not necessarily stored in files. Therefore, they
  // need to be read and parsed, and lastly, dumped into YAML strings.
  $source_data = explode("\n", Yaml::encode($source_storage
    ->read($source_name)));
  $target_data = explode("\n", Yaml::encode($target_storage
    ->read($target_name)));

  // Check for new or removed files.
  if ($source_data === [
    'false',
  ]) {

    // Added file.
    // Cast the result of t() to a string, as the diff engine doesn't know
    // about objects.
    $source_data = [
      (string) $this
        ->t('File added'),
    ];
  }
  if ($target_data === [
    'false',
  ]) {

    // Deleted file.
    // Cast the result of t() to a string, as the diff engine doesn't know
    // about objects.
    $target_data = [
      (string) $this
        ->t('File removed'),
    ];
  }
  return new Diff($source_data, $target_data);
}