Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplicationDumpCommand.php
Go to the documentation of this file.
1 <?php
7 
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
18 
22 class ApplicationDumpCommand extends Command
23 {
24  const INPUT_CONFIG_TYPES = 'config-types';
25 
29  private $writer;
30 
34  private $sources;
35 
39  private $configHash;
40 
48  public function __construct(
49  Writer $writer,
50  array $sources,
51  Hash $configHash = null
52  ) {
53  $this->writer = $writer;
54  $this->sources = $sources;
55  $this->configHash = $configHash ?: ObjectManager::getInstance()->get(Hash::class);
56  parent::__construct();
57  }
58 
62  protected function configure()
63  {
64  $this->setName('app:config:dump');
65  $this->setDescription('Create dump of application');
66 
67  $configTypes = array_unique(array_column($this->sources, 'namespace'));
68  $this->addArgument(
69  self::INPUT_CONFIG_TYPES,
70  InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
71  sprintf('Space-separated list of config types or omit to dump all [%s]', implode(', ', $configTypes))
72  );
73  parent::configure();
74  }
75 
84  protected function execute(InputInterface $input, OutputInterface $output)
85  {
86  $this->groupSourcesByPool();
87  $dumpedTypes = [];
88  foreach ($this->sources as $pool => $sources) {
89  $dump = [];
90  $comments = [];
91  foreach ($sources as $sourceData) {
92  if ($this->skipDump($input, $sourceData)) {
93  continue;
94  }
96  $source = $sourceData['source'];
97  $namespace = $sourceData['namespace'];
98  $dump[$namespace] = $source->get();
99  if (!empty($sourceData['comment'])) {
100  $comments[$namespace] = is_string($sourceData['comment'])
101  ? $sourceData['comment']
102  : $sourceData['comment']->get();
103  }
104  }
105  $this->writer->saveConfig(
106  [$pool => $dump],
107  true,
108  null,
109  $comments
110  );
111  $dumpedTypes = array_unique($dumpedTypes + array_keys($dump));
112  if (!empty($comments)) {
113  $output->writeln($comments);
114  }
115  }
116 
117  if (!$dumpedTypes) {
118  $output->writeln('<error>Nothing dumped. Check the config types specified and try again');
119  return Cli::RETURN_FAILURE;
120  }
121 
122  // Generate and save new hash of deployment configuration.
123  $this->configHash->regenerate();
124 
125  $output->writeln(sprintf('<info>Done. Config types dumped: %s</info>', implode(', ', $dumpedTypes)));
126  return Cli::RETURN_SUCCESS;
127  }
128 
136  private function groupSourcesByPool()
137  {
138  $sources = [];
139  foreach ($this->sources as $sourceData) {
140  if (!isset($sourceData['pool'])) {
141  $sourceData['pool'] = ConfigFilePool::APP_CONFIG;
142  }
143 
144  $sources[$sourceData['pool']][] = $sourceData;
145  }
146 
147  $this->sources = $sources;
148  }
149 
157  private function skipDump(InputInterface $input, array $sourceData): bool
158  {
159  $allowedTypes = $input->getArgument(self::INPUT_CONFIG_TYPES);
160  if ($allowedTypes && !in_array($sourceData['namespace'], $allowedTypes)) {
161  return true;
162  }
163  return false;
164  }
165 }
$source
Definition: source.php:23
__construct(Writer $writer, array $sources, Hash $configHash=null)