Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / config / src / Controller / ConfigController.php
1 <?php
2
3 namespace Drupal\config\Controller;
4
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;
11 use Drupal\Core\Url;
12 use Drupal\system\FileDownloadController;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\HttpFoundation\Request;
15
16 /**
17  * Returns responses for config module routes.
18  */
19 class ConfigController implements ContainerInjectionInterface {
20
21   /**
22    * The target storage.
23    *
24    * @var \Drupal\Core\Config\StorageInterface
25    */
26   protected $targetStorage;
27
28   /**
29    * The source storage.
30    *
31    * @var \Drupal\Core\Config\StorageInterface
32    */
33   protected $sourceStorage;
34
35   /**
36    * The configuration manager.
37    *
38    * @var \Drupal\Core\Config\ConfigManagerInterface
39    */
40   protected $configManager;
41
42   /**
43    * The file download controller.
44    *
45    * @var \Drupal\system\FileDownloadController
46    */
47   protected $fileDownloadController;
48
49   /**
50    * The diff formatter.
51    *
52    * @var \Drupal\Core\Diff\DiffFormatter
53    */
54   protected $diffFormatter;
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function create(ContainerInterface $container) {
60     return new static(
61       $container->get('config.storage'),
62       $container->get('config.storage.sync'),
63       $container->get('config.manager'),
64       new FileDownloadController(),
65       $container->get('diff.formatter')
66     );
67   }
68
69   /**
70    * Constructs a ConfigController object.
71    *
72    * @param \Drupal\Core\Config\StorageInterface $target_storage
73    *   The target storage.
74    * @param \Drupal\Core\Config\StorageInterface $source_storage
75    *   The source storage
76    * @param \Drupal\system\FileDownloadController $file_download_controller
77    *   The file download controller.
78    */
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;
85   }
86
87   /**
88    * Downloads a tarball of the site configuration.
89    */
90   public function downloadExport() {
91     file_unmanaged_delete(file_directory_temp() . '/config.tar.gz');
92
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()));
97     }
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)));
103       }
104     }
105
106     $request = new Request(['file' => 'config.tar.gz']);
107     return $this->fileDownloadController->download($request, 'temporary');
108   }
109
110   /**
111    * Shows diff of specified configuration file.
112    *
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
117    *   the $source_name.
118    * @param string $collection
119    *   (optional) The configuration collection name. Defaults to the default
120    *   collection.
121    *
122    * @return string
123    *   Table showing a two-way diff between the active and staged configuration.
124    */
125   public function diff($source_name, $target_name = NULL, $collection = NULL) {
126     if (!isset($collection)) {
127       $collection = StorageInterface::DEFAULT_COLLECTION;
128     }
129     $diff = $this->configManager->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
130     $this->diffFormatter->show_header = FALSE;
131
132     $build = [];
133
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';
137
138     $build['diff'] = [
139       '#type' => 'table',
140       '#attributes' => [
141         'class' => ['diff'],
142       ],
143       '#header' => [
144         ['data' => t('Active'), 'colspan' => '2'],
145         ['data' => t('Staged'), 'colspan' => '2'],
146       ],
147       '#rows' => $this->diffFormatter->format($diff),
148     ];
149
150     $build['back'] = [
151       '#type' => 'link',
152       '#attributes' => [
153         'class' => [
154           'dialog-cancel',
155         ],
156       ],
157       '#title' => "Back to 'Synchronize configuration' page.",
158       '#url' => Url::fromRoute('config.sync'),
159     ];
160
161     return $build;
162   }
163
164 }