1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Command; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
14
|
|
|
use YamlStandards\Command\Service\ResultService; |
15
|
|
|
use YamlStandards\Model\Config\YamlStandardConfigLoader; |
16
|
|
|
use YamlStandards\Result\Result; |
17
|
|
|
|
18
|
|
|
class YamlCommand extends Command |
19
|
|
|
{ |
20
|
|
|
private const COMMAND_NAME = 'yaml-standards'; |
21
|
|
|
|
22
|
|
|
public const ARGUMENT_PATH_TO_CONFIG_FILE = 'pathToConfigFile'; |
23
|
|
|
public const OPTION_FIX = 'fix'; |
24
|
|
|
|
25
|
|
|
protected static $defaultName = self::COMMAND_NAME; |
26
|
|
|
|
27
|
10 |
|
protected function configure(): void |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
10 |
|
->setName(self::COMMAND_NAME) // set command name for symfony/console lower version as 3.4 |
31
|
10 |
|
->setDescription('Check yaml files respect standards') |
32
|
10 |
|
->addArgument(self::ARGUMENT_PATH_TO_CONFIG_FILE, InputArgument::OPTIONAL, 'Path to configuration file. By default configuration file is looking in root directory', './yaml-standards.yaml') |
33
|
10 |
|
->addOption(self::OPTION_FIX, null, InputOption::VALUE_NONE, 'Automatically fix problems'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
10 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
40
|
|
|
{ |
41
|
10 |
|
$inputSettingData = new InputSettingData($input); |
42
|
|
|
|
43
|
10 |
|
$yamlStandardConfigLoader = new YamlStandardConfigLoader(); |
44
|
10 |
|
$yamlStandardConfigTotalData = $yamlStandardConfigLoader->loadFromYaml($inputSettingData->getPathToConfigFile()); |
45
|
|
|
|
46
|
9 |
|
$symfonyStyle = new SymfonyStyle($input, $output); |
47
|
9 |
|
$progressBar = $symfonyStyle->createProgressBar($yamlStandardConfigTotalData->getTotalCountOfYamlFiles()); |
48
|
9 |
|
$progressBar->setFormat('debug'); |
49
|
9 |
|
$results = [[]]; |
50
|
|
|
|
51
|
9 |
|
foreach ($yamlStandardConfigTotalData->getYamlStandardConfigsSingleData() as $yamlStandardConfigSingleData) { |
52
|
9 |
|
foreach ($yamlStandardConfigSingleData->getPathToYamlFiles() as $pathToYamlFile) { |
53
|
9 |
|
$fileResults = []; |
54
|
9 |
|
if ($this->isFileExcluded($pathToYamlFile, $yamlStandardConfigSingleData->getPathToExcludedYamlFiles())) { |
55
|
5 |
|
$progressBar->advance(); |
56
|
5 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
if (is_readable($pathToYamlFile) === false) { |
60
|
|
|
$message = 'File is not readable.'; |
61
|
|
|
$fileResults[] = new Result($pathToYamlFile, Result::RESULT_CODE_GENERAL_ERROR, $message); |
62
|
|
|
$progressBar->advance(); |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
8 |
|
foreach ($yamlStandardConfigSingleData->getYamlStandardConfigsSingleStandardData() as $yamlStandardConfigSingleCheckerData) { |
68
|
8 |
|
$standardParametersData = $yamlStandardConfigSingleCheckerData->getStandardParametersData(); |
69
|
8 |
|
$fixer = $yamlStandardConfigSingleCheckerData->getFixer(); |
70
|
8 |
|
if ($fixer !== null && $inputSettingData->isFixEnabled()) { |
71
|
|
|
$fileResults[] = $fixer->runFix($pathToYamlFile, $pathToYamlFile, $standardParametersData); |
72
|
|
|
} else { |
73
|
8 |
|
$fileResults[] = $yamlStandardConfigSingleCheckerData->getChecker()->runCheck($pathToYamlFile, $standardParametersData); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} catch (ParseException $e) { |
77
|
|
|
$message = sprintf('Unable to parse the YAML string: %s', $e->getMessage()); |
78
|
|
|
$fileResults[] = new Result($pathToYamlFile, Result::RESULT_CODE_GENERAL_ERROR, $message); |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
$results[] = $fileResults; |
82
|
8 |
|
$progressBar->advance(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
9 |
|
$progressBar->finish(); |
86
|
|
|
/** @var \YamlStandards\Result\Result[] $results */ |
87
|
9 |
|
$results = array_merge(...$results); // add all results to one array instead of multidimensional array with results for every file |
|
|
|
|
88
|
|
|
|
89
|
9 |
|
return $this->printOutput($output, $results); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
94
|
|
|
* @param \YamlStandards\Result\Result[] $results |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
9 |
|
private function printOutput(OutputInterface $output, array $results): int |
98
|
|
|
{ |
99
|
9 |
|
$output->writeln(PHP_EOL); |
100
|
9 |
|
foreach ($results as $result) { |
101
|
8 |
|
if ($result->getResultCode() !== Result::RESULT_CODE_OK) { |
102
|
|
|
$output->writeln(sprintf('FILE: %s', $result->getPathToFile())); |
103
|
|
|
$output->writeln('-------------------------------------------------'); |
104
|
|
|
$output->writeln($result->getMessage() . PHP_EOL); |
105
|
|
|
|
106
|
|
|
if ($result->canBeFixedByFixer()) { |
107
|
|
|
$output->writeln('<fg=red>This can be fixed by `--fix` option</fg=red>' . PHP_EOL); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
9 |
|
return ResultService::getResultCodeByResults($results); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $pathToYamlFile |
117
|
|
|
* @param string[] $pathToExcludedYamlFiles |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
9 |
|
private function isFileExcluded(string $pathToYamlFile, array $pathToExcludedYamlFiles): bool |
121
|
|
|
{ |
122
|
9 |
|
if (in_array($pathToYamlFile, $pathToExcludedYamlFiles, true)) { |
123
|
5 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|