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

3
use Drupal\Core\Config\Config;
4
use Drupal\Core\Config\ConfigException;
5
use Drupal\Core\Config\ConfigInstaller;
6
use Drupal\Core\Config\FileStorage;
7
use Drupal\Core\Config\StorageInterface;
8
use Drupal\Core\Config\StorageComparer;
9
use Symfony\Component\Yaml\Dumper;
gdd's avatar
gdd committed
10

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

16
/**
17
 * Installs the default configuration of a given extension.
18
 *
19 20 21 22
 * @param string $type
 *   The extension type; e.g., 'module' or 'theme'.
 * @param string $name
 *   The name of the module or theme to install default configuration for.
23
 */
24 25 26
function config_install_default_config($type, $name) {
  $config_dir = drupal_get_path($type, $name) . '/config';
  if (is_dir($config_dir)) {
27
    $source_storage = new FileStorage($config_dir);
28 29 30 31 32 33 34 35 36 37 38 39
    $storage_comparer = new StorageComparer($source_storage, Drupal::service('config.storage'));
    // Only import new config. Changed config is from previous enables and
    // should not be overwritten.
    $storage_comparer->addChangelistCreate();
    $installer = new ConfigInstaller(
      $storage_comparer,
      Drupal::service('event_dispatcher'),
      Drupal::service('config.factory'),
      Drupal::entityManager(),
      Drupal::lock()
    );
    $installer->import();
40
  }
41 42
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/**
 * Uninstalls the default configuration of a given extension.
 *
 * @param string $type
 *   The extension type; e.g., 'module' or 'theme'.
 * @param string $name
 *   The name of the module or theme to install default configuration for.
 */
function config_uninstall_default_config($type, $name) {
  $storage = drupal_container()->get('config.storage');
  $config_names = $storage->listAll($name . '.');
  foreach ($config_names as $config_name) {
    config($config_name)->delete();
  }
}

59
/**
60 61 62
 * Gets configuration object names starting with a given prefix.
 *
 * @see Drupal\Core\Config\StorageInterface::listAll()
63
 */
64
function config_get_storage_names_with_prefix($prefix = '') {
65
  return drupal_container()->get('config.storage')->listAll($prefix);
66 67
}

Katherine Senzee's avatar
Katherine Senzee committed
68
/**
69
 * Retrieves a configuration object.
Katherine Senzee's avatar
Katherine Senzee committed
70 71
 *
 * This is the main entry point to the configuration API. Calling
72 73
 * @code config('book.admin') @endcode will return a configuration object in
 * which the book module can store its administrative settings.
Katherine Senzee's avatar
Katherine Senzee committed
74
 *
75 76 77
 * @deprecated This function has been replaced by the \Drupal::config() method.
 * Use that instead.
 *
78
 * @param string $name
Katherine Senzee's avatar
Katherine Senzee committed
79
 *   The name of the configuration object to retrieve. The name corresponds to
80
 *   a configuration file. For @code config('book.admin') @endcode, the config
81
 *   object returned will contain the contents of book.admin configuration file.
82
 *
83 84
 * @return Drupal\Core\Config\Config
 *   A configuration object.
Katherine Senzee's avatar
Katherine Senzee committed
85
 */
86
function config($name) {
87
  return drupal_container()->get('config.factory')->get($name);
88
}
89

90
/**
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 * Sets the config context on the config factory.
 *
 * This allows configuration objects to be created using special configuration
 * contexts eg. global override free or locale using a user preferred language.
 * Calling this function affects all subsequent calls to config() until
 * config_context_leave() is called.
 *
 * @see config_context_leave()
 * @see \Drupal\Core\Config\ConfigFactory
 *
 * @param string $context_name
 *   The name of the config context service on the container or a fully
 *   qualified class implementing \Drupal\Core\Config\Context\ContextInterface.
 *
 * @return \Drupal\Core\Config\Context\ContextInterface
 *   The configuration context object.
 */
function config_context_enter($context_name) {
  if (drupal_container()->has($context_name)) {
    $context = drupal_container()->get($context_name);
  }
112
  elseif (class_exists($context_name) && in_array('Drupal\Core\Config\Context\ContextInterface', class_implements($context_name))) {
113 114 115 116 117 118 119 120 121 122 123 124 125
    $context = drupal_container()
      ->get('config.context.factory')
      ->get($context_name);
  }
  else {
    throw new ConfigException(sprintf('Unknown config context service or class: %s', $context_name));
  }
  drupal_container()
    ->get('config.factory')
    ->enterContext($context);
  return $context;
}

126
/**
127 128 129 130 131 132 133 134 135 136 137
 * Leaves the current config context returning to the previous context.
 *
 * @see config_context_enter()
 * @see \Drupal\Core\Config\ConfigFactory
 */
function config_context_leave() {
  drupal_container()
    ->get('config.factory')
    ->leaveContext();
}

138
/**
139 140 141 142 143 144 145 146
 * Return a list of all config entity types provided by a module.
 *
 * @param string $module
 *   The name of the module possibly providing config entities.
 *
 * @return array
 *   An associative array containing the entity info for any config entities
 *   provided by the requested module, keyed by the entity type.
147
 */
148 149 150 151 152 153 154 155
function config_get_module_config_entities($module) {
  // While this is a lot of work to generate, it's not worth static caching
  // since this function is only called at install/uninstall, and only
  // once per module.
  $info = entity_get_info();
  return array_filter($info, function($entity_info) use ($module) {
    return ($entity_info['module'] == $module) && is_subclass_of($entity_info['class'], 'Drupal\Core\Config\Entity\ConfigEntityInterface');
  });
156
}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

/**
 * Returns the entity type of a configuration object.
 *
 * @param string $name
 *   The configuration object name.
 *
 * @return string|null
 *   Either the entity type name, or NULL if none match.
 */
function config_get_entity_type_by_name($name) {
  $entities = array_filter(entity_get_info(), function($entity_info) use ($name) {
    return (isset($entity_info['config_prefix']) && strpos($name, $entity_info['config_prefix'] . '.') === 0);
  });
  return key($entities);
}
173

174
/**
175 176 177 178 179 180 181 182 183 184 185
 * Returns the typed config manager service.
 *
 * Use the typed data manager service for creating typed configuration objects.
 *
 * @see Drupal\Core\TypedData\TypedDataManager::create()
 *
 * @return Drupal\Core\TypedData\TypedConfigManager
 */
function config_typed() {
  return drupal_container()->get('config.typed');
}
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/**
 * Creates a configuration snapshot following a successful import.
 *
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The storage to synchronize configuration from.
 * @param Drupal\Core\Config\StorageInterface $snapshot_storage
 *   The storage to synchronize configuration to.
 */
function config_import_create_snapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) {
  $snapshot_storage->deleteAll();
  foreach ($source_storage->listAll() as $name) {
    $snapshot_storage->write($name, $source_storage->read($name));
  }
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/**
 * Return a formatted diff of a named config between two storages.
 *
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The storage to diff configuration from.
 * @param Drupal\Core\Config\StorageInterface $target_storage
 *   The storage to diff configuration to.
 * @param string $name
 *   The name of the configuration object to diff.
 *
 * @return core/lib/Drupal/Component/Diff
 *   A formatted string showing the difference between the two storages.
 *
 * @todo Make renderer injectable
 */
function config_diff(StorageInterface $source_storage, StorageInterface $target_storage, $name) {
  require_once DRUPAL_ROOT . '/core/lib/Drupal/Component/Diff/DiffEngine.php';

  // 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.
  $dumper = new Dumper();
  $dumper->setIndentation(2);

  $source_data = explode("\n", $dumper->dump($source_storage->read($name), PHP_INT_MAX));
  $target_data = explode("\n", $dumper->dump($target_storage->read($name), PHP_INT_MAX));

  // Check for new or removed files.
  if ($source_data === array('false')) {
    // Added file.
    $source_data = array(t('File added'));
  }
  if ($target_data === array('false')) {
    // Deleted file.
    $target_data = array(t('File removed'));
  }

  return new Diff($source_data, $target_data);
}