config_update_ui.drush.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for the Configuration Update Reports module.
  5. */
  6. use Drupal\Component\Diff\DiffFormatter;
  7. /**
  8. * Implements hook_drush_command().
  9. */
  10. function config_update_ui_drush_command() {
  11. $items = [];
  12. $items['config-list-types'] = [
  13. 'description' => 'List config types',
  14. 'aliases' => ['clt'],
  15. 'core' => ['8+'],
  16. 'outputformat' => [
  17. 'default' => 'list',
  18. ],
  19. ];
  20. $items['config-added-report'] = [
  21. 'description' => 'Display a list of config items that did not come from your installed modules, themes, or install profile',
  22. 'arguments' => [
  23. 'name' => 'The type of config to report on. See config-list-types to list them. You can also use system.all for all types, or system.simple for simple config.',
  24. ],
  25. 'required-arguments' => 1,
  26. 'examples' => [
  27. 'drush config-added-report action' => 'Displays the added config report for action config.',
  28. ],
  29. 'aliases' => ['cra'],
  30. 'core' => ['8+'],
  31. 'outputformat' => [
  32. 'default' => 'list',
  33. ],
  34. ];
  35. $items['config-missing-report'] = [
  36. 'description' => 'Display a list of config items from your installed modules, themes, or install profile that are not currently in your active config',
  37. 'arguments' => [
  38. 'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
  39. 'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
  40. ],
  41. 'required-arguments' => 2,
  42. 'examples' => [
  43. 'drush config-missing-report type action' => 'Displays the missing config report for action config.',
  44. ],
  45. 'aliases' => ['crm'],
  46. 'core' => ['8+'],
  47. 'outputformat' => [
  48. 'default' => 'list',
  49. ],
  50. ];
  51. $items['config-inactive-report'] = [
  52. 'description' => 'Display a list of optional config items from your installed modules, themes, or install profile that are not currently in your active config',
  53. 'arguments' => [
  54. 'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
  55. 'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
  56. ],
  57. 'required-arguments' => 2,
  58. 'examples' => [
  59. 'drush config-inactive-report type action' => 'Displays the inactive config report for action config.',
  60. ],
  61. 'aliases' => ['cri'],
  62. 'core' => ['8+'],
  63. 'outputformat' => [
  64. 'default' => 'list',
  65. ],
  66. ];
  67. $items['config-different-report'] = [
  68. 'description' => 'Display a list of config items that differ from the versions provided by your installed modules, themes, or install profile. See config-diff to show what the differences are.',
  69. 'arguments' => [
  70. 'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
  71. 'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
  72. ],
  73. 'required-arguments' => 2,
  74. 'examples' => [
  75. 'drush config-different-report type action' => 'Displays the differing config report for action config.',
  76. ],
  77. 'aliases' => ['crd'],
  78. 'core' => ['8+'],
  79. 'outputformat' => [
  80. 'default' => 'list',
  81. ],
  82. ];
  83. $items['config-diff'] = [
  84. 'description' => 'Display line-by-line differences for one config item between your active config and the version currently being provided by an installed module, theme, or install profile',
  85. 'arguments' => [
  86. 'name' => 'The config item to diff. See config-different-report to list config items that are different.',
  87. ],
  88. 'required-arguments' => 1,
  89. 'examples' => [
  90. 'drush config-diff block.block.bartik_search' => 'Displays the config differences for the search block in the Bartik theme.',
  91. ],
  92. 'aliases' => ['cfd'],
  93. 'core' => ['8+'],
  94. ];
  95. $items['config-revert'] = [
  96. 'description' => 'Revert one config item in active storage to the version provided by an installed module, theme, or install profile.',
  97. 'arguments' => [
  98. 'name' => 'The config item to revert. See config-different-report to list config items that are different.',
  99. ],
  100. 'required-arguments' => 1,
  101. 'examples' => [
  102. 'drush config-revert block.block.bartik_search' => 'Revert the config for the search block in the Bartik theme to the version provided by the install profile.',
  103. ],
  104. 'aliases' => ['cfr'],
  105. 'core' => ['8+'],
  106. ];
  107. $items['config-import-missing'] = [
  108. 'description' => 'Import a missing or inactive config item provided by an installed module, theme, or install profile. Be sure that requirements are met.',
  109. 'arguments' => [
  110. 'name' => 'The name of the config item to import (usually the ID you would see in the user interface). See config-missing-report to list config items that are missing, and config-inactive-report to list config items that are inactive.',
  111. ],
  112. 'required-arguments' => 1,
  113. 'examples' => [
  114. 'drush config-import-missing block.block.bartik_search' => 'Import the config for the search block in the Bartik theme from the version provided by the install profile.',
  115. ],
  116. 'aliases' => ['cfi'],
  117. 'core' => ['8+'],
  118. ];
  119. $items['config-revert-multiple'] = [
  120. 'description' => 'Revert a set of config items to the versions provided by installed modules, themes, or install profiles. A set is all differing items from one extension, or one type of configuration.',
  121. 'arguments' => [
  122. 'type' => 'Type of set to revert: "module" for all items from a module, "theme" for all items from a theme, "profile" for all items from the install profile, or "type" for all items of one config entity type. See config-different-report to list config items that are different.',
  123. 'name' => 'The machine name of the module, theme, etc. to revert items of. All items in the corresponding config-different-report will be reverted.',
  124. ],
  125. 'required-arguments' => 2,
  126. 'examples' => [
  127. 'drush config-revert-multiple type action' => 'Revert all differing config items of type action.',
  128. ],
  129. 'aliases' => ['cfrm'],
  130. 'core' => ['8+'],
  131. ];
  132. return $items;
  133. }
  134. /**
  135. * Lists available config types.
  136. */
  137. function drush_config_update_ui_config_list_types() {
  138. $list = [];
  139. $definitions = \Drupal::service('config_update.config_list')->listTypes();
  140. return array_keys($definitions);
  141. }
  142. /**
  143. * Runs the config added report.
  144. *
  145. * @param string $name
  146. * Type of config to report on.
  147. */
  148. function drush_config_update_ui_config_added_report($name) {
  149. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig('type', $name);
  150. $added = array_diff($active_list, $install_list, $optional_list);
  151. if (!count($added)) {
  152. drush_print(dt('No added config'), 0, STDERR);
  153. }
  154. sort($added);
  155. return $added;
  156. }
  157. /**
  158. * Runs the config missing report.
  159. *
  160. * @param string $type
  161. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  162. * @param string $name
  163. * Machine name of item to report on.
  164. */
  165. function drush_config_update_ui_config_missing_report($type, $name) {
  166. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  167. $missing = array_diff($install_list, $active_list);
  168. if (!count($missing)) {
  169. drush_print(dt('No missing config'), 0, STDERR);
  170. }
  171. sort($missing);
  172. return $missing;
  173. }
  174. /**
  175. * Runs the config inactive report.
  176. *
  177. * @param string $type
  178. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  179. * @param string $name
  180. * Machine name of item to report on.
  181. */
  182. function drush_config_update_ui_config_inactive_report($type, $name) {
  183. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  184. $missing = array_diff($optional_list, $active_list);
  185. if (!count($missing)) {
  186. drush_print(dt('No inactive config'), 0, STDERR);
  187. }
  188. sort($missing);
  189. return $missing;
  190. }
  191. /**
  192. * Runs the config different report.
  193. *
  194. * @param string $type
  195. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  196. * @param string $name
  197. * Machine name of item to report on.
  198. */
  199. function drush_config_update_ui_config_different_report($type, $name) {
  200. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  201. $reverter = \Drupal::service('config_update.config_update');
  202. $differ = \Drupal::service('config_update.config_diff');
  203. $added = array_diff($active_list, $install_list, $optional_list);
  204. $both = array_diff($active_list, $added);
  205. $different = [];
  206. foreach ($both as $name) {
  207. $active = $reverter->getFromActive('', $name);
  208. $extension = $reverter->getFromExtension('', $name);
  209. if (!$differ->same($active, $extension)) {
  210. $different[] = $name;
  211. }
  212. }
  213. if (!count($different)) {
  214. drush_print(dt('No different config'), 0, STDERR);
  215. }
  216. sort($different);
  217. return $different;
  218. }
  219. /**
  220. * Runs the drush config-diff command.
  221. *
  222. * @param string $name
  223. * Config item to diff.
  224. */
  225. function drush_config_update_ui_config_diff($name) {
  226. $reverter = \Drupal::service('config_update.config_update');
  227. $differ = \Drupal::service('config_update.config_diff');
  228. $formatter = new DiffFormatter();
  229. $extension = $reverter->getFromExtension('', $name);
  230. $active = $reverter->getFromActive('', $name);
  231. if ($extension && $active) {
  232. $diff = $differ->diff($extension, $active);
  233. $output = $formatter->format($diff);
  234. return $output;
  235. }
  236. else {
  237. drush_print(dt('Config is missing, cannot diff'), 0, STDERR);
  238. return '';
  239. }
  240. }
  241. /**
  242. * Runs the drush config-revert command.
  243. *
  244. * @param string $name
  245. * Config item to revert.
  246. */
  247. function drush_config_update_ui_config_revert($name) {
  248. $lister = \Drupal::service('config_update.config_list');
  249. $reverter = \Drupal::service('config_update.config_update');
  250. $manager = \Drupal::service('entity.manager');
  251. // The revert command needs the type and the unprefixed name.
  252. $type = $lister->getTypeNameByConfigName($name);
  253. // The lister gives NULL if simple configuration, but the reverter expects
  254. // 'system.simple' so we convert it.
  255. if ($type === NULL) {
  256. $type = 'system.simple';
  257. }
  258. $shortname = $name;
  259. if ($type != 'system.simple') {
  260. $definition = $manager->getDefinition($type);
  261. $prefix = $definition->getConfigPrefix() . '.';
  262. if (strpos($name, $prefix) === 0) {
  263. $shortname = substr($name, strlen($prefix));
  264. }
  265. }
  266. if ($reverter->revert($type, $shortname)) {
  267. drush_print(dt('The configuration item @name was reverted to its source.', ['@name' => $name]));
  268. }
  269. else {
  270. drush_print(dt('There was an error and the configuration item @name was not reverted.', ['@name' => $name]), 0, STDERR);
  271. }
  272. }
  273. /**
  274. * Runs the drush config-import-missing command.
  275. *
  276. * @param string $name
  277. * Name of config item to import, without prefix.
  278. */
  279. function drush_config_update_ui_config_import_missing($name) {
  280. $lister = \Drupal::service('config_update.config_list');
  281. $reverter = \Drupal::service('config_update.config_update');
  282. $manager = \Drupal::service('entity.manager');
  283. // The import command needs the type and the unprefixed name.
  284. $type = $lister->getTypeNameByConfigName($name);
  285. $shortname = $name;
  286. if ($type && $type != 'system.simple') {
  287. $definition = $manager->getDefinition($type);
  288. $prefix = $definition->getConfigPrefix() . '.';
  289. if (strpos($name, $prefix) === 0) {
  290. $shortname = substr($name, strlen($prefix));
  291. }
  292. }
  293. if ($reverter->import($type, $shortname)) {
  294. drush_print(dt('The configuration item @name was imported from its source.', ['@name' => $name]));
  295. }
  296. else {
  297. drush_print(dt('There was an error and the configuration item @name was not imported.', ['@name' => $name]), 0, STDERR);
  298. drush_print(dt('The configuration was imported from its source.'));
  299. }
  300. }
  301. /**
  302. * Runs the drush config-revert-multiple command.
  303. *
  304. * @param string $type
  305. * Type of config to revert: 'type', 'module', 'theme', or 'profile'.
  306. * @param string $name
  307. * Machine name of type to revert.
  308. */
  309. function drush_config_update_ui_config_revert_multiple($type, $name) {
  310. // This command will print an error if there are no items to revert.
  311. $different = drush_config_update_ui_config_different_report($type, $name);
  312. foreach ($different as $name) {
  313. // This command will print a message if it succeeds or fails for each item.
  314. drush_config_update_ui_config_revert($name);
  315. }
  316. }