View source
<?php
namespace Drupal\config\Controller;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Diff\DiffFormatter;
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 $sourceStorage;
protected $configManager;
protected $fileDownloadController;
protected $diffFormatter;
public static function create(ContainerInterface $container) {
return new static($container
->get('config.storage'), $container
->get('config.storage.sync'), $container
->get('config.manager'), new FileDownloadController(), $container
->get('diff.formatter'));
}
public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ConfigManagerInterface $config_manager, FileDownloadController $file_download_controller, DiffFormatter $diff_formatter) {
$this->targetStorage = $target_storage;
$this->sourceStorage = $source_storage;
$this->configManager = $config_manager;
$this->fileDownloadController = $file_download_controller;
$this->diffFormatter = $diff_formatter;
}
public function downloadExport() {
file_unmanaged_delete(file_directory_temp() . '/config.tar.gz');
$archiver = new ArchiveTar(file_directory_temp() . '/config.tar.gz', 'gz');
foreach ($this->configManager
->getConfigFactory()
->listAll() as $name) {
$archiver
->addString("{$name}.yml", Yaml::encode($this->configManager
->getConfigFactory()
->get($name)
->getRawData()));
}
foreach ($this->targetStorage
->getAllCollectionNames() as $collection) {
$collection_storage = $this->targetStorage
->createCollection($collection);
foreach ($collection_storage
->listAll() as $name) {
$archiver
->addString(str_replace('.', '/', $collection) . "/{$name}.yml", Yaml::encode($collection_storage
->read($name)));
}
}
$request = new Request(array(
'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;
}
$diff = $this->configManager
->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
$this->diffFormatter->show_header = FALSE;
$build = array();
$build['#title'] = t('View changes of @config_file', array(
'@config_file' => $source_name,
));
$build['#attached']['library'][] = 'system/diff';
$build['diff'] = array(
'#type' => 'table',
'#header' => array(
array(
'data' => t('Active'),
'colspan' => '2',
),
array(
'data' => t('Staged'),
'colspan' => '2',
),
),
'#rows' => $this->diffFormatter
->format($diff),
);
$build['back'] = array(
'#type' => 'link',
'#attributes' => array(
'class' => array(
'dialog-cancel',
),
),
'#title' => "Back to 'Synchronize configuration' page.",
'#url' => Url::fromRoute('config.sync'),
);
return $build;
}
}