3 namespace Drupal\config\Controller;
5 use Drupal\Core\Archiver\ArchiveTar;
6 use Drupal\Core\Config\ConfigManagerInterface;
7 use Drupal\Core\Config\StorageInterface;
8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
9 use Drupal\Core\Diff\DiffFormatter;
10 use Drupal\Core\Serialization\Yaml;
12 use Drupal\system\FileDownloadController;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\HttpFoundation\Request;
17 * Returns responses for config module routes.
19 class ConfigController implements ContainerInjectionInterface {
24 * @var \Drupal\Core\Config\StorageInterface
26 protected $targetStorage;
31 * @var \Drupal\Core\Config\StorageInterface
33 protected $sourceStorage;
36 * The configuration manager.
38 * @var \Drupal\Core\Config\ConfigManagerInterface
40 protected $configManager;
43 * The file download controller.
45 * @var \Drupal\system\FileDownloadController
47 protected $fileDownloadController;
52 * @var \Drupal\Core\Diff\DiffFormatter
54 protected $diffFormatter;
59 public static function create(ContainerInterface $container) {
61 $container->get('config.storage'),
62 $container->get('config.storage.sync'),
63 $container->get('config.manager'),
64 new FileDownloadController(),
65 $container->get('diff.formatter')
70 * Constructs a ConfigController object.
72 * @param \Drupal\Core\Config\StorageInterface $target_storage
74 * @param \Drupal\Core\Config\StorageInterface $source_storage
76 * @param \Drupal\system\FileDownloadController $file_download_controller
77 * The file download controller.
79 public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ConfigManagerInterface $config_manager, FileDownloadController $file_download_controller, DiffFormatter $diff_formatter) {
80 $this->targetStorage = $target_storage;
81 $this->sourceStorage = $source_storage;
82 $this->configManager = $config_manager;
83 $this->fileDownloadController = $file_download_controller;
84 $this->diffFormatter = $diff_formatter;
88 * Downloads a tarball of the site configuration.
90 public function downloadExport() {
91 file_unmanaged_delete(file_directory_temp() . '/config.tar.gz');
93 $archiver = new ArchiveTar(file_directory_temp() . '/config.tar.gz', 'gz');
94 // Get raw configuration data without overrides.
95 foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
96 $archiver->addString("$name.yml", Yaml::encode($this->configManager->getConfigFactory()->get($name)->getRawData()));
98 // Get all override data from the remaining collections.
99 foreach ($this->targetStorage->getAllCollectionNames() as $collection) {
100 $collection_storage = $this->targetStorage->createCollection($collection);
101 foreach ($collection_storage->listAll() as $name) {
102 $archiver->addString(str_replace('.', '/', $collection) . "/$name.yml", Yaml::encode($collection_storage->read($name)));
106 $request = new Request(['file' => 'config.tar.gz']);
107 return $this->fileDownloadController->download($request, 'temporary');
111 * Shows diff of specified configuration file.
113 * @param string $source_name
114 * The name of the configuration file.
115 * @param string $target_name
116 * (optional) The name of the target configuration file if different from
118 * @param string $collection
119 * (optional) The configuration collection name. Defaults to the default
123 * Table showing a two-way diff between the active and staged configuration.
125 public function diff($source_name, $target_name = NULL, $collection = NULL) {
126 if (!isset($collection)) {
127 $collection = StorageInterface::DEFAULT_COLLECTION;
129 $diff = $this->configManager->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
130 $this->diffFormatter->show_header = FALSE;
134 $build['#title'] = t('View changes of @config_file', ['@config_file' => $source_name]);
135 // Add the CSS for the inline diff.
136 $build['#attached']['library'][] = 'system/diff';
144 ['data' => t('Active'), 'colspan' => '2'],
145 ['data' => t('Staged'), 'colspan' => '2'],
147 '#rows' => $this->diffFormatter->format($diff),
157 '#title' => "Back to 'Synchronize configuration' page.",
158 '#url' => Url::fromRoute('config.sync'),