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