1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of project-quality-inspector. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre GESLIN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ProjectQualityInspector\Loader; |
12
|
|
|
|
13
|
|
|
use ProjectQualityInspector\Iterator\RuleFilterIterator; |
14
|
|
|
use ProjectQualityInspector\Rule\ComposerConfigRule; |
15
|
|
|
use ProjectQualityInspector\Rule\FilesRule; |
16
|
|
|
use ProjectQualityInspector\Rule\GitRule; |
17
|
|
|
use ProjectQualityInspector\Rule\CurlRule; |
18
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
19
|
|
|
use Symfony\Component\Yaml\Yaml; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class RulesLoader |
23
|
|
|
* |
24
|
|
|
* @package ProjectQualityInspector\Loader |
25
|
|
|
*/ |
26
|
|
|
class RulesLoader |
27
|
|
|
{ |
28
|
|
|
const COMMON_RULES = 'common'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $configFile |
32
|
|
|
* @param string $applicationType |
33
|
|
|
* @param string $baseDir |
34
|
|
|
* @param string $baseDir |
35
|
|
|
* @return array $ruleNames |
36
|
|
|
* |
37
|
|
|
* @throws \InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function load($configFile, $applicationType, $baseDir, array $ruleNames = []) |
40
|
|
|
{ |
41
|
|
|
$configs = $this->parseFileContent($configFile); |
42
|
|
|
$existingRules = $this->getExistingRules(); |
43
|
|
|
|
44
|
|
|
if (!isset($configs[$applicationType])) { |
45
|
|
|
throw new \InvalidArgumentException(sprintf('application type "%s" does not exists in config file.', $applicationType)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$config = $configs[$applicationType]; |
49
|
|
|
|
50
|
|
|
if (isset($configs[$this::COMMON_RULES])) { |
51
|
|
|
$config = array_merge_recursive($config, $configs[$this::COMMON_RULES]); //TODO: test if array_merge do not only concatenate arrays and sub arrays |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$rules = new \ArrayIterator(); |
55
|
|
|
foreach ($config as $ruleName => $ruleConfig) { |
56
|
|
|
if (key_exists($ruleName, $existingRules)) { //TODO delete existingRules, and try to instanciate class with sanitize names in CamelCases |
57
|
|
|
$rules[] = new $existingRules[$ruleName]($ruleConfig['config'], $baseDir); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new RuleFilterIterator($rules, $ruleNames); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $configFile |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function parseFileContent($configFile) |
69
|
|
|
{ |
70
|
|
|
if (!file_exists($configFile)) { |
71
|
|
|
throw new \InvalidArgumentException(sprintf('config file "%s" not found.', $configFile)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$configs = Yaml::parse(file_get_contents($configFile)); //TODO: change this deprecated method call |
76
|
|
|
} catch (ParseException $e) { |
77
|
|
|
throw new \InvalidArgumentException(sprintf("unable to parse the YAML string in file %s: %s", $configFile, $e->getMessage())); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $configs; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function getExistingRules() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
call_user_func(FilesRule::class . '::getRuleName') => FilesRule::class, |
90
|
|
|
call_user_func(ComposerConfigRule::class . '::getRuleName') => ComposerConfigRule::class, |
91
|
|
|
call_user_func(GitRule::class . '::getRuleName') => GitRule::class, |
92
|
|
|
call_user_func(CurlRule::class . '::getRuleName') => CurlRule::class, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |