ConfigAnalyzer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Troubleshooting;
  7. use Magento\Mtf\Config\DataInterface;
  8. use Magento\Mtf\ObjectManagerInterface;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. /**
  12. * Checks if config.xml is configured properly.
  13. */
  14. class ConfigAnalyzer extends \Symfony\Component\Console\Command\Command
  15. {
  16. /**
  17. * Config file path.
  18. *
  19. * @var string
  20. */
  21. private $configFilePath = MTF_BP . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'config.xml';
  22. /**
  23. * Object manager instance.
  24. *
  25. * @var ObjectManagerInterface
  26. */
  27. private $objectManager;
  28. /**
  29. * Config.xml file data.
  30. *
  31. * @var DataInterface
  32. */
  33. private $configXml;
  34. /**
  35. * Config.xml.dist file data.
  36. *
  37. * @var DataInterface
  38. */
  39. private $configXmlDist;
  40. /**
  41. * @param ObjectManagerInterface $objectManager
  42. * @param DataInterface $configXml
  43. * @param DataInterface $configXmlDist
  44. */
  45. public function __construct(
  46. ObjectManagerInterface $objectManager,
  47. DataInterface $configXml,
  48. DataInterface $configXmlDist
  49. ) {
  50. parent::__construct();
  51. $this->objectManager = $objectManager;
  52. $this->configXml = $configXml->get();
  53. $this->configXmlDist = $configXmlDist->get();
  54. }
  55. /**
  56. * Configure command.
  57. *
  58. * @return void
  59. */
  60. protected function configure()
  61. {
  62. parent::configure();
  63. $this->setName('troubleshooting:check-config-valid')
  64. ->setDescription('Check if config.xml is configured properly.');
  65. }
  66. /**
  67. * Execute check if config.xml is configured properly.
  68. *
  69. * @param InputInterface $input
  70. * @param OutputInterface $output
  71. * @return void
  72. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  73. */
  74. protected function execute(InputInterface $input, OutputInterface $output)
  75. {
  76. $output = $this->objectManager->create(
  77. \Magento\Mtf\Console\Output::class,
  78. ['output' => $output]
  79. );
  80. $output->writeln("Checking config.xml file configuration...");
  81. $output->outputMessages($this->checkConfigFileAvailable());
  82. $output->writeln("config.xml file check is finished.");
  83. }
  84. /**
  85. * Check if config.xml file is present in MTF_BP/etc folder.
  86. *
  87. * @return array
  88. */
  89. private function checkConfigFileAvailable()
  90. {
  91. $messages = [];
  92. $configFileExists = false;
  93. if (file_exists($this->configFilePath)) {
  94. $configFileExists = true;
  95. if ($this->recursiveKeys($this->configXml) != $this->recursiveKeys($this->configXmlDist)) {
  96. $messages['error'][] = 'Check your config.xml file to contain all configs from config.xml.dist.';
  97. }
  98. } else {
  99. if (file_exists($this->configFilePath . '.dist')) {
  100. if (!copy($this->configFilePath . '.dist', $this->configFilePath)) {
  101. $messages['error'][] = 'Failed to copy config.xml.dist to config.xml.';
  102. return $messages;
  103. }
  104. $messages['info'][] = 'config.xml file has been created based on config.xml.dist.';
  105. $configFileExists = true;
  106. }
  107. }
  108. if (!$configFileExists) {
  109. $messages['error'][] = 'Cannot define config.xml configuration path.';
  110. }
  111. return $messages;
  112. }
  113. /**
  114. * Get array of array keys.
  115. *
  116. * @param array $input
  117. * @return array
  118. */
  119. private function recursiveKeys(array $input)
  120. {
  121. $output = array_keys($input);
  122. foreach ($input as $sub) {
  123. if (is_array($sub)) {
  124. $output = array_merge($output, $this->recursiveKeys($sub));
  125. }
  126. }
  127. return $output;
  128. }
  129. }