/web/core/modules/locale/src/LocaleDefaultConfigStorage.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 161 lines · 68 code · 14 blank · 79 comment · 6 complexity · f62aee33bf077814e93f6ab9b4dd3d14 MD5 · raw file

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