annotate core/modules/locale/src/LocaleDefaultConfigStorage.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\locale;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ExtensionInstallStorage;
Chris@0 6 use Drupal\Core\Config\StorageInterface;
Chris@0 7 use Drupal\language\ConfigurableLanguageManagerInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Provides access to default configuration for locale integration.
Chris@0 11 *
Chris@0 12 * Allows unified access to default configuration from one of three sources:
Chris@0 13 * - Required default configuration (config/install/*)
Chris@0 14 * - Optional default configuration (config/optional/*)
Chris@0 15 * - Predefined languages mocked as default configuration (list defined in
Chris@0 16 * LocaleConfigManagerInterface::getStandardLanguageList())
Chris@0 17 *
Chris@0 18 * These sources are considered equal in terms of how locale module interacts
Chris@0 19 * with them for translation. Their translatable source strings are exposed
Chris@0 20 * for interface translation and participate in remote translation updates.
Chris@0 21 */
Chris@0 22 class LocaleDefaultConfigStorage {
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The storage instance for reading configuration data.
Chris@0 26 *
Chris@0 27 * @var \Drupal\Core\Config\StorageInterface
Chris@0 28 */
Chris@0 29 protected $configStorage;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The language manager.
Chris@0 33 *
Chris@0 34 * @var \Drupal\language\ConfigurableLanguageManagerInterface
Chris@0 35 */
Chris@0 36 protected $languageManager;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * The storage instance for reading required default configuration data.
Chris@0 40 *
Chris@0 41 * @var \Drupal\Core\Config\StorageInterface
Chris@0 42 */
Chris@0 43 protected $requiredInstallStorage;
Chris@0 44
Chris@0 45 /**
Chris@0 46 * The storage instance for reading optional default configuration data.
Chris@0 47 *
Chris@0 48 * @var \Drupal\Core\Config\StorageInterface
Chris@0 49 */
Chris@0 50 protected $optionalInstallStorage;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Constructs a LocaleDefaultConfigStorage.
Chris@0 54 *
Chris@0 55 * @param \Drupal\Core\Config\StorageInterface $config_storage
Chris@0 56 * The storage object to use for reading configuration data.
Chris@0 57 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
Chris@0 58 * The language manager.
Chris@0 59 */
Chris@0 60 public function __construct(StorageInterface $config_storage, ConfigurableLanguageManagerInterface $language_manager, $install_profile) {
Chris@0 61 $this->configStorage = $config_storage;
Chris@0 62 $this->languageManager = $language_manager;
Chris@0 63
Chris@0 64 $this->requiredInstallStorage = new ExtensionInstallStorage($this->configStorage, ExtensionInstallStorage::CONFIG_INSTALL_DIRECTORY, ExtensionInstallStorage::DEFAULT_COLLECTION, TRUE, $install_profile);
Chris@0 65 $this->optionalInstallStorage = new ExtensionInstallStorage($this->configStorage, ExtensionInstallStorage::CONFIG_OPTIONAL_DIRECTORY, ExtensionInstallStorage::DEFAULT_COLLECTION, TRUE, $install_profile);
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Read a configuration from install storage or default languages.
Chris@0 70 *
Chris@0 71 * @param string $name
Chris@0 72 * Configuration object name.
Chris@0 73 *
Chris@0 74 * @return array
Chris@0 75 * Configuration data from install storage or default language.
Chris@0 76 */
Chris@0 77 public function read($name) {
Chris@0 78 if ($this->requiredInstallStorage->exists($name)) {
Chris@0 79 return $this->requiredInstallStorage->read($name);
Chris@0 80 }
Chris@0 81 elseif ($this->optionalInstallStorage->exists($name)) {
Chris@0 82 return $this->optionalInstallStorage->read($name);
Chris@0 83 }
Chris@0 84 elseif (strpos($name, 'language.entity.') === 0) {
Chris@0 85 // Simulate default languages as if they were shipped as default
Chris@0 86 // configuration.
Chris@0 87 $langcode = str_replace('language.entity.', '', $name);
Chris@0 88 $predefined_languages = $this->languageManager->getStandardLanguageList();
Chris@0 89 if (isset($predefined_languages[$langcode])) {
Chris@0 90 $data = $this->configStorage->read($name);
Chris@0 91 $data['label'] = $predefined_languages[$langcode][0];
Chris@0 92 return $data;
Chris@0 93 }
Chris@0 94 }
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Return the list of configuration in install storage and current languages.
Chris@0 99 *
Chris@0 100 * @return array
Chris@0 101 * List of configuration in install storage and current languages.
Chris@0 102 */
Chris@0 103 public function listAll() {
Chris@0 104 $languages = $this->predefinedConfiguredLanguages();
Chris@0 105 return array_unique(
Chris@0 106 array_merge(
Chris@0 107 $this->requiredInstallStorage->listAll(),
Chris@0 108 $this->optionalInstallStorage->listAll(),
Chris@0 109 $languages
Chris@0 110 )
Chris@0 111 );
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Get all configuration names and folders for a list of modules or themes.
Chris@0 116 *
Chris@0 117 * @param string $type
Chris@0 118 * Type of components: 'module' | 'theme' | 'profile'
Chris@0 119 * @param array $list
Chris@0 120 * Array of theme or module names.
Chris@0 121 *
Chris@0 122 * @return array
Chris@0 123 * Configuration names provided by that component. In case of language
Chris@0 124 * module this list is extended with configured languages that have
Chris@0 125 * predefined names as well.
Chris@0 126 */
Chris@0 127 public function getComponentNames($type, array $list) {
Chris@0 128 $names = array_unique(
Chris@0 129 array_merge(
Chris@0 130 array_keys($this->requiredInstallStorage->getComponentNames($type, $list)),
Chris@0 131 array_keys($this->optionalInstallStorage->getComponentNames($type, $list))
Chris@0 132 )
Chris@0 133 );
Chris@0 134 if ($type == 'module' && in_array('language', $list)) {
Chris@0 135 $languages = $this->predefinedConfiguredLanguages();
Chris@0 136 $names = array_unique(array_merge($names, $languages));
Chris@0 137 }
Chris@0 138 return $names;
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Compute the list of configuration names that match predefined languages.
Chris@0 143 *
Chris@0 144 * @return array
Chris@0 145 * The list of configuration names that match predefined languages.
Chris@0 146 */
Chris@0 147 protected function predefinedConfiguredLanguages() {
Chris@0 148 $names = $this->configStorage->listAll('language.entity.');
Chris@0 149 $predefined_languages = $this->languageManager->getStandardLanguageList();
Chris@0 150 foreach ($names as $id => $name) {
Chris@0 151 $langcode = str_replace('language.entity.', '', $name);
Chris@0 152 if (!isset($predefined_languages[$langcode])) {
Chris@0 153 unset($names[$id]);
Chris@0 154 }
Chris@0 155 }
Chris@0 156 return array_values($names);
Chris@0 157 }
Chris@0 158
Chris@0 159 }