Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigAnalyzer.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Config\DataInterface;
10 use Magento\Mtf\ObjectManagerInterface;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 
17 class ConfigAnalyzer extends \Symfony\Component\Console\Command\Command
18 {
24  private $configFilePath = MTF_BP . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'config.xml';
25 
31  private $objectManager;
32 
38  private $configXml;
39 
45  private $configXmlDist;
46 
52  public function __construct(
53  ObjectManagerInterface $objectManager,
54  DataInterface $configXml,
55  DataInterface $configXmlDist
56  ) {
57  parent::__construct();
58  $this->objectManager = $objectManager;
59  $this->configXml = $configXml->get();
60  $this->configXmlDist = $configXmlDist->get();
61  }
62 
68  protected function configure()
69  {
70  parent::configure();
71  $this->setName('troubleshooting:check-config-valid')
72  ->setDescription('Check if config.xml is configured properly.');
73  }
74 
83  protected function execute(InputInterface $input, OutputInterface $output)
84  {
85  $output = $this->objectManager->create(
86  \Magento\Mtf\Console\Output::class,
87  ['output' => $output]
88  );
89  $output->writeln("Checking config.xml file configuration...");
90  $output->outputMessages($this->checkConfigFileAvailable());
91  $output->writeln("config.xml file check is finished.");
92  }
93 
99  private function checkConfigFileAvailable()
100  {
101  $messages = [];
102  $configFileExists = false;
103  if (file_exists($this->configFilePath)) {
104  $configFileExists = true;
105  if ($this->recursiveKeys($this->configXml) != $this->recursiveKeys($this->configXmlDist)) {
106  $messages['error'][] = 'Check your config.xml file to contain all configs from config.xml.dist.';
107  }
108  } else {
109  if (file_exists($this->configFilePath . '.dist')) {
110  if (!copy($this->configFilePath . '.dist', $this->configFilePath)) {
111  $messages['error'][] = 'Failed to copy config.xml.dist to config.xml.';
112  return $messages;
113  }
114  $messages['info'][] = 'config.xml file has been created based on config.xml.dist.';
115  $configFileExists = true;
116  }
117  }
118  if (!$configFileExists) {
119  $messages['error'][] = 'Cannot define config.xml configuration path.';
120  }
121  return $messages;
122  }
123 
130  private function recursiveKeys(array $input)
131  {
132  $output = array_keys($input);
133  foreach ($input as $sub) {
134  if (is_array($sub)) {
135  $output = array_merge($output, $this->recursiveKeys($sub));
136  }
137  }
138  return $output;
139  }
140 }
$objectManager
Definition: bootstrap.php:17
execute(InputInterface $input, OutputInterface $output)
defined('MTF_TESTS_PATH')||define('MTF_TESTS_PATH' MTF_BP
Definition: bootstrap.php:10
__construct(ObjectManagerInterface $objectManager, DataInterface $configXml, DataInterface $configXmlDist)