Version 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                 '',
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         $directory = $input->getOption('directory');
88         $file = $input->getOption('file');
89
90         if (!$file && !$directory) {
91             $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
92         }
93
94         $ymlFile = new Parser();
95         try {
96             $source_storage = new StorageReplaceDataWrapper(
97                 $this->configStorage
98             );
99
100             foreach ($name as $nameItem) {
101                 // Allow for accidental .yml extension.
102                 if (substr($nameItem, -4) === '.yml') {
103                     $nameItem = substr($nameItem, 0, -4);
104                 }
105
106                 $configFile = $directory.DIRECTORY_SEPARATOR.$nameItem.'.yml';
107                 if (file_exists($configFile)) {
108                     $value = $ymlFile->parse(file_get_contents($configFile));
109                     $source_storage->replaceData($nameItem, $value);
110                     continue;
111                 }
112
113                 $io->error($this->trans('commands.config.import.single.messages.empty-value'));
114                 return 1;
115             }
116
117             $storageComparer = new StorageComparer(
118                 $source_storage,
119                 $this->configStorage,
120                 $this->configManager
121             );
122
123             if ($this->configImport($io, $storageComparer)) {
124                 $io->success(
125                     sprintf(
126                         $this->trans(
127                             'commands.config.import.single.messages.success'
128                         ),
129                         implode(", ", $name)
130                     )
131                 );
132             }
133         } catch (\Exception $e) {
134             $io->error($e->getMessage());
135
136             return 1;
137         }
138     }
139
140     private function configImport($io, StorageComparer $storageComparer)
141     {
142         $configImporter = new ConfigImporter(
143             $storageComparer,
144             \Drupal::service('event_dispatcher'),
145             \Drupal::service('config.manager'),
146             \Drupal::lock(),
147             \Drupal::service('config.typed'),
148             \Drupal::moduleHandler(),
149             \Drupal::service('module_installer'),
150             \Drupal::service('theme_handler'),
151             \Drupal::service('string_translation')
152         );
153
154         if ($configImporter->alreadyImporting()) {
155             $io->success($this->trans('commands.config.import.messages.already-imported'));
156         } else {
157             try {
158                 if ($configImporter->validate()) {
159                     $sync_steps = $configImporter->initialize();
160
161                     foreach ($sync_steps as $step) {
162                         $context = [];
163                         do {
164                             $configImporter->doSyncStep($step, $context);
165                         } while ($context['finished'] < 1);
166                     }
167
168                     return true;
169                 }
170             } catch (ConfigImporterException $e) {
171                 $message = 'The import failed due for the following reasons:' . "\n";
172                 $message .= implode("\n", $configImporter->getErrors());
173                 $io->error(
174                     sprintf(
175                         $this->trans('commands.site.import.local.messages.error-writing'),
176                         $message
177                     )
178                 );
179             } catch (\Exception $e) {
180                 $io->error(
181                     sprintf(
182                         $this->trans('commands.site.import.local.messages.error-writing'),
183                         $e->getMessage()
184                     )
185                 );
186             }
187         }
188     }
189
190     /**
191      * {@inheritdoc}
192      */
193     protected function interact(InputInterface $input, OutputInterface $output)
194     {
195         $io = new DrupalStyle($input, $output);
196         $name = $input->getOption('name');
197         $file = $input->getOption('file');
198         $directory = $input->getOption('directory');
199
200         if (!$name) {
201             $name = $io->ask(
202                 $this->trans('commands.config.import.single.questions.name')
203             );
204             $input->setOption('name', $name);
205         }
206
207         if (!$directory && !$file) {
208             $file = $io->askEmpty(
209                 $this->trans('commands.config.import.single.questions.file')
210             );
211             $input->setOption('file', $file);
212         }
213
214
215         if (!$file && !$directory) {
216             $directory = $io->askEmpty(
217                 $this->trans('commands.config.import.single.questions.directory')
218             );
219             $input->setOption('directory', $directory);
220         }
221     }
222 }