ApplicationDumpCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console\Command\App;
  7. use Magento\Deploy\Model\DeploymentConfig\Hash;
  8. use Magento\Framework\App\Config\ConfigSourceInterface;
  9. use Magento\Framework\App\DeploymentConfig\Writer;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\Console\Cli;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * Command for dump application state
  19. */
  20. class ApplicationDumpCommand extends Command
  21. {
  22. const INPUT_CONFIG_TYPES = 'config-types';
  23. /**
  24. * @var Writer
  25. */
  26. private $writer;
  27. /**
  28. * @var ConfigSourceInterface[]
  29. */
  30. private $sources;
  31. /**
  32. * @var Hash
  33. */
  34. private $configHash;
  35. /**
  36. * ApplicationDumpCommand constructor
  37. *
  38. * @param Writer $writer
  39. * @param array $sources
  40. * @param Hash $configHash
  41. */
  42. public function __construct(
  43. Writer $writer,
  44. array $sources,
  45. Hash $configHash = null
  46. ) {
  47. $this->writer = $writer;
  48. $this->sources = $sources;
  49. $this->configHash = $configHash ?: ObjectManager::getInstance()->get(Hash::class);
  50. parent::__construct();
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function configure()
  56. {
  57. $this->setName('app:config:dump');
  58. $this->setDescription('Create dump of application');
  59. $configTypes = array_unique(array_column($this->sources, 'namespace'));
  60. $this->addArgument(
  61. self::INPUT_CONFIG_TYPES,
  62. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  63. sprintf('Space-separated list of config types or omit to dump all [%s]', implode(', ', $configTypes))
  64. );
  65. parent::configure();
  66. }
  67. /**
  68. * Dump Application
  69. *
  70. * @param InputInterface $input
  71. * @param OutputInterface $output
  72. * @return boolean
  73. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  74. */
  75. protected function execute(InputInterface $input, OutputInterface $output)
  76. {
  77. $this->groupSourcesByPool();
  78. $dumpedTypes = [];
  79. foreach ($this->sources as $pool => $sources) {
  80. $dump = [];
  81. $comments = [];
  82. foreach ($sources as $sourceData) {
  83. if ($this->skipDump($input, $sourceData)) {
  84. continue;
  85. }
  86. /** @var ConfigSourceInterface $source */
  87. $source = $sourceData['source'];
  88. $namespace = $sourceData['namespace'];
  89. $dump[$namespace] = $source->get();
  90. if (!empty($sourceData['comment'])) {
  91. $comments[$namespace] = is_string($sourceData['comment'])
  92. ? $sourceData['comment']
  93. : $sourceData['comment']->get();
  94. }
  95. }
  96. $this->writer->saveConfig(
  97. [$pool => $dump],
  98. true,
  99. null,
  100. $comments
  101. );
  102. $dumpedTypes = array_unique($dumpedTypes + array_keys($dump));
  103. if (!empty($comments)) {
  104. $output->writeln($comments);
  105. }
  106. }
  107. if (!$dumpedTypes) {
  108. $output->writeln('<error>Nothing dumped. Check the config types specified and try again');
  109. return Cli::RETURN_FAILURE;
  110. }
  111. // Generate and save new hash of deployment configuration.
  112. $this->configHash->regenerate();
  113. $output->writeln(sprintf('<info>Done. Config types dumped: %s</info>', implode(', ', $dumpedTypes)));
  114. return Cli::RETURN_SUCCESS;
  115. }
  116. /**
  117. * Groups sources by theirs pool.
  118. *
  119. * If source doesn't have pool option puts him into APP_CONFIG pool.
  120. *
  121. * @return void
  122. */
  123. private function groupSourcesByPool()
  124. {
  125. $sources = [];
  126. foreach ($this->sources as $sourceData) {
  127. if (!isset($sourceData['pool'])) {
  128. $sourceData['pool'] = ConfigFilePool::APP_CONFIG;
  129. }
  130. $sources[$sourceData['pool']][] = $sourceData;
  131. }
  132. $this->sources = $sources;
  133. }
  134. /**
  135. * Check whether the dump source should be skipped
  136. *
  137. * @param InputInterface $input
  138. * @param array $sourceData
  139. * @return bool
  140. */
  141. private function skipDump(InputInterface $input, array $sourceData): bool
  142. {
  143. $allowedTypes = $input->getArgument(self::INPUT_CONFIG_TYPES);
  144. if ($allowedTypes && !in_array($sourceData['namespace'], $allowedTypes)) {
  145. return true;
  146. }
  147. return false;
  148. }
  149. }