ConfigUpdateController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. namespace Drupal\config_update_ui\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Drupal\Core\Diff\DiffFormatter;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Extension\ThemeHandlerInterface;
  7. use Drupal\Core\Site\Settings;
  8. use Drupal\Core\Url;
  9. use Drupal\config_update\ConfigDiffInterface;
  10. use Drupal\config_update\ConfigListInterface;
  11. use Drupal\config_update\ConfigRevertInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Returns responses for Configuration Revert module operations.
  15. */
  16. class ConfigUpdateController extends ControllerBase {
  17. /**
  18. * The config differ.
  19. *
  20. * @var \Drupal\config_update\ConfigDiffInterface
  21. */
  22. protected $configDiff;
  23. /**
  24. * The config lister.
  25. *
  26. * @var \Drupal\config_update\ConfigListInterface
  27. */
  28. protected $configList;
  29. /**
  30. * The config reverter.
  31. *
  32. * @var \Drupal\config_update\ConfigRevertInterface
  33. */
  34. protected $configRevert;
  35. /**
  36. * The diff formatter.
  37. *
  38. * @var \Drupal\Core\Diff\DiffFormatter
  39. */
  40. protected $diffFormatter;
  41. /**
  42. * The module handler.
  43. *
  44. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  45. */
  46. protected $moduleHandler;
  47. /**
  48. * The theme handler.
  49. *
  50. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  51. */
  52. protected $themeHandler;
  53. /**
  54. * Constructs a ConfigUpdateController object.
  55. *
  56. * @param \Drupal\config_update\ConfigDiffInterface $config_diff
  57. * The config differ.
  58. * @param \Drupal\config_update\ConfigListInterface $config_list
  59. * The config lister.
  60. * @param \Drupal\config_update\ConfigRevertInterface $config_update
  61. * The config reverter.
  62. * @param \Drupal\Core\Diff\DiffFormatter $diff_formatter
  63. * The diff formatter to use.
  64. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  65. * The module handler.
  66. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  67. * The theme handler.
  68. */
  69. public function __construct(ConfigDiffInterface $config_diff, ConfigListInterface $config_list, ConfigRevertInterface $config_update, DiffFormatter $diff_formatter, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
  70. $this->configDiff = $config_diff;
  71. $this->configList = $config_list;
  72. $this->configRevert = $config_update;
  73. $this->diffFormatter = $diff_formatter;
  74. $this->diffFormatter->show_header = FALSE;
  75. $this->moduleHandler = $module_handler;
  76. $this->themeHandler = $theme_handler;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public static function create(ContainerInterface $container) {
  82. return new static(
  83. $container->get('config_update.config_diff'),
  84. $container->get('config_update.config_list'),
  85. $container->get('config_update.config_update'),
  86. $container->get('diff.formatter'),
  87. $container->get('module_handler'),
  88. $container->get('theme_handler')
  89. );
  90. }
  91. /**
  92. * Imports configuration from a module, theme, or profile.
  93. *
  94. * Configuration is assumed not to currently exist.
  95. *
  96. * @param string $config_type
  97. * The type of configuration.
  98. * @param string $config_name
  99. * The name of the config item, without the prefix.
  100. *
  101. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  102. * Redirects to the updates report.
  103. */
  104. public function import($config_type, $config_name) {
  105. $this->configRevert->import($config_type, $config_name);
  106. drupal_set_message($this->t('The configuration was imported.'));
  107. return $this->redirect('config_update_ui.report');
  108. }
  109. /**
  110. * Shows the diff between active and provided configuration.
  111. *
  112. * @param string $config_type
  113. * The type of configuration.
  114. * @param string $config_name
  115. * The name of the config item, without the prefix.
  116. *
  117. * @return array
  118. * Render array for page showing differences between them.
  119. */
  120. public function diff($config_type, $config_name) {
  121. $diff = $this->configDiff->diff(
  122. $this->configRevert->getFromExtension($config_type, $config_name),
  123. $this->configRevert->getFromActive($config_type, $config_name)
  124. );
  125. $build = [];
  126. $definition = $this->configList->getType($config_type);
  127. $config_type_label = ($definition) ? $definition->getLabel() : $this->t('Simple configuration');
  128. $build['#title'] = $this->t('Config difference for @type @name', ['@type' => $config_type_label, '@name' => $config_name]);
  129. $build['#attached']['library'][] = 'system/diff';
  130. $build['diff'] = [
  131. '#type' => 'table',
  132. '#header' => [
  133. ['data' => $this->t('Source config'), 'colspan' => '2'],
  134. ['data' => $this->t('Site config'), 'colspan' => '2'],
  135. ],
  136. '#rows' => $this->diffFormatter->format($diff),
  137. '#attributes' => ['class' => ['diff']],
  138. ];
  139. $url = new Url('config_update_ui.report');
  140. $build['back'] = [
  141. '#type' => 'link',
  142. '#attributes' => [
  143. 'class' => [
  144. 'dialog-cancel',
  145. ],
  146. ],
  147. '#title' => $this->t("Back to 'Updates report' page."),
  148. '#url' => $url,
  149. ];
  150. return $build;
  151. }
  152. /**
  153. * Generates the config updates report.
  154. *
  155. * @param string $report_type
  156. * (optional) Type of report to run:
  157. * - type: Configuration entity type.
  158. * - module: Module.
  159. * - theme: Theme.
  160. * - profile: Install profile.
  161. * @param string $name
  162. * (optional) Name of specific item to run report for (config entity type
  163. * ID, module machine name, etc.). Ignored for profile.
  164. *
  165. * @return array
  166. * Render array for report, with section at the top for selecting another
  167. * report to run. If either $report_type or $name is missing, the report
  168. * itself is not generated.
  169. */
  170. public function report($report_type = NULL, $name = NULL) {
  171. $links = $this->generateReportLinks();
  172. $report = $this->generateReport($report_type, $name);
  173. if (!$report) {
  174. return $links;
  175. }
  176. // If there is a report, extract the title, put table of links in a
  177. // details element, and add report to build.
  178. $build = [];
  179. $build['#title'] = $report['#title'];
  180. unset($report['#title']);
  181. $build['links_wrapper'] = [
  182. '#type' => 'details',
  183. '#title' => $this->t('Generate new report'),
  184. '#children' => $links,
  185. ];
  186. $build['report'] = $report;
  187. $build['#attached']['library'][] = 'config_update/report_css';
  188. return $build;
  189. }
  190. /**
  191. * Generates the operations links for running individual reports.
  192. *
  193. * @return array
  194. * Render array for the operations links for running reports.
  195. */
  196. protected function generateReportLinks() {
  197. // These links are put into an 'operations' render array element. They do
  198. // not look good outside of tables. Also note that the array index in
  199. // operations links is used as a class on the LI element. Some classes are
  200. // special in the Seven CSS, such as "contextual", so avoid hitting these
  201. // accidentally by prefixing.
  202. $build = [];
  203. $build['links'] = [
  204. '#type' => 'table',
  205. '#header' => [
  206. $this->t('Report type'),
  207. $this->t('Report on'),
  208. ],
  209. '#rows' => [],
  210. ];
  211. $definitions = $this->configList->listTypes();
  212. $links = [];
  213. foreach ($definitions as $entity_type => $definition) {
  214. $links['type_' . $entity_type] = [
  215. 'title' => $definition->getLabel(),
  216. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => $entity_type]),
  217. ];
  218. }
  219. uasort($links, [$this, 'sortLinks']);
  220. $links = [
  221. 'type_all' => [
  222. 'title' => $this->t('All types'),
  223. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => 'system.all']),
  224. ],
  225. 'type_system.simple' => [
  226. 'title' => $this->t('Simple configuration'),
  227. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => 'system.simple']),
  228. ],
  229. ] + $links;
  230. $build['links']['#rows'][] = [
  231. $this->t('Configuration type'),
  232. [
  233. 'data' => [
  234. '#type' => 'operations',
  235. '#links' => $links,
  236. ],
  237. ],
  238. ];
  239. // Make a list of installed modules.
  240. $profile = Settings::get('install_profile');
  241. $modules = $this->moduleHandler->getModuleList();
  242. $links = [];
  243. foreach ($modules as $machine_name => $module) {
  244. if ($machine_name != $profile) {
  245. $links['module_' . $machine_name] = [
  246. 'title' => $this->moduleHandler->getName($machine_name),
  247. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'module', 'name' => $machine_name]),
  248. ];
  249. }
  250. }
  251. uasort($links, [$this, 'sortLinks']);
  252. $build['links']['#rows'][] = [
  253. $this->t('Module'),
  254. [
  255. 'data' => [
  256. '#type' => 'operations',
  257. '#links' => $links,
  258. ],
  259. ],
  260. ];
  261. // Make a list of installed themes.
  262. $themes = $this->themeHandler->listInfo();
  263. $links = [];
  264. foreach ($themes as $machine_name => $theme) {
  265. $links['theme_' . $machine_name] = [
  266. 'title' => $this->themeHandler->getName($machine_name),
  267. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'theme', 'name' => $machine_name]),
  268. ];
  269. }
  270. uasort($links, [$this, 'sortLinks']);
  271. $build['links']['#rows'][] = [
  272. $this->t('Theme'),
  273. [
  274. 'data' => [
  275. '#type' => 'operations',
  276. '#links' => $links,
  277. ],
  278. ],
  279. ];
  280. // Profile is just one option.
  281. $links = [];
  282. $links['profile_' . $profile] = [
  283. 'title' => $this->moduleHandler->getName($profile),
  284. 'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'profile']),
  285. ];
  286. $build['links']['#rows'][] = [
  287. $this->t('Installation profile'),
  288. [
  289. 'data' => [
  290. '#type' => 'operations',
  291. '#links' => $links,
  292. ],
  293. ],
  294. ];
  295. return $build;
  296. }
  297. /**
  298. * Generates a report about config updates.
  299. *
  300. * @param string $report_type
  301. * Type of report to generate: 'type', 'module', 'theme', or 'profile'.
  302. * @param string $value
  303. * Machine name of a configuration type, module, or theme to generate the
  304. * report for. Ignored for profile, since that uses the active profile.
  305. *
  306. * @return array
  307. * Render array for the updates report. Empty if invalid or missing
  308. * report type or value.
  309. */
  310. protected function generateReport($report_type, $value) {
  311. // Figure out what to name the report, and incidentally, validate that
  312. // $value exists for this type of report.
  313. switch ($report_type) {
  314. case 'type':
  315. if ($value == 'system.all') {
  316. $label = $this->t('All configuration');
  317. }
  318. elseif ($value == 'system.simple') {
  319. $label = $this->t('Simple configuration');
  320. }
  321. else {
  322. $definition = $this->configList->getType($value);
  323. if (!$definition) {
  324. return NULL;
  325. }
  326. $label = $this->t('@name configuration', ['@name' => $definition->getLabel()]);
  327. }
  328. break;
  329. case 'module':
  330. $list = $this->moduleHandler->getModuleList();
  331. if (!isset($list[$value])) {
  332. return NULL;
  333. }
  334. $label = $this->t('@name module', ['@name' => $this->moduleHandler->getName($value)]);
  335. break;
  336. case 'theme':
  337. $list = $this->themeHandler->listInfo();
  338. if (!isset($list[$value])) {
  339. return NULL;
  340. }
  341. $label = $this->t('@name theme', ['@name' => $this->themeHandler->getName($value)]);
  342. break;
  343. case 'profile':
  344. $profile = Settings::get('install_profile');
  345. $label = $this->t('@name profile', ['@name' => $this->moduleHandler->getName($profile)]);
  346. break;
  347. default:
  348. return NULL;
  349. }
  350. // List the active and extension-provided config.
  351. list($active_list, $install_list, $optional_list) = $this->configList->listConfig($report_type, $value);
  352. // Build the report.
  353. $build = [];
  354. $build['#title'] = $this->t('Configuration updates report for @label', ['@label' => $label]);
  355. $build['report_header'] = ['#markup' => '<h3>' . $this->t('Updates report') . '</h3>'];
  356. // List items missing from site.
  357. $removed = array_diff($install_list, $active_list);
  358. $build['removed'] = [
  359. '#caption' => $this->t('Missing configuration items'),
  360. '#empty' => $this->t('None: all provided configuration items are in your active configuration.'),
  361. ] + $this->makeReportTable($removed, 'extension', ['import']);
  362. // List optional items that are not installed.
  363. $inactive = array_diff($optional_list, $active_list);
  364. $build['inactive'] = [
  365. '#caption' => $this->t('Inactive optional items'),
  366. '#empty' => $this->t('None: all optional configuration items are in your active configuration.'),
  367. ] + $this->makeReportTable($inactive, 'extension', ['import']);
  368. // List items added to site, which only makes sense in the report for a
  369. // config type.
  370. $added = array_diff($active_list, $install_list, $optional_list);
  371. if ($report_type == 'type') {
  372. $build['added'] = [
  373. '#caption' => $this->t('Added configuration items'),
  374. '#empty' => $this->t('None: all active configuration items of this type were provided by modules, themes, or install profile.'),
  375. ] + $this->makeReportTable($added, 'active', ['export', 'delete']);
  376. }
  377. // For differences, we need to go through the array of config in both
  378. // and see if each config item is the same or not.
  379. $both = array_diff($active_list, $added);
  380. $different = [];
  381. foreach ($both as $name) {
  382. if (!$this->configDiff->same(
  383. $this->configRevert->getFromExtension('', $name),
  384. $this->configRevert->getFromActive('', $name)
  385. )) {
  386. $different[] = $name;
  387. }
  388. }
  389. $build['different'] = [
  390. '#caption' => $this->t('Changed configuration items'),
  391. '#empty' => $this->t('None: no active configuration items differ from their current provided versions.'),
  392. ] + $this->makeReportTable($different, 'active', ['diff', 'export', 'revert']);
  393. return $build;
  394. }
  395. /**
  396. * Builds a table for the report.
  397. *
  398. * @param string[] $names
  399. * List of machine names of config items for the table.
  400. * @param string $storage
  401. * Config storage the items can be loaded from, either 'active' or
  402. * 'extension'.
  403. * @param string[] $actions
  404. * Action links to include, one or more of:
  405. * - diff
  406. * - revert
  407. * - export
  408. * - import
  409. * - delete
  410. *
  411. * @return array
  412. * Render array for the table, not including the #empty and #prefix
  413. * properties.
  414. */
  415. protected function makeReportTable($names, $storage, $actions) {
  416. $build = [];
  417. $build['#type'] = 'table';
  418. $build['#attributes'] = ['class' => ['config-update-report']];
  419. $build['#header'] = [
  420. 'name' => [
  421. 'data' => $this->t('Machine name'),
  422. ],
  423. 'label' => [
  424. 'data' => $this->t('Label (if any)'),
  425. 'class' => [RESPONSIVE_PRIORITY_LOW],
  426. ],
  427. 'type' => [
  428. 'data' => $this->t('Type'),
  429. 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
  430. ],
  431. 'operations' => [
  432. 'data' => $this->t('Operations'),
  433. ],
  434. ];
  435. $build['#rows'] = [];
  436. foreach ($names as $name) {
  437. $row = [];
  438. if ($storage == 'active') {
  439. $config = $this->configRevert->getFromActive('', $name);
  440. }
  441. else {
  442. $config = $this->configRevert->getFromExtension('', $name);
  443. }
  444. // Figure out what type of config it is, and get the ID.
  445. $entity_type = $this->configList->getTypeNameByConfigName($name);
  446. if (!$entity_type) {
  447. // This is simple config.
  448. $id = $name;
  449. $type_label = $this->t('Simple configuration');
  450. $entity_type = 'system.simple';
  451. }
  452. else {
  453. $definition = $this->configList->getType($entity_type);
  454. $id_key = $definition->getKey('id');
  455. $id = $config[$id_key];
  456. $type_label = $definition->getLabel();
  457. }
  458. $label = (isset($config['label'])) ? $config['label'] : '';
  459. $row[] = $name;
  460. $row[] = $label;
  461. $row[] = $type_label;
  462. $links = [];
  463. $routes = [
  464. 'export' => 'config.export_single',
  465. 'import' => 'config_update_ui.import',
  466. 'diff' => 'config_update_ui.diff',
  467. 'revert' => 'config_update_ui.revert',
  468. 'delete' => 'config_update_ui.delete',
  469. ];
  470. $titles = [
  471. 'export' => $this->t('Export'),
  472. 'import' => $this->t('Import from source'),
  473. 'diff' => $this->t('Show differences'),
  474. 'revert' => $this->t('Revert to source'),
  475. 'delete' => $this->t('Delete'),
  476. ];
  477. foreach ($actions as $action) {
  478. $links[$action] = [
  479. 'url' => Url::fromRoute($routes[$action], ['config_type' => $entity_type, 'config_name' => $id]),
  480. 'title' => $titles[$action],
  481. ];
  482. }
  483. $row[] = [
  484. 'data' => [
  485. '#type' => 'operations',
  486. '#links' => $links,
  487. ],
  488. ];
  489. $build['#rows'][] = $row;
  490. }
  491. return $build;
  492. }
  493. /**
  494. * Compares links for uasort(), to sort by displayed link title.
  495. */
  496. protected static function sortLinks($link1, $link2) {
  497. $title1 = $link1['title'];
  498. $title2 = $link2['title'];
  499. if ($title1 == $title2) {
  500. return 0;
  501. }
  502. return ($title1 < $title2) ? -1 : 1;
  503. }
  504. }