config_update_ui.drush.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 imported from 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. 'drupal dependencies' => ['config_update_ui'],
  78. 'aliases' => ['crd'],
  79. 'core' => ['8+'],
  80. 'outputformat' => [
  81. 'default' => 'list',
  82. ],
  83. ];
  84. $items['config-diff'] = [
  85. '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',
  86. 'arguments' => [
  87. 'name' => 'The config item to diff. See config-different-report to list config items that are different.',
  88. ],
  89. 'required-arguments' => 1,
  90. 'examples' => [
  91. 'drush config-diff block.block.bartik_search' => 'Displays the config differences for the search block in the Bartik theme.',
  92. ],
  93. 'aliases' => ['cfd'],
  94. 'core' => ['8+'],
  95. ];
  96. return $items;
  97. }
  98. /**
  99. * Lists available config types.
  100. */
  101. function drush_config_update_ui_config_list_types() {
  102. $list = [];
  103. $definitions = \Drupal::service('config_update.config_list')->listTypes();
  104. return array_keys($definitions);
  105. }
  106. /**
  107. * Runs the config added report.
  108. *
  109. * @param string $name
  110. * Type of config to report on.
  111. */
  112. function drush_config_update_ui_config_added_report($name) {
  113. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig('type', $name);
  114. $added = array_diff($active_list, $install_list, $optional_list);
  115. if (!count($added)) {
  116. drush_print(dt('No added config'), 0, STDERR);
  117. }
  118. sort($added);
  119. return $added;
  120. }
  121. /**
  122. * Runs the config missing report.
  123. *
  124. * @param string $type
  125. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  126. * @param string $name
  127. * Machine name of item to report on.
  128. */
  129. function drush_config_update_ui_config_missing_report($type, $name) {
  130. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  131. $missing = array_diff($install_list, $active_list);
  132. if (!count($missing)) {
  133. drush_print(dt('No missing config'), 0, STDERR);
  134. }
  135. sort($missing);
  136. return $missing;
  137. }
  138. /**
  139. * Runs the config inactive report.
  140. *
  141. * @param string $type
  142. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  143. * @param string $name
  144. * Machine name of item to report on.
  145. */
  146. function drush_config_update_ui_config_inactive_report($type, $name) {
  147. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  148. $missing = array_diff($optional_list, $active_list);
  149. if (!count($missing)) {
  150. drush_print(dt('No inactive config'), 0, STDERR);
  151. }
  152. sort($missing);
  153. return $missing;
  154. }
  155. /**
  156. * Runs the config different report.
  157. *
  158. * @param string $type
  159. * Type of report to run: 'type', 'module', 'theme', or 'profile'.
  160. * @param string $name
  161. * Machine name of item to report on.
  162. */
  163. function drush_config_update_ui_config_different_report($type, $name) {
  164. list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);
  165. $reverter = \Drupal::service('config_update.config_update');
  166. $differ = \Drupal::service('config_update.config_diff');
  167. $added = array_diff($active_list, $install_list, $optional_list);
  168. $both = array_diff($active_list, $added);
  169. $different = [];
  170. foreach ($both as $name) {
  171. $active = $reverter->getFromActive('', $name);
  172. $extension = $reverter->getFromExtension('', $name);
  173. if (!$differ->same($active, $extension)) {
  174. $different[] = $name;
  175. }
  176. }
  177. if (!count($different)) {
  178. drush_print(dt('No different config'), 0, STDERR);
  179. }
  180. sort($different);
  181. return $different;
  182. }
  183. /**
  184. * Runs the drush config-diff command.
  185. *
  186. * @param string $name
  187. * Config item to diff.
  188. */
  189. function drush_config_update_ui_config_diff($name) {
  190. $reverter = \Drupal::service('config_update.config_update');
  191. $differ = \Drupal::service('config_update.config_diff');
  192. $formatter = new DiffFormatter();
  193. $extension = $reverter->getFromExtension('', $name);
  194. $active = $reverter->getFromActive('', $name);
  195. if ($extension && !$active) {
  196. $diff = $differ->diff($extension, $active);
  197. drush_print($formatter->format($diff));
  198. }
  199. else {
  200. drush_print(dt('Config is missing, cannot diff'), 0, STDERR);
  201. }
  202. }