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