db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Command / Config / DiffCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Config\DiffCommand.
5  */
6
7 namespace Drupal\Console\Command\Config;
8
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;
20
21 class DiffCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26      * @var CachedStorage
27      */
28     protected $configStorage;
29
30     /**
31      * @var ConfigManager
32      */
33     protected $configManager;
34
35     /**
36      * DiffCommand constructor.
37      *
38      * @param CachedStorage $configStorage
39      * @param ConfigManager $configManager
40      */
41     public function __construct(
42         CachedStorage $configStorage,
43         ConfigManager $configManager
44     ) {
45         $this->configStorage = $configStorage;
46         $this->configManager = $configManager;
47         parent::__construct();
48     }
49
50     /**
51      * A static array map of operations -> color strings.
52      *
53      * @see http://symfony.com/doc/current/components/console/introduction.html#coloring-the-output
54      *
55      * @var array
56      */
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>',
61         'default' => '%s',
62     ];
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function configure()
68     {
69         $this
70             ->setName('config:diff')
71             ->setDescription($this->trans('commands.config.diff.description'))
72             ->addArgument(
73                 'directory',
74                 InputArgument::OPTIONAL,
75                 $this->trans('commands.config.diff.arguments.directory')
76             )
77             ->addOption(
78                 'reverse',
79                 null,
80                 InputOption::VALUE_NONE,
81                 $this->trans('commands.config.diff.options.reverse')
82             );
83     }
84
85     /**
86      * {@inheritdoc}
87      */
88     protected function interact(InputInterface $input, OutputInterface $output)
89     {
90         global $config_directories;
91         $io = new DrupalStyle($input, $output);
92
93         $directory = $input->getArgument('directory');
94         if (!$directory) {
95             $directory = $io->choice(
96                 $this->trans('commands.config.diff.questions.directories'),
97                 array_keys($config_directories),
98                 CONFIG_SYNC_DIRECTORY
99             );
100
101             $input->setArgument('directory', $config_directories[$directory]);
102         }
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     protected function execute(InputInterface $input, OutputInterface $output)
109     {
110         $io = new DrupalStyle($input, $output);
111         $directory = $input->getArgument('directory');
112         $source_storage = new FileStorage($directory);
113
114         if ($input->getOption('reverse')) {
115             $config_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
116         } else {
117             $config_comparer = new StorageComparer($this->configStorage, $source_storage, $this->configManager);
118         }
119         if (!$config_comparer->createChangelist()->hasChanges()) {
120             $output->writeln($this->trans('commands.config.diff.messages.no-changes'));
121         }
122
123         $change_list = [];
124         foreach ($config_comparer->getAllCollectionNames() as $collection) {
125             $change_list[$collection] = $config_comparer->getChangelist(null, $collection);
126         }
127
128         $this->outputDiffTable($io, $change_list);
129     }
130
131     /**
132      * Ouputs a table of configuration changes.
133      *
134      * @param DrupalStyle $io
135      *   The io.
136      * @param array       $change_list
137      *   The list of changes from the StorageComparer.
138      */
139     protected function outputDiffTable(DrupalStyle $io, array $change_list)
140     {
141         $header = [
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'),
145         ];
146         $rows = [];
147         foreach ($change_list as $collection => $changes) {
148             foreach ($changes as $operation => $configs) {
149                 foreach ($configs as $config) {
150                     $rows[] = [
151                         $collection,
152                         $config,
153                         sprintf(self::$operationColours[$operation], $operation),
154                     ];
155                 }
156             }
157         }
158         $io->table($header, $rows);
159     }
160 }