ConfigImportSubscriber.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Config\Config;
  4. use Drupal\Core\Config\ConfigImporter;
  5. use Drupal\Core\Config\ConfigImporterEvent;
  6. use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;
  7. use Drupal\Core\Config\ConfigNameException;
  8. use Drupal\Core\Extension\ThemeHandlerInterface;
  9. /**
  10. * Config import subscriber for config import events.
  11. */
  12. class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {
  13. /**
  14. * Theme data.
  15. *
  16. * @var \Drupal\Core\Extension\Extension[]
  17. */
  18. protected $themeData;
  19. /**
  20. * Module data.
  21. *
  22. * @var \Drupal\Core\Extension\Extension[]
  23. */
  24. protected $moduleData;
  25. /**
  26. * The theme handler.
  27. *
  28. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  29. */
  30. protected $themeHandler;
  31. /**
  32. * Constructs the ConfigImportSubscriber.
  33. *
  34. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  35. * The theme handler.
  36. */
  37. public function __construct(ThemeHandlerInterface $theme_handler) {
  38. $this->themeHandler = $theme_handler;
  39. }
  40. /**
  41. * Validates the configuration to be imported.
  42. *
  43. * @param \Drupal\Core\Config\ConfigImporterEvent $event
  44. * The Event to process.
  45. *
  46. * @throws \Drupal\Core\Config\ConfigNameException
  47. */
  48. public function onConfigImporterValidate(ConfigImporterEvent $event) {
  49. foreach (['delete', 'create', 'update'] as $op) {
  50. foreach ($event->getConfigImporter()->getUnprocessedConfiguration($op) as $name) {
  51. try {
  52. Config::validateName($name);
  53. }
  54. catch (ConfigNameException $e) {
  55. $message = $this->t('The config name @config_name is invalid.', ['@config_name' => $name]);
  56. $event->getConfigImporter()->logError($message);
  57. }
  58. }
  59. }
  60. $config_importer = $event->getConfigImporter();
  61. if ($config_importer->getStorageComparer()->getSourceStorage()->exists('core.extension')) {
  62. $this->validateModules($config_importer);
  63. $this->validateThemes($config_importer);
  64. $this->validateDependencies($config_importer);
  65. }
  66. else {
  67. $config_importer->logError($this->t('The core.extension configuration does not exist.'));
  68. }
  69. }
  70. /**
  71. * Validates module installations and uninstallations.
  72. *
  73. * @param \Drupal\Core\Config\ConfigImporter $config_importer
  74. * The configuration importer.
  75. */
  76. protected function validateModules(ConfigImporter $config_importer) {
  77. $core_extension = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension');
  78. // Get a list of modules with dependency weights as values.
  79. $module_data = $this->getModuleData();
  80. $nonexistent_modules = array_keys(array_diff_key($core_extension['module'], $module_data));
  81. foreach ($nonexistent_modules as $module) {
  82. $config_importer->logError($this->t('Unable to install the %module module since it does not exist.', ['%module' => $module]));
  83. }
  84. // Ensure that all modules being installed have their dependencies met.
  85. $installs = $config_importer->getExtensionChangelist('module', 'install');
  86. foreach ($installs as $module) {
  87. $missing_dependencies = [];
  88. foreach (array_keys($module_data[$module]->requires) as $required_module) {
  89. if (!isset($core_extension['module'][$required_module])) {
  90. $missing_dependencies[] = $module_data[$required_module]->info['name'];
  91. }
  92. }
  93. if (!empty($missing_dependencies)) {
  94. $module_name = $module_data[$module]->info['name'];
  95. $message = $this->formatPlural(count($missing_dependencies),
  96. 'Unable to install the %module module since it requires the %required_module module.',
  97. 'Unable to install the %module module since it requires the %required_module modules.',
  98. ['%module' => $module_name, '%required_module' => implode(', ', $missing_dependencies)]
  99. );
  100. $config_importer->logError($message);
  101. }
  102. }
  103. // Get the install profile from the site's configuration.
  104. $current_core_extension = $config_importer->getStorageComparer()->getTargetStorage()->read('core.extension');
  105. $install_profile = isset($current_core_extension['profile']) ? $current_core_extension['profile'] : NULL;
  106. // Ensure that all modules being uninstalled are not required by modules
  107. // that will be installed after the import.
  108. $uninstalls = $config_importer->getExtensionChangelist('module', 'uninstall');
  109. foreach ($uninstalls as $module) {
  110. foreach (array_keys($module_data[$module]->required_by) as $dependent_module) {
  111. if ($module_data[$dependent_module]->status && !in_array($dependent_module, $uninstalls, TRUE) && $dependent_module !== $install_profile) {
  112. $module_name = $module_data[$module]->info['name'];
  113. $dependent_module_name = $module_data[$dependent_module]->info['name'];
  114. $config_importer->logError($this->t('Unable to uninstall the %module module since the %dependent_module module is installed.', ['%module' => $module_name, '%dependent_module' => $dependent_module_name]));
  115. }
  116. }
  117. }
  118. // Ensure that the install profile is not being uninstalled.
  119. if (in_array($install_profile, $uninstalls, TRUE)) {
  120. $profile_name = $module_data[$install_profile]->info['name'];
  121. $config_importer->logError($this->t('Unable to uninstall the %profile profile since it is the install profile.', ['%profile' => $profile_name]));
  122. }
  123. // Ensure the profile is not changing.
  124. if ($install_profile !== $core_extension['profile']) {
  125. $config_importer->logError($this->t('Cannot change the install profile from %profile to %new_profile once Drupal is installed.', ['%profile' => $install_profile, '%new_profile' => $core_extension['profile']]));
  126. }
  127. }
  128. /**
  129. * Validates theme installations and uninstallations.
  130. *
  131. * @param \Drupal\Core\Config\ConfigImporter $config_importer
  132. * The configuration importer.
  133. */
  134. protected function validateThemes(ConfigImporter $config_importer) {
  135. $core_extension = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension');
  136. // Get all themes including those that are not installed.
  137. $theme_data = $this->getThemeData();
  138. $installs = $config_importer->getExtensionChangelist('theme', 'install');
  139. foreach ($installs as $key => $theme) {
  140. if (!isset($theme_data[$theme])) {
  141. $config_importer->logError($this->t('Unable to install the %theme theme since it does not exist.', ['%theme' => $theme]));
  142. // Remove non-existing installs from the list so we can validate theme
  143. // dependencies later.
  144. unset($installs[$key]);
  145. }
  146. }
  147. // Ensure that all themes being installed have their dependencies met.
  148. foreach ($installs as $theme) {
  149. foreach (array_keys($theme_data[$theme]->requires) as $required_theme) {
  150. if (!isset($core_extension['theme'][$required_theme])) {
  151. $theme_name = $theme_data[$theme]->info['name'];
  152. $required_theme_name = $theme_data[$required_theme]->info['name'];
  153. $config_importer->logError($this->t('Unable to install the %theme theme since it requires the %required_theme theme.', ['%theme' => $theme_name, '%required_theme' => $required_theme_name]));
  154. }
  155. }
  156. }
  157. // Ensure that all themes being uninstalled are not required by themes that
  158. // will be installed after the import.
  159. $uninstalls = $config_importer->getExtensionChangelist('theme', 'uninstall');
  160. foreach ($uninstalls as $theme) {
  161. foreach (array_keys($theme_data[$theme]->required_by) as $dependent_theme) {
  162. if ($theme_data[$dependent_theme]->status && !in_array($dependent_theme, $uninstalls, TRUE)) {
  163. $theme_name = $theme_data[$theme]->info['name'];
  164. $dependent_theme_name = $theme_data[$dependent_theme]->info['name'];
  165. $config_importer->logError($this->t('Unable to uninstall the %theme theme since the %dependent_theme theme is installed.', ['%theme' => $theme_name, '%dependent_theme' => $dependent_theme_name]));
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Validates configuration being imported does not have unmet dependencies.
  172. *
  173. * @param \Drupal\Core\Config\ConfigImporter $config_importer
  174. * The configuration importer.
  175. */
  176. protected function validateDependencies(ConfigImporter $config_importer) {
  177. $core_extension = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension');
  178. $existing_dependencies = [
  179. 'config' => $config_importer->getStorageComparer()->getSourceStorage()->listAll(),
  180. 'module' => array_keys($core_extension['module']),
  181. 'theme' => array_keys($core_extension['theme']),
  182. ];
  183. $theme_data = $this->getThemeData();
  184. $module_data = $this->getModuleData();
  185. // Validate the dependencies of all the configuration. We have to validate
  186. // the entire tree because existing configuration might depend on
  187. // configuration that is being deleted.
  188. foreach ($config_importer->getStorageComparer()->getSourceStorage()->listAll() as $name) {
  189. // Ensure that the config owner is installed. This checks all
  190. // configuration including configuration entities.
  191. list($owner,) = explode('.', $name, 2);
  192. if ($owner !== 'core') {
  193. $message = FALSE;
  194. if (!isset($core_extension['module'][$owner]) && isset($module_data[$owner])) {
  195. $message = $this->t('Configuration %name depends on the %owner module that will not be installed after import.', [
  196. '%name' => $name,
  197. '%owner' => $module_data[$owner]->info['name']
  198. ]);
  199. }
  200. elseif (!isset($core_extension['theme'][$owner]) && isset($theme_data[$owner])) {
  201. $message = $this->t('Configuration %name depends on the %owner theme that will not be installed after import.', [
  202. '%name' => $name,
  203. '%owner' => $theme_data[$owner]->info['name']
  204. ]);
  205. }
  206. elseif (!isset($core_extension['module'][$owner]) && !isset($core_extension['theme'][$owner])) {
  207. $message = $this->t('Configuration %name depends on the %owner extension that will not be installed after import.', [
  208. '%name' => $name,
  209. '%owner' => $owner
  210. ]);
  211. }
  212. if ($message) {
  213. $config_importer->logError($message);
  214. continue;
  215. }
  216. }
  217. $data = $config_importer->getStorageComparer()->getSourceStorage()->read($name);
  218. // Configuration entities have dependencies on modules, themes, and other
  219. // configuration entities that we can validate. Their content dependencies
  220. // are not validated since we assume that they are soft dependencies.
  221. // Configuration entities can be identified by having 'dependencies' and
  222. // 'uuid' keys.
  223. if (isset($data['dependencies']) && isset($data['uuid'])) {
  224. $dependencies_to_check = array_intersect_key($data['dependencies'], array_flip(['module', 'theme', 'config']));
  225. foreach ($dependencies_to_check as $type => $dependencies) {
  226. $diffs = array_diff($dependencies, $existing_dependencies[$type]);
  227. if (!empty($diffs)) {
  228. $message = FALSE;
  229. switch ($type) {
  230. case 'module':
  231. $message = $this->formatPlural(
  232. count($diffs),
  233. 'Configuration %name depends on the %module module that will not be installed after import.',
  234. 'Configuration %name depends on modules (%module) that will not be installed after import.',
  235. ['%name' => $name, '%module' => implode(', ', $this->getNames($diffs, $module_data))]
  236. );
  237. break;
  238. case 'theme':
  239. $message = $this->formatPlural(
  240. count($diffs),
  241. 'Configuration %name depends on the %theme theme that will not be installed after import.',
  242. 'Configuration %name depends on themes (%theme) that will not be installed after import.',
  243. ['%name' => $name, '%theme' => implode(', ', $this->getNames($diffs, $theme_data))]
  244. );
  245. break;
  246. case 'config':
  247. $message = $this->formatPlural(
  248. count($diffs),
  249. 'Configuration %name depends on the %config configuration that will not exist after import.',
  250. 'Configuration %name depends on configuration (%config) that will not exist after import.',
  251. ['%name' => $name, '%config' => implode(', ', $diffs)]
  252. );
  253. break;
  254. }
  255. if ($message) {
  256. $config_importer->logError($message);
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. /**
  264. * Gets theme data.
  265. *
  266. * @return \Drupal\Core\Extension\Extension[]
  267. */
  268. protected function getThemeData() {
  269. if (!isset($this->themeData)) {
  270. $this->themeData = $this->themeHandler->rebuildThemeData();
  271. }
  272. return $this->themeData;
  273. }
  274. /**
  275. * Gets module data.
  276. *
  277. * @return \Drupal\Core\Extension\Extension[]
  278. */
  279. protected function getModuleData() {
  280. if (!isset($this->moduleData)) {
  281. $this->moduleData = system_rebuild_module_data();
  282. }
  283. return $this->moduleData;
  284. }
  285. /**
  286. * Gets human readable extension names.
  287. *
  288. * @param array $names
  289. * A list of extension machine names.
  290. * @param \Drupal\Core\Extension\Extension[] $extension_data
  291. * Extension data.
  292. *
  293. * @return array
  294. * A list of human-readable extension names, or machine names if
  295. * human-readable names are not available.
  296. */
  297. protected function getNames(array $names, array $extension_data) {
  298. return array_map(function ($name) use ($extension_data) {
  299. if (isset($extension_data[$name])) {
  300. $name = $extension_data[$name]->info['name'];
  301. }
  302. return $name;
  303. }, $names);
  304. }
  305. }