Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Config / ImportCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Config\ImportCommand.
5  */
6
7 namespace Drupal\Console\Command\Config;
8
9 use Symfony\Component\Console\Input\InputOption;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Drupal\Console\Core\Command\Command;
13 use Drupal\Core\Config\CachedStorage;
14 use Drupal\Core\Config\ConfigManager;
15 use Drupal\Core\Config\ConfigImporterException;
16 use Drupal\Core\Config\ConfigImporter;
17 use Drupal\Core\Config\FileStorage;
18 use Drupal\Core\Config\StorageComparerInterface;
19
20 class ImportCommand extends Command
21 {
22     /**
23      * @var CachedStorage
24      */
25     protected $configStorage;
26
27     /**
28      * @var ConfigManager
29      */
30     protected $configManager;
31
32     /**
33      * ImportCommand constructor.
34      *
35      * @param CachedStorage $configStorage
36      * @param ConfigManager $configManager
37      */
38     public function __construct(
39         CachedStorage $configStorage,
40         ConfigManager $configManager
41     ) {
42         $this->configStorage = $configStorage;
43         $this->configManager = $configManager;
44         parent::__construct();
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     protected function configure()
51     {
52         $this
53             ->setName('config:import')
54             ->setDescription($this->trans('commands.config.import.description'))
55             ->addOption(
56                 'file',
57                 null,
58                 InputOption::VALUE_OPTIONAL,
59                 $this->trans('commands.config.import.options.file')
60             )
61             ->addOption(
62                 'directory',
63                 null,
64                 InputOption::VALUE_OPTIONAL,
65                 $this->trans('commands.config.import.options.directory')
66             )
67             ->addOption(
68                 'remove-files',
69                 null,
70                 InputOption::VALUE_NONE,
71                 $this->trans('commands.config.import.options.remove-files')
72             )
73             ->addOption(
74                 'skip-uuid',
75                 null,
76                 InputOption::VALUE_NONE,
77                 $this->trans('commands.config.import.options.skip-uuid')
78             )
79             ->setAliases(['ci']);
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function execute(InputInterface $input, OutputInterface $output)
86     {
87         $directory = $input->getOption('directory');
88         $skipUuid = $input->getOption('skip-uuid');
89
90         if ($directory) {
91             $configSyncDir = $directory;
92         } else {
93             $configSyncDir = config_get_config_directory(
94                 CONFIG_SYNC_DIRECTORY
95             );
96         }
97
98         $source_storage = new FileStorage($configSyncDir);
99
100         $storageComparer = '\Drupal\Core\Config\StorageComparer';
101         if ($skipUuid) {
102             $storageComparer = '\Drupal\Console\Override\StorageComparer';
103         }
104
105         $storage_comparer = new $storageComparer(
106             $source_storage,
107             $this->configStorage,
108             $this->configManager
109         );
110
111         if (!$storage_comparer->createChangelist()->hasChanges()) {
112             $this->getIo()->success($this->trans('commands.config.import.messages.nothing-to-do'));
113         }
114
115         if ($this->configImport($storage_comparer)) {
116             $this->getIo()->success($this->trans('commands.config.import.messages.imported'));
117         } else {
118             return 1;
119         }
120     }
121
122
123     private function configImport(StorageComparerInterface $storage_comparer)
124     {
125         $config_importer = new ConfigImporter(
126             $storage_comparer,
127             \Drupal::service('event_dispatcher'),
128             \Drupal::service('config.manager'),
129             \Drupal::lock(),
130             \Drupal::service('config.typed'),
131             \Drupal::moduleHandler(),
132             \Drupal::service('module_installer'),
133             \Drupal::service('theme_handler'),
134             \Drupal::service('string_translation')
135         );
136
137         if ($config_importer->alreadyImporting()) {
138             $this->getIo()->success($this->trans('commands.config.import.messages.already-imported'));
139         } else {
140             try {
141                 $this->getIo()->info($this->trans('commands.config.import.messages.importing'));
142                 $config_importer->import();
143                 return true;
144             } catch (ConfigImporterException $e) {
145                 $this->getIo()->error(
146                     sprintf(
147                         $this->trans('commands.site.import.local.messages.error-writing'),
148                         implode("\n", $config_importer->getErrors())
149                     )
150                 );
151             } catch (\Exception $e) {
152                 $this->getIo()->error(
153                     sprintf(
154                         $this->trans('commands.site.import.local.messages.error-writing'),
155                         $e->getMessage()
156                     )
157                 );
158             }
159         }
160     }
161 }