annotate core/modules/config/src/Controller/ConfigController.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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@18 10 use Drupal\Core\File\Exception\FileException;
Chris@18 11 use Drupal\Core\File\FileSystemInterface;
Chris@0 12 use Drupal\Core\Serialization\Yaml;
Chris@0 13 use Drupal\Core\Url;
Chris@0 14 use Drupal\system\FileDownloadController;
Chris@0 15 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 16 use Symfony\Component\HttpFoundation\Request;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Returns responses for config module routes.
Chris@0 20 */
Chris@0 21 class ConfigController implements ContainerInjectionInterface {
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The target storage.
Chris@0 25 *
Chris@0 26 * @var \Drupal\Core\Config\StorageInterface
Chris@0 27 */
Chris@0 28 protected $targetStorage;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * The source storage.
Chris@0 32 *
Chris@0 33 * @var \Drupal\Core\Config\StorageInterface
Chris@0 34 */
Chris@0 35 protected $sourceStorage;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The configuration manager.
Chris@0 39 *
Chris@0 40 * @var \Drupal\Core\Config\ConfigManagerInterface
Chris@0 41 */
Chris@0 42 protected $configManager;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * The file download controller.
Chris@0 46 *
Chris@0 47 * @var \Drupal\system\FileDownloadController
Chris@0 48 */
Chris@0 49 protected $fileDownloadController;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * The diff formatter.
Chris@0 53 *
Chris@0 54 * @var \Drupal\Core\Diff\DiffFormatter
Chris@0 55 */
Chris@0 56 protected $diffFormatter;
Chris@0 57
Chris@0 58 /**
Chris@18 59 * The file system.
Chris@18 60 *
Chris@18 61 * @var \Drupal\Core\File\FileSystemInterface
Chris@18 62 */
Chris@18 63 protected $fileSystem;
Chris@18 64
Chris@18 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public static function create(ContainerInterface $container) {
Chris@0 69 return new static(
Chris@0 70 $container->get('config.storage'),
Chris@0 71 $container->get('config.storage.sync'),
Chris@0 72 $container->get('config.manager'),
Chris@0 73 new FileDownloadController(),
Chris@18 74 $container->get('diff.formatter'),
Chris@18 75 $container->get('file_system')
Chris@0 76 );
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Constructs a ConfigController object.
Chris@0 81 *
Chris@0 82 * @param \Drupal\Core\Config\StorageInterface $target_storage
Chris@0 83 * The target storage.
Chris@0 84 * @param \Drupal\Core\Config\StorageInterface $source_storage
Chris@18 85 * The source storage.
Chris@18 86 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
Chris@18 87 * The config manager.
Chris@0 88 * @param \Drupal\system\FileDownloadController $file_download_controller
Chris@0 89 * The file download controller.
Chris@18 90 * @param \Drupal\Core\Diff\DiffFormatter $diff_formatter
Chris@18 91 * The diff formatter.
Chris@18 92 * @param \Drupal\Core\File\FileSystemInterface $file_system
Chris@18 93 * The file system.
Chris@0 94 */
Chris@18 95 public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ConfigManagerInterface $config_manager, FileDownloadController $file_download_controller, DiffFormatter $diff_formatter, FileSystemInterface $file_system) {
Chris@0 96 $this->targetStorage = $target_storage;
Chris@0 97 $this->sourceStorage = $source_storage;
Chris@0 98 $this->configManager = $config_manager;
Chris@0 99 $this->fileDownloadController = $file_download_controller;
Chris@0 100 $this->diffFormatter = $diff_formatter;
Chris@18 101 $this->fileSystem = $file_system;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Downloads a tarball of the site configuration.
Chris@0 106 */
Chris@0 107 public function downloadExport() {
Chris@18 108 try {
Chris@18 109 $this->fileSystem->delete(file_directory_temp() . '/config.tar.gz');
Chris@18 110 }
Chris@18 111 catch (FileException $e) {
Chris@18 112 // Ignore failed deletes.
Chris@18 113 }
Chris@0 114
Chris@0 115 $archiver = new ArchiveTar(file_directory_temp() . '/config.tar.gz', 'gz');
Chris@0 116 // Get raw configuration data without overrides.
Chris@0 117 foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
Chris@0 118 $archiver->addString("$name.yml", Yaml::encode($this->configManager->getConfigFactory()->get($name)->getRawData()));
Chris@0 119 }
Chris@0 120 // Get all override data from the remaining collections.
Chris@0 121 foreach ($this->targetStorage->getAllCollectionNames() as $collection) {
Chris@0 122 $collection_storage = $this->targetStorage->createCollection($collection);
Chris@0 123 foreach ($collection_storage->listAll() as $name) {
Chris@0 124 $archiver->addString(str_replace('.', '/', $collection) . "/$name.yml", Yaml::encode($collection_storage->read($name)));
Chris@0 125 }
Chris@0 126 }
Chris@0 127
Chris@0 128 $request = new Request(['file' => 'config.tar.gz']);
Chris@0 129 return $this->fileDownloadController->download($request, 'temporary');
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Shows diff of specified configuration file.
Chris@0 134 *
Chris@0 135 * @param string $source_name
Chris@0 136 * The name of the configuration file.
Chris@0 137 * @param string $target_name
Chris@0 138 * (optional) The name of the target configuration file if different from
Chris@0 139 * the $source_name.
Chris@0 140 * @param string $collection
Chris@0 141 * (optional) The configuration collection name. Defaults to the default
Chris@0 142 * collection.
Chris@0 143 *
Chris@0 144 * @return string
Chris@0 145 * Table showing a two-way diff between the active and staged configuration.
Chris@0 146 */
Chris@0 147 public function diff($source_name, $target_name = NULL, $collection = NULL) {
Chris@0 148 if (!isset($collection)) {
Chris@0 149 $collection = StorageInterface::DEFAULT_COLLECTION;
Chris@0 150 }
Chris@0 151 $diff = $this->configManager->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
Chris@0 152 $this->diffFormatter->show_header = FALSE;
Chris@0 153
Chris@0 154 $build = [];
Chris@0 155
Chris@0 156 $build['#title'] = t('View changes of @config_file', ['@config_file' => $source_name]);
Chris@0 157 // Add the CSS for the inline diff.
Chris@0 158 $build['#attached']['library'][] = 'system/diff';
Chris@0 159
Chris@0 160 $build['diff'] = [
Chris@0 161 '#type' => 'table',
Chris@0 162 '#attributes' => [
Chris@0 163 'class' => ['diff'],
Chris@0 164 ],
Chris@0 165 '#header' => [
Chris@0 166 ['data' => t('Active'), 'colspan' => '2'],
Chris@0 167 ['data' => t('Staged'), 'colspan' => '2'],
Chris@0 168 ],
Chris@0 169 '#rows' => $this->diffFormatter->format($diff),
Chris@0 170 ];
Chris@0 171
Chris@0 172 $build['back'] = [
Chris@0 173 '#type' => 'link',
Chris@0 174 '#attributes' => [
Chris@0 175 'class' => [
Chris@0 176 'dialog-cancel',
Chris@0 177 ],
Chris@0 178 ],
Chris@0 179 '#title' => "Back to 'Synchronize configuration' page.",
Chris@0 180 '#url' => Url::fromRoute('config.sync'),
Chris@0 181 ];
Chris@0 182
Chris@0 183 return $build;
Chris@0 184 }
Chris@0 185
Chris@0 186 }