AbstractCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Abstract shell command for the static code inspection
  8. */
  9. namespace Magento\TestFramework\Inspection;
  10. abstract class AbstractCommand
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $_reportFile;
  16. /**
  17. * @var int
  18. */
  19. protected $_lastExitCode;
  20. /**
  21. * @var string
  22. */
  23. protected $_lastOutput;
  24. /**
  25. * @var string
  26. */
  27. protected $_lastRunMessage;
  28. /**
  29. * Constructor
  30. *
  31. * @param string $reportFile Destination file to write inspection report to
  32. */
  33. public function __construct($reportFile)
  34. {
  35. $this->_reportFile = $reportFile;
  36. }
  37. /**
  38. * Build and execute the shell command
  39. *
  40. * @param array $whiteList Files/directories to be inspected
  41. * @param array $blackList Files/directories to be excluded from the inspection
  42. * @return bool
  43. */
  44. public function run(array $whiteList, array $blackList = [])
  45. {
  46. if (file_exists($this->_reportFile)) {
  47. unlink($this->_reportFile);
  48. }
  49. $shellCmd = $this->_buildShellCmd($whiteList, $blackList);
  50. $result = $this->_execShellCmd($shellCmd);
  51. $this->_generateLastRunMessage();
  52. return $result !== false;
  53. }
  54. /**
  55. * Whether the command can be ran on the current environment
  56. *
  57. * @return bool
  58. */
  59. public function canRun()
  60. {
  61. return $this->_execShellCmd($this->_buildVersionShellCmd()) !== false;
  62. }
  63. /**
  64. * Retrieve the shell command version
  65. *
  66. * @return string|null
  67. */
  68. public function getVersion()
  69. {
  70. $versionOutput = $this->_execShellCmd($this->_buildVersionShellCmd());
  71. if (!$versionOutput) {
  72. return null;
  73. }
  74. return preg_match('/[^\d]*([^\s]+)/', $versionOutput, $matches) ? $matches[1] : $versionOutput;
  75. }
  76. /**
  77. * Get path to the report file
  78. *
  79. * @return string
  80. */
  81. public function getReportFile()
  82. {
  83. return $this->_reportFile;
  84. }
  85. /**
  86. * Build the shell command that outputs the version
  87. *
  88. * @return string
  89. */
  90. abstract protected function _buildVersionShellCmd();
  91. /**
  92. * Build the valid shell command
  93. *
  94. * @param array $whiteList
  95. * @param array $blackList
  96. * @return string
  97. */
  98. abstract protected function _buildShellCmd($whiteList, $blackList);
  99. /**
  100. * Execute a shell command on the current environment and return its output or FALSE on failure
  101. *
  102. * @param string $shellCmd
  103. * @return string|false
  104. */
  105. protected function _execShellCmd($shellCmd)
  106. {
  107. $output = [];
  108. exec($shellCmd . ' 2>&1', $output, $this->_lastExitCode);
  109. $this->_lastOutput = implode(PHP_EOL, $output);
  110. return $this->_lastExitCode === 0 ? $this->_lastOutput : false;
  111. }
  112. /**
  113. * Generate message about last execution result, prepared for output to a user
  114. *
  115. * @return \Magento\TestFramework\Inspection\AbstractCommand
  116. */
  117. protected function _generateLastRunMessage()
  118. {
  119. if ($this->_lastExitCode === null) {
  120. $this->_lastRunMessage = "Nothing was executed.";
  121. } elseif (!$this->_lastExitCode) {
  122. $this->_lastRunMessage = 'Success reported.';
  123. } elseif (file_exists($this->_reportFile)) {
  124. $this->_lastRunMessage = "See detailed report in '{$this->_reportFile}'.";
  125. } else {
  126. $this->_lastRunMessage = 'Command-line tool reports: ' . $this->_lastOutput;
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Return message from the last run of the command
  132. *
  133. * @return string
  134. */
  135. public function getLastRunMessage()
  136. {
  137. return $this->_lastRunMessage;
  138. }
  139. }