Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Config / ImportSingleCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\ImportSingleCommand.
6  */
7 namespace Drupal\Console\Command\Config;
8
9 use Drupal\config\StorageReplaceDataWrapper;
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Drupal\Console\Core\Style\DrupalStyle;
12 use Drupal\Core\Config\CachedStorage;
13 use Drupal\Core\Config\ConfigImporter;
14 use Drupal\Core\Config\ConfigImporterException;
15 use Drupal\Core\Config\ConfigManager;
16 use Drupal\Core\Config\StorageComparer;
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Input\InputOption;
21 use Symfony\Component\Console\Output\OutputInterface;
22 use Symfony\Component\Yaml\Parser;
23
24 class ImportSingleCommand extends Command
25 {
26     use CommandTrait;
27
28     /**
29      * @var CachedStorage
30      */
31     protected $configStorage;
32
33     /**
34      * @var ConfigManager
35      */
36     protected $configManager;
37
38     /**
39      * ImportSingleCommand constructor.
40      *
41      * @param CachedStorage $configStorage
42      * @param ConfigManager $configManager
43      */
44     public function __construct(
45         CachedStorage $configStorage,
46         ConfigManager $configManager
47     ) {
48         $this->configStorage = $configStorage;
49         $this->configManager = $configManager;
50         parent::__construct();
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     protected function configure()
57     {
58         $this
59             ->setName('config:import:single')
60             ->setDescription($this->trans('commands.config.import.single.description'))
61             ->addOption(
62                 'name',
63                 null,
64                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
65                 $this->trans('commands.config.import.single.options.name')
66             )->addOption(
67                 'file',
68                 null,
69                 InputOption::VALUE_OPTIONAL,
70                 $this->trans('commands.config.import.single.options.file')
71             )->addOption(
72                 'directory',
73                 null,
74                 InputOption::VALUE_OPTIONAL,
75                 $this->trans('commands.config.import.arguments.directory')
76             );
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function execute(InputInterface $input, OutputInterface $output)
83     {
84         $io = new DrupalStyle($input, $output);
85
86         $name = $input->getOption('name');
87         $name = is_array($name) ? $name : [$name];
88         $directory = $input->getOption('directory');
89         $file = $input->getOption('file');
90
91         if (!$file && !$directory) {
92             $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
93         }
94
95         $ymlFile = new Parser();
96         try {
97             $source_storage = new StorageReplaceDataWrapper(
98                 $this->configStorage
99             );
100
101             foreach ($name as $nameItem) {
102                 // Allow for accidental .yml extension.
103                 if (substr($nameItem, -4) === '.yml') {
104                     $nameItem = substr($nameItem, 0, -4);
105                 }
106
107                 $configFile = count($name) == 1 ?
108                   $file :
109                   $directory.DIRECTORY_SEPARATOR.$nameItem.'.yml';
110
111                 if (file_exists($configFile)) {
112                     $value = $ymlFile->parse(file_get_contents($configFile));
113                     $source_storage->delete($nameItem);
114                     $source_storage->write($nameItem, $value);
115                     continue;
116                 }
117
118                 $io->error($this->trans('commands.config.import.single.messages.empty-value'));
119                 return 1;
120             }
121
122             $storageComparer = new StorageComparer(
123                 $source_storage,
124                 $this->configStorage,
125                 $this->configManager
126             );
127
128
129             if ($this->configImport($io, $storageComparer)) {
130                 $io->success(
131                     sprintf(
132                         $this->trans(
133                             'commands.config.import.single.messages.success'
134                         ),
135                         implode(", ", $name)
136                     )
137                 );
138             }
139         } catch (\Exception $e) {
140             $io->error($e->getMessage());
141
142             return 1;
143         }
144     }
145
146     private function configImport($io, StorageComparer $storageComparer)
147     {
148         $configImporter = new ConfigImporter(
149             $storageComparer,
150             \Drupal::service('event_dispatcher'),
151             \Drupal::service('config.manager'),
152             \Drupal::lock(),
153             \Drupal::service('config.typed'),
154             \Drupal::moduleHandler(),
155             \Drupal::service('module_installer'),
156             \Drupal::service('theme_handler'),
157             \Drupal::service('string_translation')
158         );
159
160         if ($configImporter->alreadyImporting()) {
161             $io->success($this->trans('commands.config.import.messages.already-imported'));
162         } else {
163             try {
164                 if ($configImporter->validate()) {
165                     $sync_steps = $configImporter->initialize();
166
167                     foreach ($sync_steps as $step) {
168                         $context = [];
169                         do {
170                             $configImporter->doSyncStep($step, $context);
171                         } while ($context['finished'] < 1);
172                     }
173
174                     return true;
175                 }
176             } catch (ConfigImporterException $e) {
177                 $message = 'The import failed due to the following reasons:' . "\n";
178                 $message .= implode("\n", $configImporter->getErrors());
179                 $io->error(
180                     sprintf(
181                         $this->trans('commands.site.import.local.messages.error-writing'),
182                         $message
183                     )
184                 );
185             } catch (\Exception $e) {
186                 $io->error(
187                     sprintf(
188                         $this->trans('commands.site.import.local.messages.error-writing'),
189                         $e->getMessage()
190                     )
191                 );
192             }
193         }
194     }
195
196     /**
197      * {@inheritdoc}
198      */
199     protected function interact(InputInterface $input, OutputInterface $output)
200     {
201         $io = new DrupalStyle($input, $output);
202         $name = $input->getOption('name');
203         $file = $input->getOption('file');
204         $directory = $input->getOption('directory');
205
206         if (!$name) {
207             $name = $io->ask(
208                 $this->trans('commands.config.import.single.questions.name')
209             );
210             $input->setOption('name', $name);
211         }
212
213         if (!$directory && !$file) {
214             $file = $io->askEmpty(
215                 $this->trans('commands.config.import.single.questions.file')
216             );
217             $input->setOption('file', $file);
218         }
219
220
221         if (!$file && !$directory) {
222             $directory = $io->askEmpty(
223                 $this->trans('commands.config.import.single.questions.directory')
224             );
225             $input->setOption('directory', $directory);
226         }
227     }
228 }