config.inc 8.27 KB
Newer Older
chx's avatar
chx committed
1 2
<?php

3
use Drupal\Core\Config\Config;
4
use Drupal\Core\Config\DatabaseStorage;
5
use Drupal\Core\Config\FileStorage;
6 7
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageInterface;
gdd's avatar
gdd committed
8

gdd's avatar
gdd committed
9 10 11 12 13
/**
 * @file
 * This is the API for configuration storage.
 */

14
/**
15
 * Installs the default configuration of a given module.
16 17 18
 *
 * @param
 *   The name of the module we are installing.
gdd's avatar
gdd committed
19 20 21
 *
 * @todo Make this acknowledge other storage engines rather than having
 *   SQL be hardcoded.
22 23 24
 */
function config_install_default_config($module) {
  $module_config_dir = drupal_get_path('module', $module) . '/config';
25
  if (is_dir($module_config_dir)) {
26 27 28
    $source_storage = new FileStorage(array('directory' => $module_config_dir));
    $target_storage = new DatabaseStorage();
    $null_storage = new NullStorage();
29

30 31 32 33 34 35 36 37
    // Upon module installation, only new config objects need to be created.
    // config_sync_get_changes() would potentially perform a diff of hundreds or
    // even thousands of config objects that happen to be contained in the
    // active store. We leverage the NullStorage to avoid that needless
    // computation of differences.
    $config_changes = config_sync_get_changes($source_storage, $null_storage);
    if (empty($config_changes)) {
      return;
38
    }
39 40
    $remaining_changes = config_import_invoke_owner($config_changes, $source_storage, $target_storage);
    config_sync_changes($remaining_changes, $source_storage, $target_storage);
41
  }
42 43
}

44
/**
45
 * @todo Modules need a way to access the active store, whatever it is.
46
 */
47
function config_get_storage_names_with_prefix($prefix = '') {
48 49
  $storage = new DatabaseStorage();
  return $storage->listAll($prefix);
50 51
}

Katherine Senzee's avatar
Katherine Senzee committed
52
/**
53
 * Retrieves a configuration object.
Katherine Senzee's avatar
Katherine Senzee committed
54 55 56 57 58 59 60
 *
 * This is the main entry point to the configuration API. Calling
 * @code config(book.admin) @endcode will return a configuration object in which
 * the book module can store its administrative settings.
 *
 * @param $name
 *   The name of the configuration object to retrieve. The name corresponds to
61 62
 *   a configuration file. For @code config(book.admin) @endcode, the config
 *   object returned will contain the contents of book.admin configuration file.
63
 *
64 65
 * @return Drupal\Core\Config\Config
 *   A configuration object.
Katherine Senzee's avatar
Katherine Senzee committed
66
 */
67 68
function config($name) {
  return drupal_container()->get('config.factory')->get($name)->load();
69
}
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/**
 * Returns a list of differences between configuration storages.
 *
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The storage to synchronize configuration from.
 * @param Drupal\Core\Config\StorageInterface $target_storage
 *   The storage to synchronize configuration to.
 *
 * @return array|bool
 *   An assocative array containing the differences between source and target
 *   storage, or FALSE if there are no differences.
 */
function config_sync_get_changes(StorageInterface $source_storage, StorageInterface $target_storage) {
  $source_names = $source_storage->listAll();
  $target_names = $target_storage->listAll();
  $config_changes = array(
    'create' => array_diff($source_names, $target_names),
    'change' => array(),
    'delete' => array_diff($target_names, $source_names),
  );
  foreach (array_intersect($source_names, $target_names) as $name) {
    $source_config_data = $source_storage->read($name);
    $target_config_data = $target_storage->read($name);
94
    if ($source_config_data !== $target_config_data) {
95 96 97 98 99
      $config_changes['change'][] = $name;
    }
  }

  // Do not trigger subsequent synchronization operations if there are no
100
  // changes in any category.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  if (empty($config_changes['create']) && empty($config_changes['change']) && empty($config_changes['delete'])) {
    return FALSE;
  }
  return $config_changes;
}

/**
 * Writes an array of config file changes from a source storage to a target storage.
 *
 * @param array $config_changes
 *   An array of changes to be written.
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The storage to synchronize configuration from.
 * @param Drupal\Core\Config\StorageInterface $target_storage
 *   The storage to synchronize configuration to.
 */
function config_sync_changes(array $config_changes, StorageInterface $source_storage, StorageInterface $target_storage) {
  foreach (array('delete', 'create', 'change') as $op) {
    foreach ($config_changes[$op] as $name) {
      if ($op == 'delete') {
        $target_storage->delete($name);
      }
      else {
        $data = $source_storage->read($name);
        $target_storage->write($name, $data);
      }
    }
  }
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/**
 * Imports configuration from FileStorage to DatabaseStorage.
 *
 * @return bool|null
 *   TRUE if configuration was imported successfully, FALSE in case of a
 *   synchronization error, or NULL if there are no changes to synchronize.
 */
function config_import() {
  // Retrieve a list of differences between FileStorage and DatabaseStorage.
  // @todo Leverage DI + config.storage.info.
  $source_storage = new FileStorage();
  $target_storage = new DatabaseStorage();

  $config_changes = config_sync_get_changes($source_storage, $target_storage);
  if (empty($config_changes)) {
    return;
  }

  if (!lock_acquire(__FUNCTION__)) {
    // Another request is synchronizing configuration.
    // Return a negative result for UI purposes. We do not differentiate between
    // an actual synchronization error and a failed lock, because concurrent
    // synchronizations are an edge-case happening only when multiple developers
    // or site builders attempt to do it without coordinating.
    return FALSE;
  }

  $success = TRUE;
  try {
    $remaining_changes = config_import_invoke_owner($config_changes, $source_storage, $target_storage);
    config_sync_changes($remaining_changes, $source_storage, $target_storage);
    // Flush all caches and reset static variables after a successful import.
    drupal_flush_all_caches();
  }
  catch (ConfigException $e) {
    watchdog_exception('config_import', $e);
    $success = FALSE;
  }
  lock_release(__FUNCTION__);
  return $success;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/**
 * Invokes MODULE_config_import() callbacks for configuration changes.
 *
 * @param array $config_changes
 *   An array of changes to be loaded.
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The storage to synchronize configuration from.
 * @param Drupal\Core\Config\StorageInterface $target_storage
 *   The storage to synchronize configuration to.
 *
 * @todo Add support for other extension types; e.g., themes etc.
 */
function config_import_invoke_owner(array $config_changes, StorageInterface $source_storage, StorageInterface $target_storage) {
  $storage_dispatcher = drupal_container()->get('config.storage.dispatcher');

  // Allow modules to take over configuration change operations for
  // higher-level configuration data.
  // First pass deleted, then new, and lastly changed configuration, in order to
  // handle dependencies correctly.
  foreach (array('delete', 'create', 'change') as $op) {
    foreach ($config_changes[$op] as $key => $name) {
      // Extract owner from configuration object name.
      $module = strtok($name, '.');
      // Check whether the module implements hook_config_import() and ask it to
      // handle the configuration change.
      $handled_by_module = FALSE;
      if (module_hook($module, 'config_import_' . $op)) {
        $old_config = new Config($storage_dispatcher);
        $old_config
          ->setName($name)
          ->load();

        $data = $source_storage->read($name);
        $new_config = new Config($storage_dispatcher);
        $new_config->setName($name);
        if ($data !== FALSE) {
          $new_config->setData($data);
        }

        $handled_by_module = module_invoke($module, 'config_import_' . $op, $name, $new_config, $old_config);
      }
      if (!empty($handled_by_module)) {
        unset($config_changes[$op][$key]);
      }
    }
  }
  return $config_changes;
}
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

/**
 * Exports configuration from DatabaseStorage to FileStorage.
 */
function config_export() {
  // Retrieve a list of differences between DatabaseStorage and FileStorage.
  // @todo Leverage DI + config.storage.info.
  $source_storage = new DatabaseStorage();
  $target_storage = new FileStorage();

  $config_changes = config_sync_get_changes($source_storage, $target_storage);
  if (empty($config_changes)) {
    return;
  }
  config_sync_changes($config_changes, $source_storage, $target_storage);
  return TRUE;
}