4 * Contains \Drupal\Console\Command\Config\DiffCommand.
7 namespace Drupal\Console\Command\Config;
9 use Drupal\Core\Config\FileStorage;
10 use Drupal\Core\Config\StorageComparer;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Core\Config\CachedStorage;
17 use Drupal\Core\Config\ConfigManager;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\Console\Core\Style\DrupalStyle;
21 class DiffCommand extends Command
28 protected $configStorage;
33 protected $configManager;
36 * DiffCommand constructor.
38 * @param CachedStorage $configStorage
39 * @param ConfigManager $configManager
41 public function __construct(
42 CachedStorage $configStorage,
43 ConfigManager $configManager
45 $this->configStorage = $configStorage;
46 $this->configManager = $configManager;
47 parent::__construct();
51 * A static array map of operations -> color strings.
53 * @see http://symfony.com/doc/current/components/console/introduction.html#coloring-the-output
57 protected static $operationColours = [
58 'delete' => '<fg=red>%s</fg=red>',
59 'update' => '<fg=yellow>%s</fg=yellow>',
60 'create' => '<fg=green>%s</fg=green>',
67 protected function configure()
70 ->setName('config:diff')
71 ->setDescription($this->trans('commands.config.diff.description'))
74 InputArgument::OPTIONAL,
75 $this->trans('commands.config.diff.arguments.directory')
80 InputOption::VALUE_NONE,
81 $this->trans('commands.config.diff.options.reverse')
88 protected function interact(InputInterface $input, OutputInterface $output)
90 global $config_directories;
91 $io = new DrupalStyle($input, $output);
93 $directory = $input->getArgument('directory');
95 $directory = $io->choice(
96 $this->trans('commands.config.diff.questions.directories'),
97 array_keys($config_directories),
101 $input->setArgument('directory', $config_directories[$directory]);
108 protected function execute(InputInterface $input, OutputInterface $output)
110 $io = new DrupalStyle($input, $output);
111 $directory = $input->getArgument('directory');
112 $source_storage = new FileStorage($directory);
114 if ($input->getOption('reverse')) {
115 $config_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
117 $config_comparer = new StorageComparer($this->configStorage, $source_storage, $this->configManager);
119 if (!$config_comparer->createChangelist()->hasChanges()) {
120 $output->writeln($this->trans('commands.config.diff.messages.no-changes'));
124 foreach ($config_comparer->getAllCollectionNames() as $collection) {
125 $change_list[$collection] = $config_comparer->getChangelist(null, $collection);
128 $this->outputDiffTable($io, $change_list);
132 * Ouputs a table of configuration changes.
134 * @param DrupalStyle $io
136 * @param array $change_list
137 * The list of changes from the StorageComparer.
139 protected function outputDiffTable(DrupalStyle $io, array $change_list)
142 $this->trans('commands.config.diff.table.headers.collection'),
143 $this->trans('commands.config.diff.table.headers.config-name'),
144 $this->trans('commands.config.diff.table.headers.operation'),
147 foreach ($change_list as $collection => $changes) {
148 foreach ($changes as $operation => $configs) {
149 foreach ($configs as $config) {
153 sprintf(self::$operationColours[$operation], $operation),
158 $io->table($header, $rows);