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