annotate core/modules/config/src/Controller/ConfigController.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\config\Controller;
Chris@0 4
Chris@0 5 use Drupal\Core\Archiver\ArchiveTar;
Chris@0 6 use Drupal\Core\Config\ConfigManagerInterface;
Chris@0 7 use Drupal\Core\Config\StorageInterface;
Chris@0 8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Chris@0 9 use Drupal\Core\Diff\DiffFormatter;
Chris@0 10 use Drupal\Core\Serialization\Yaml;
Chris@0 11 use Drupal\Core\Url;
Chris@0 12 use Drupal\system\FileDownloadController;
Chris@0 13 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 14 use Symfony\Component\HttpFoundation\Request;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Returns responses for config module routes.
Chris@0 18 */
Chris@0 19 class ConfigController implements ContainerInjectionInterface {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The target storage.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\Config\StorageInterface
Chris@0 25 */
Chris@0 26 protected $targetStorage;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The source storage.
Chris@0 30 *
Chris@0 31 * @var \Drupal\Core\Config\StorageInterface
Chris@0 32 */
Chris@0 33 protected $sourceStorage;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The configuration manager.
Chris@0 37 *
Chris@0 38 * @var \Drupal\Core\Config\ConfigManagerInterface
Chris@0 39 */
Chris@0 40 protected $configManager;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * The file download controller.
Chris@0 44 *
Chris@0 45 * @var \Drupal\system\FileDownloadController
Chris@0 46 */
Chris@0 47 protected $fileDownloadController;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * The diff formatter.
Chris@0 51 *
Chris@0 52 * @var \Drupal\Core\Diff\DiffFormatter
Chris@0 53 */
Chris@0 54 protected $diffFormatter;
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 public static function create(ContainerInterface $container) {
Chris@0 60 return new static(
Chris@0 61 $container->get('config.storage'),
Chris@0 62 $container->get('config.storage.sync'),
Chris@0 63 $container->get('config.manager'),
Chris@0 64 new FileDownloadController(),
Chris@0 65 $container->get('diff.formatter')
Chris@0 66 );
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Constructs a ConfigController object.
Chris@0 71 *
Chris@0 72 * @param \Drupal\Core\Config\StorageInterface $target_storage
Chris@0 73 * The target storage.
Chris@0 74 * @param \Drupal\Core\Config\StorageInterface $source_storage
Chris@0 75 * The source storage
Chris@0 76 * @param \Drupal\system\FileDownloadController $file_download_controller
Chris@0 77 * The file download controller.
Chris@0 78 */
Chris@0 79 public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ConfigManagerInterface $config_manager, FileDownloadController $file_download_controller, DiffFormatter $diff_formatter) {
Chris@0 80 $this->targetStorage = $target_storage;
Chris@0 81 $this->sourceStorage = $source_storage;
Chris@0 82 $this->configManager = $config_manager;
Chris@0 83 $this->fileDownloadController = $file_download_controller;
Chris@0 84 $this->diffFormatter = $diff_formatter;
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * Downloads a tarball of the site configuration.
Chris@0 89 */
Chris@0 90 public function downloadExport() {
Chris@0 91 file_unmanaged_delete(file_directory_temp() . '/config.tar.gz');
Chris@0 92
Chris@0 93 $archiver = new ArchiveTar(file_directory_temp() . '/config.tar.gz', 'gz');
Chris@0 94 // Get raw configuration data without overrides.
Chris@0 95 foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
Chris@0 96 $archiver->addString("$name.yml", Yaml::encode($this->configManager->getConfigFactory()->get($name)->getRawData()));
Chris@0 97 }
Chris@0 98 // Get all override data from the remaining collections.
Chris@0 99 foreach ($this->targetStorage->getAllCollectionNames() as $collection) {
Chris@0 100 $collection_storage = $this->targetStorage->createCollection($collection);
Chris@0 101 foreach ($collection_storage->listAll() as $name) {
Chris@0 102 $archiver->addString(str_replace('.', '/', $collection) . "/$name.yml", Yaml::encode($collection_storage->read($name)));
Chris@0 103 }
Chris@0 104 }
Chris@0 105
Chris@0 106 $request = new Request(['file' => 'config.tar.gz']);
Chris@0 107 return $this->fileDownloadController->download($request, 'temporary');
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * Shows diff of specified configuration file.
Chris@0 112 *
Chris@0 113 * @param string $source_name
Chris@0 114 * The name of the configuration file.
Chris@0 115 * @param string $target_name
Chris@0 116 * (optional) The name of the target configuration file if different from
Chris@0 117 * the $source_name.
Chris@0 118 * @param string $collection
Chris@0 119 * (optional) The configuration collection name. Defaults to the default
Chris@0 120 * collection.
Chris@0 121 *
Chris@0 122 * @return string
Chris@0 123 * Table showing a two-way diff between the active and staged configuration.
Chris@0 124 */
Chris@0 125 public function diff($source_name, $target_name = NULL, $collection = NULL) {
Chris@0 126 if (!isset($collection)) {
Chris@0 127 $collection = StorageInterface::DEFAULT_COLLECTION;
Chris@0 128 }
Chris@0 129 $diff = $this->configManager->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
Chris@0 130 $this->diffFormatter->show_header = FALSE;
Chris@0 131
Chris@0 132 $build = [];
Chris@0 133
Chris@0 134 $build['#title'] = t('View changes of @config_file', ['@config_file' => $source_name]);
Chris@0 135 // Add the CSS for the inline diff.
Chris@0 136 $build['#attached']['library'][] = 'system/diff';
Chris@0 137
Chris@0 138 $build['diff'] = [
Chris@0 139 '#type' => 'table',
Chris@0 140 '#attributes' => [
Chris@0 141 'class' => ['diff'],
Chris@0 142 ],
Chris@0 143 '#header' => [
Chris@0 144 ['data' => t('Active'), 'colspan' => '2'],
Chris@0 145 ['data' => t('Staged'), 'colspan' => '2'],
Chris@0 146 ],
Chris@0 147 '#rows' => $this->diffFormatter->format($diff),
Chris@0 148 ];
Chris@0 149
Chris@0 150 $build['back'] = [
Chris@0 151 '#type' => 'link',
Chris@0 152 '#attributes' => [
Chris@0 153 'class' => [
Chris@0 154 'dialog-cancel',
Chris@0 155 ],
Chris@0 156 ],
Chris@0 157 '#title' => "Back to 'Synchronize configuration' page.",
Chris@0 158 '#url' => Url::fromRoute('config.sync'),
Chris@0 159 ];
Chris@0 160
Chris@0 161 return $build;
Chris@0 162 }
Chris@0 163
Chris@0 164 }