Yaffs site version 1.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 Drupal\Core\Config\ConfigManagerInterface;
13 use Drupal\Core\Config\StorageInterface;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Style\DrupalStyle;
21 use Drupal\Core\Config\ConfigManager;
22
23 class ExportCommand extends Command
24 {
25     use CommandTrait;
26
27     /**
28      * @var ConfigManager
29      */
30     protected $configManager;
31
32     /**
33      * @var StorageInterface
34      */
35     protected $storage;
36
37     /**
38      * ExportCommand constructor.
39      *
40      * @param ConfigManagerInterface $configManager
41      * @param StorageInterface       $storage
42      */
43     public function __construct(ConfigManagerInterface $configManager, StorageInterface $storage)
44     {
45         parent::__construct();
46         $this->configManager = $configManager;
47         $this->storage = $storage;
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function configure()
54     {
55         $this
56             ->setName('config:export')
57             ->setDescription($this->trans('commands.config.export.description'))
58             ->addOption(
59                 'directory',
60                 null,
61                 InputOption::VALUE_OPTIONAL,
62                 $this->trans('commands.config.export.arguments.directory')
63             )
64             ->addOption(
65                 'tar',
66                 null,
67                 InputOption::VALUE_NONE,
68                 $this->trans('commands.config.export.arguments.tar')
69             )->addOption(
70                 'remove-uuid',
71                 null,
72                 InputOption::VALUE_NONE,
73                 $this->trans('commands.config.export.single.options.remove-uuid')
74             )->addOption(
75                 'remove-config-hash',
76                 null,
77                 InputOption::VALUE_NONE,
78                 $this->trans('commands.config.export.single.options.remove-config-hash')
79             );
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function execute(InputInterface $input, OutputInterface $output)
86     {
87         $io = new DrupalStyle($input, $output);
88
89         $directory = $input->getOption('directory');
90         $tar = $input->getOption('tar');
91         $removeUuid = $input->getOption('remove-uuid');
92         $removeHash = $input->getOption('remove-config-hash');
93         
94         if (!$directory) {
95             $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
96         }
97
98         $fileSystem = new Filesystem();
99         try {
100             $fileSystem->mkdir($directory);
101         } catch (IOExceptionInterface $e) {
102             $io->error(
103                 sprintf(
104                     $this->trans('commands.config.export.messages.error'),
105                     $e->getPath()
106                 )
107             );
108         }
109
110         // Remove previous yaml files before creating new ones
111         array_map('unlink', glob($directory . '/*'));
112
113         if ($tar) {
114             $dateTime = new \DateTime();
115
116             $archiveFile = sprintf(
117                 '%s/config-%s.tar.gz',
118                 $directory,
119                 $dateTime->format('Y-m-d-H-i-s')
120             );
121             $archiveTar = new ArchiveTar($archiveFile, 'gz');
122         }
123
124         try {
125             // Get raw configuration data without overrides.
126             foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
127                 $configName = "$name.yml";
128                 $configData = $this->configManager->getConfigFactory()->get($name)->getRawData();
129                 if ($removeUuid) {
130                     unset($configData['uuid']);
131                 }
132                 if ($removeHash) {
133                     unset($configData['_core']['default_config_hash']);
134                 }
135                 $ymlData = Yaml::encode($configData);
136
137                 if ($tar) {
138                     $archiveTar->addString($configName, $ymlData);
139                 } else {
140                     file_put_contents("$directory/$configName", $ymlData);
141                 }
142             }
143             // Get all override data from the remaining collections.
144             foreach ($this->storage->getAllCollectionNames() as $collection) {
145                 $collection_storage = $this->storage->createCollection($collection);
146                 foreach ($collection_storage->listAll() as $name) {
147                     $configName = str_replace('.', '/', $collection) . "/$name.yml";
148                     $configData = $collection_storage->read($name);
149                     if ($removeUuid) {
150                         unset($configData['uuid']);
151                     }
152                     if ($removeHash) {
153                         unset($configData['_core']['default_config_hash']);
154                     }
155
156                     $ymlData = Yaml::encode($configData);
157                     if ($tar) {
158                         $archiveTar->addString($configName, $ymlData);
159                     } else {
160                         file_put_contents("$directory/$configName", $ymlData);
161                     }
162                 }
163             }
164         } catch (\Exception $e) {
165             $io->error($e->getMessage());
166         }
167
168         $io->info(
169             sprintf(
170                 $this->trans('commands.config.export.messages.directory'),
171                 $directory
172             )
173         );
174     }
175 }