View source
<?php
namespace Drupal\content_sync\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Diff\DiffFormatter;
use Drupal\Core\Url;
use Drupal\Core\File\FileSystemInterface;
class ContentController implements ContainerInjectionInterface {
protected $targetStorage;
protected $sourceStorage;
protected $contentManager;
protected $fileDownloadController;
protected $diffFormatter;
public static function create(ContainerInterface $container) {
return new static($container
->get('content.storage'), $container
->get('content.storage.sync'), $container
->get('config.manager'), $container
->get('diff.formatter'), $container
->get('file_system'));
}
public function __construct(StorageInterface $target_storage, StorageInterface $source_storage, ConfigManagerInterface $content_manager, DiffFormatter $diff_formatter, FileSystemInterface $file_system) {
$this->targetStorage = $target_storage;
$this->sourceStorage = $source_storage;
$this->contentManager = $content_manager;
$this->diffFormatter = $diff_formatter;
$this->fileSystem = $file_system;
}
public function downloadExport() {
$filename = 'content.tar.gz';
$file_path = $this->fileSystem
->getTempDirectory() . '/' . $filename;
if (file_exists($file_path)) {
unset($_SESSION['content_tar_download_file']);
$mime = \Drupal::service('file.mime_type.guesser')
->guess($file_path);
$headers = array(
'Content-Type' => $mime . '; name="' . Unicode::mimeHeaderEncode(basename($file_path)) . '"',
'Content-Length' => filesize($file_path),
'Content-Disposition' => 'attachment; filename="' . Unicode::mimeHeaderEncode($filename) . '"',
'Cache-Control' => 'private',
);
return new BinaryFileResponse($file_path, 200, $headers);
}
return -1;
}
public function diff($source_name, $target_name = NULL, $collection = NULL) {
if (!isset($collection)) {
$collection = StorageInterface::DEFAULT_COLLECTION;
}
$diff = $this->contentManager
->diff($this->targetStorage, $this->sourceStorage, $source_name, $target_name, $collection);
$this->diffFormatter->show_header = FALSE;
$build = [];
$build['#title'] = t('View changes of @content_file', [
'@content_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 content' page.",
'#url' => Url::fromRoute('content.sync'),
];
return $build;
}
}