1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\Config; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\Definition\Processor; |
8
|
|
|
use Symfony\Component\Yaml\Parser; |
9
|
|
|
use YamlStandards\Command\Service\YamlFilesPathService; |
10
|
|
|
use YamlStandards\Model\Config\Exception\YamlStandardConfigNotFoundException; |
11
|
|
|
|
12
|
|
|
class YamlStandardConfigLoader |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $configFilename |
16
|
|
|
* @return \YamlStandards\Model\Config\YamlStandardConfigTotalData |
17
|
|
|
*/ |
18
|
10 |
|
public function loadFromYaml(string $configFilename): YamlStandardConfigTotalData |
19
|
|
|
{ |
20
|
10 |
|
if (file_exists($configFilename) === false) { |
21
|
|
|
throw new YamlStandardConfigNotFoundException('Config file ' . $configFilename . ' does not exist'); |
22
|
|
|
} |
23
|
|
|
|
24
|
10 |
|
$yamlParser = new Parser(); |
25
|
10 |
|
$yamlStandardsConfigDefinition = new YamlStandardConfigDefinition(); |
26
|
10 |
|
$processor = new Processor(); |
27
|
|
|
|
28
|
10 |
|
$configContent = $yamlParser->parseFile($configFilename); |
29
|
10 |
|
$outputConfigs = $processor->processConfiguration($yamlStandardsConfigDefinition, [$configContent]); |
30
|
|
|
|
31
|
9 |
|
return $this->createConfigData($outputConfigs); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $outputConfigs |
36
|
|
|
* @return \YamlStandards\Model\Config\YamlStandardConfigTotalData |
37
|
|
|
*/ |
38
|
9 |
|
private function createConfigData(array $outputConfigs): YamlStandardConfigTotalData |
39
|
|
|
{ |
40
|
9 |
|
$yamlStandardConfigsSingleData = []; |
41
|
|
|
|
42
|
9 |
|
foreach ($outputConfigs as $outputConfig) { |
43
|
9 |
|
$pathToYamlFiles = YamlFilesPathService::getPathToYamlFiles($outputConfig[YamlStandardConfigDefinition::CONFIG_PATHS_TO_CHECK]); |
44
|
9 |
|
$pathToExcludedYamlFiles = YamlFilesPathService::getPathToYamlFiles($outputConfig[YamlStandardConfigDefinition::CONFIG_EXCLUDED_PATHS]); |
45
|
9 |
|
$standardConfigs = $this->createStandardsConfig($outputConfig[YamlStandardConfigDefinition::CONFIG_CHECKERS]); |
46
|
|
|
|
47
|
9 |
|
$yamlStandardConfigsSingleData[] = new YamlStandardConfigSingleData($pathToYamlFiles, $pathToExcludedYamlFiles, $standardConfigs); |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
return new YamlStandardConfigTotalData($yamlStandardConfigsSingleData); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $checkersConfig |
55
|
|
|
* @return \YamlStandards\Model\Config\YamlStandardConfigSingleStandardData[] |
56
|
|
|
*/ |
57
|
9 |
|
private function createStandardsConfig(array $checkersConfig): array |
58
|
|
|
{ |
59
|
9 |
|
$yamlStandardConfigsSingleCheckerData = []; |
60
|
|
|
|
61
|
9 |
|
foreach ($checkersConfig as $checkerConfig) { |
62
|
9 |
|
$checkerClassName = $checkerConfig[YamlStandardConfigDefinition::CONFIG_PATH_TO_CHECKER]; |
63
|
9 |
|
$checkerClass = new $checkerClassName(); |
64
|
|
|
|
65
|
9 |
|
$fixerClassName = str_replace('Checker', 'Fixer', $checkerClassName); |
66
|
9 |
|
$fixerClass = class_exists($fixerClassName) === false ? null : new $fixerClassName(); |
67
|
|
|
|
68
|
9 |
|
$parameters = $checkerConfig[YamlStandardConfigDefinition::CONFIG_PARAMETERS_FOR_CHECKER]; |
69
|
9 |
|
$depth = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_DEPTH]; |
70
|
9 |
|
$indents = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_INDENTS]; |
71
|
9 |
|
$level = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_LEVEL]; |
72
|
9 |
|
$serviceAliasingType = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_SERVICE_ALIASING_TYPE]; |
73
|
9 |
|
$indentsCommentsWithoutParent = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_INDENTS_COMMENTS_WITHOUT_PARENT]; |
74
|
9 |
|
$alphabeticalPrioritizedKeys = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_ALPHABETICAL_PRIORITIZED_KEYS]; |
75
|
9 |
|
$ignoreCommentsIndent = $parameters[YamlStandardConfigDefinition::CONFIG_PARAMETERS_IGNORE_COMMENTS_INDENT]; |
76
|
9 |
|
$parametersClass = new StandardParametersData($depth, $indents, $level, $serviceAliasingType, $indentsCommentsWithoutParent, $alphabeticalPrioritizedKeys, $ignoreCommentsIndent); |
77
|
|
|
|
78
|
9 |
|
$yamlStandardConfigsSingleCheckerData[] = new YamlStandardConfigSingleStandardData($checkerClass, $fixerClass, $parametersClass); |
79
|
|
|
} |
80
|
|
|
|
81
|
9 |
|
return $yamlStandardConfigsSingleCheckerData; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|