View source
<?php
namespace Drupal\config_split\Controller;
use Drupal\config_split\ConfigSplitManager;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Diff\DiffFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ConfigSplitDiffController implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $activeStorage;
protected $entityTypeManager;
protected $configManager;
protected $diffFormatter;
protected $configSplitManager;
public function __construct(StorageInterface $activeStorage, EntityTypeManagerInterface $entityTypeManager, ConfigManagerInterface $configManager, DiffFormatter $diffFormatter, ConfigSplitManager $configSplitManager) {
$this->activeStorage = $activeStorage;
$this->entityTypeManager = $entityTypeManager;
$this->configManager = $configManager;
$this->diffFormatter = $diffFormatter;
$this->configSplitManager = $configSplitManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.storage'), $container
->get('entity_type.manager'), $container
->get('config.manager'), $container
->get('diff.formatter'), $container
->get('config_split.manager'));
}
public function diff($config_split, $operation, $source_name, $target_name = NULL, $collection = NULL) {
$split = $this->configSplitManager
->getSplitConfig($config_split);
if ($split === NULL) {
return $this
->redirect('system.404');
}
$headers = [
'existing' => $this
->t('Active'),
'new' => $this
->t('Staged'),
];
switch ($operation) {
case 'deactivate':
$source_storage = $this->activeStorage;
$target_storage = $this->configSplitManager
->singleDeactivate($split, FALSE);
break;
case 'activate':
$source_storage = $this->activeStorage;
$target_storage = $this->configSplitManager
->singleActivate($split, !$split
->get('status'));
break;
case 'import':
$source_storage = $this->activeStorage;
$target_storage = $this->configSplitManager
->singleImport($split, !$split
->get('status'));
break;
case 'export':
$source_storage = $this->configSplitManager
->singleExportTarget($split);
$target_storage = $this->configSplitManager
->singleExportPreview($split);
$headers = [
'existing' => $this
->t('Existing'),
'new' => $this
->t('New'),
];
break;
default:
return $this
->redirect('system.404');
}
if (!isset($collection)) {
$collection = StorageInterface::DEFAULT_COLLECTION;
}
$diff = $this->configManager
->diff($source_storage, $target_storage, $source_name, $target_name, $collection);
$this->diffFormatter->show_header = FALSE;
$build = [];
$build['#title'] = $this
->t('View changes of @config_file', [
'@config_file' => $source_name,
]);
$build['#attached']['library'][] = 'system/diff';
$build['diff'] = [
'#type' => 'table',
'#attributes' => [
'class' => [
'diff',
],
],
'#header' => [
[
'data' => $headers['existing'],
'colspan' => '2',
],
[
'data' => $headers['new'],
'colspan' => '2',
],
],
'#rows' => $this->diffFormatter
->format($diff),
];
$build['back'] = [
'#type' => 'link',
'#attributes' => [
'class' => [
'dialog-cancel',
],
],
'#title' => $this
->t("Back to overview."),
'#url' => Url::fromRoute('config.sync'),
];
return $build;
}
protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
$options['absolute'] = TRUE;
return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)
->toString(), $status);
}
}