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