Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Config / ExportCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\ExportCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
9
10 use Drupal\Core\Archiver\ArchiveTar;
11 use Drupal\Component\Serialization\Yaml;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Core\Config\ConfigManager;
20
21 class ExportCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26      * @var ConfigManager
27      */
28     protected $configManager;
29
30     /**
31      * ExportCommand constructor.
32      *
33      * @param ConfigManager $configManager
34      */
35     public function __construct(ConfigManager $configManager)
36     {
37         $this->configManager = $configManager;
38         parent::__construct();
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function configure()
45     {
46         $this
47             ->setName('config:export')
48             ->setDescription($this->trans('commands.config.export.description'))
49             ->addOption(
50                 'directory',
51                 null,
52                 InputOption::VALUE_OPTIONAL,
53                 $this->trans('commands.config.export.arguments.directory')
54             )
55             ->addOption(
56                 'tar',
57                 false,
58                 InputOption::VALUE_NONE,
59                 $this->trans('commands.config.export.arguments.tar')
60             )->addOption(
61                 'remove-uuid',
62                 '',
63                 InputOption::VALUE_NONE,
64                 $this->trans('commands.config.export.single.options.remove-uuid')
65             )->addOption(
66                 'remove-config-hash',
67                 '',
68                 InputOption::VALUE_NONE,
69                 $this->trans('commands.config.export.single.options.remove-config-hash')
70             );
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     protected function execute(InputInterface $input, OutputInterface $output)
77     {
78         $io = new DrupalStyle($input, $output);
79
80         $directory = $input->getOption('directory');
81         $tar = $input->getOption('tar');
82         $removeUuid = $input->getOption('remove-uuid');
83         $removeHash = $input->getOption('remove-config-hash');
84         
85         if (!$directory) {
86             $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
87         }
88
89         $fileSystem = new Filesystem();
90         try {
91             $fileSystem->mkdir($directory);
92         } catch (IOExceptionInterface $e) {
93             $io->error(
94                 sprintf(
95                     $this->trans('commands.config.export.messages.error'),
96                     $e->getPath()
97                 )
98             );
99         }
100
101         if ($tar) {
102             $dateTime = new \DateTime();
103
104             $archiveFile = sprintf(
105                 '%s/config-%s.tar.gz',
106                 $directory,
107                 $dateTime->format('Y-m-d-H-i-s')
108             );
109             $archiveTar = new ArchiveTar($archiveFile, 'gz');
110         }
111
112         try {
113             // Get raw configuration data without overrides.
114             foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
115                 $configData = $this->configManager->getConfigFactory()->get($name)->getRawData();
116                 $configName =  sprintf('%s.yml', $name);
117
118                 if ($removeUuid) {
119                     unset($configData['uuid']);
120                 }
121                 
122                 if ($removeHash) {
123                     unset($configData['_core']['default_config_hash']);
124                 }
125
126                 $ymlData = Yaml::encode($configData);
127
128                 if ($tar) {
129                     $archiveTar->addString(
130                         $configName,
131                         $ymlData
132                     );
133                     continue;
134                 }
135
136                 $configFileName =  sprintf('%s/%s', $directory, $configName);
137
138                 file_put_contents($configFileName, $ymlData);
139             }
140         } catch (\Exception $e) {
141             $io->error($e->getMessage());
142         }
143
144         $io->info(
145             sprintf(
146                 $this->trans('commands.config.export.messages.directory'),
147                 $directory
148             )
149         );
150     }
151 }