View source
<?php
namespace Drupal\config\Controller;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\ImportStorageTransformer;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Diff\DiffFormatter;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Url;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ConfigController implements ContainerInjectionInterface {
protected $targetStorage;
protected $syncStorage;
protected $importTransformer;
protected $configManager;
protected $exportStorage;
protected $fileDownloadController;
protected $diffFormatter;
protected $fileSystem;
public static function create(ContainerInterface $container) {
return new static($container
->get('config.storage'), $container
->get('config.storage.sync'), $container
->get('config.manager'), FileDownloadController::create($container), $container
->get('diff.formatter'), $container
->get('file_system'), $container
->get('config.storage.export'), $container
->get('config.import_transformer'));
}
public function __construct(StorageInterface $target_storage, StorageInterface $sync_storage, ConfigManagerInterface $config_manager, FileDownloadController $file_download_controller, DiffFormatter $diff_formatter, FileSystemInterface $file_system, StorageInterface $export_storage = NULL, ImportStorageTransformer $import_transformer = NULL) {
$this->targetStorage = $target_storage;
$this->syncStorage = $sync_storage;
$this->configManager = $config_manager;
$this->fileDownloadController = $file_download_controller;
$this->diffFormatter = $diff_formatter;
$this->fileSystem = $file_system;
if (is_null($export_storage)) {
@trigger_error('The config.storage.export service must be passed to ConfigController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3037022.', E_USER_DEPRECATED);
$export_storage = \Drupal::service('config.storage.export');
}
$this->exportStorage = $export_storage;
if (is_null($import_transformer)) {
@trigger_error('The config.import_transformer service must be passed to ConfigController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3066005.', E_USER_DEPRECATED);
$import_transformer = \Drupal::service('config.import_transformer');
}
$this->importTransformer = $import_transformer;
}
public function downloadExport() {
try {
$this->fileSystem
->delete($this->fileSystem
->getTempDirectory() . '/config.tar.gz');
} catch (FileException $e) {
}
$archiver = new ArchiveTar($this->fileSystem
->getTempDirectory() . '/config.tar.gz', 'gz');
foreach ($this->exportStorage
->listAll() as $name) {
$archiver
->addString("{$name}.yml", Yaml::encode($this->exportStorage
->read($name)));
}
foreach ($this->exportStorage
->getAllCollectionNames() as $collection) {
$collection_storage = $this->exportStorage
->createCollection($collection);
foreach ($collection_storage
->listAll() as $name) {
$archiver
->addString(str_replace('.', '/', $collection) . "/{$name}.yml", Yaml::encode($collection_storage
->read($name)));
}
}
$request = new Request([
'file' => 'config.tar.gz',
]);
return $this->fileDownloadController
->download($request, 'temporary');
}
public function diff($source_name, $target_name = NULL, $collection = NULL) {
if (!isset($collection)) {
$collection = StorageInterface::DEFAULT_COLLECTION;
}
$syncStorage = $this->importTransformer
->transform($this->syncStorage);
$diff = $this->configManager
->diff($this->targetStorage, $syncStorage, $source_name, $target_name, $collection);
$this->diffFormatter->show_header = FALSE;
$build = [];
$build['#title'] = t('View changes of @config_file', [
'@config_file' => $source_name,
]);
$build['#attached']['library'][] = 'system/diff';
$build['diff'] = [
'#type' => 'table',
'#attributes' => [
'class' => [
'diff',
],
],
'#header' => [
[
'data' => t('Active'),
'colspan' => '2',
],
[
'data' => t('Staged'),
'colspan' => '2',
],
],
'#rows' => $this->diffFormatter
->format($diff),
];
$build['back'] = [
'#type' => 'link',
'#attributes' => [
'class' => [
'dialog-cancel',
],
],
'#title' => "Back to 'Synchronize configuration' page.",
'#url' => Url::fromRoute('config.sync'),
];
return $build;
}
}