1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\App; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\App\RuleLoader; |
14
|
|
|
use Lechimp\Dicto\Definition\RuleParser; |
15
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
16
|
|
|
use Lechimp\Dicto\Rules as R; |
17
|
|
|
use Lechimp\Dicto\Variables as V; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
use Pimple\Container; |
20
|
|
|
use PhpParser\ParserFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The App to be run from a script. |
24
|
|
|
*/ |
25
|
|
|
class App { |
26
|
|
|
/** |
27
|
|
|
* @var RuleLoader |
28
|
|
|
*/ |
29
|
|
|
protected $rule_loader; |
30
|
|
|
|
31
|
4 |
|
public function __construct() { |
32
|
4 |
|
ini_set('xdebug.max_nesting_level', 200); |
33
|
|
|
$parser = new RuleParser |
34
|
4 |
|
( array |
35
|
4 |
|
( new V\Classes() |
36
|
4 |
|
, new V\Functions() |
37
|
4 |
|
, new V\Globals() |
38
|
4 |
|
, new V\Files() |
39
|
4 |
|
, new V\Methods() |
40
|
|
|
// TODO: Add some language constructs here... |
41
|
4 |
|
) |
42
|
4 |
|
, array |
43
|
4 |
|
( new R\ContainText() |
44
|
4 |
|
, new R\DependOn() |
45
|
4 |
|
, new R\Invoke() |
46
|
4 |
|
) |
47
|
4 |
|
, array |
48
|
4 |
|
( new V\Name() |
49
|
4 |
|
) |
50
|
4 |
|
); |
51
|
4 |
|
$this->rule_loader = new RuleLoader($parser); |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Run the app. |
56
|
|
|
* |
57
|
|
|
* @param array $params from commandline |
58
|
|
|
* @return null |
59
|
|
|
*/ |
60
|
|
|
public function run(array $params) { |
61
|
|
|
if (count($params) < 2) { |
62
|
|
|
throw new \RuntimeException( |
63
|
|
|
"Expected path to rule-file as first parameter."); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$ruleset = $this->load_rules_file($params[1]); |
67
|
|
|
|
68
|
|
|
// drop programm name and rule file path |
69
|
|
|
array_shift($params); |
70
|
|
|
array_shift($params); |
71
|
|
|
|
72
|
|
|
$configs = array(); |
73
|
|
|
$this->load_configs($params, $configs); |
74
|
|
|
|
75
|
|
|
$dic = $this->create_dic($ruleset, $configs); |
76
|
|
|
|
77
|
|
|
$dic["engine"]->run(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Load rules and initial config from a *.php-file. |
82
|
|
|
* |
83
|
|
|
* @param string $path |
84
|
|
|
* @return array ($ruleset, $config) |
85
|
|
|
*/ |
86
|
4 |
|
protected function load_rules_file($path) { |
87
|
4 |
|
if (!file_exists($path)) { |
88
|
|
|
throw new \RuntimeException("Unknown rule-file '$path'"); |
89
|
|
|
} |
90
|
4 |
|
$ruleset = $this->rule_loader->load_rules_from($path); |
91
|
4 |
|
return $ruleset; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Load extra configs from yaml files. |
96
|
|
|
* |
97
|
|
|
* @param array $config_file_paths |
98
|
|
|
* @param array &$configs_array |
99
|
|
|
* @return null |
100
|
|
|
*/ |
101
|
3 |
|
protected function load_configs(array $config_file_paths, array &$configs_array) { |
102
|
3 |
|
foreach ($config_file_paths as $config_file) { |
103
|
3 |
|
if (!file_exists($config_file)) { |
104
|
|
|
throw new \RuntimeException("Unknown config-file '$config_file'"); |
105
|
|
|
} |
106
|
3 |
|
$configs_array[] = Yaml::parse(file_get_contents($config_file)); |
107
|
3 |
|
} |
108
|
3 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create and initialize the DI-container. |
112
|
|
|
* |
113
|
|
|
* @param RuleSet $ruleset |
114
|
|
|
* @param array &$configs |
115
|
|
|
* @return Container |
116
|
|
|
*/ |
117
|
3 |
|
protected function create_dic(RuleSet $ruleset, array &$configs) { |
118
|
3 |
|
$container = new Container(); |
119
|
|
|
|
120
|
|
|
$container["config"] = function () use (&$configs) { |
121
|
2 |
|
return new Config($configs); |
122
|
|
|
}; |
123
|
|
|
|
124
|
3 |
|
$container["ruleset"] = $ruleset; |
125
|
|
|
|
126
|
|
|
$container["engine"] = function($c) { |
127
|
|
|
return new Engine |
128
|
1 |
|
( $c["log"] |
129
|
1 |
|
, $c["config"] |
130
|
1 |
|
, $c["database_factory"] |
131
|
1 |
|
, $c["indexer_factory"] |
132
|
1 |
|
, $c["analyzer_factory"] |
133
|
1 |
|
, $c["source_status"] |
134
|
1 |
|
); |
135
|
|
|
}; |
136
|
|
|
|
137
|
|
|
$container["log"] = function () { |
138
|
2 |
|
return new CLILogger(); |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
$container["database_factory"] = function() { |
142
|
1 |
|
return new DBFactory(); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
$container["indexer_factory"] = function($c) { |
146
|
|
|
return new \Lechimp\Dicto\Indexer\IndexerFactory |
147
|
2 |
|
( $c["log"] |
148
|
2 |
|
, $c["php_parser"] |
149
|
2 |
|
, array |
150
|
2 |
|
( new \Lechimp\Dicto\Rules\ContainText() |
151
|
2 |
|
, new \Lechimp\Dicto\Rules\DependOn() |
152
|
2 |
|
, new \Lechimp\Dicto\Rules\Invoke() |
153
|
2 |
|
) |
154
|
2 |
|
); |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
$container["analyzer_factory"] = function($c) { |
158
|
|
|
return new \Lechimp\Dicto\Analysis\AnalyzerFactory |
159
|
1 |
|
( $c["log"] |
160
|
1 |
|
, $c["ruleset"] |
161
|
1 |
|
); |
162
|
|
|
}; |
163
|
|
|
|
164
|
|
|
$container["php_parser"] = function() { |
165
|
2 |
|
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
$container["report_generator"] = function() { |
169
|
|
|
return new CLIReportGenerator(); |
170
|
|
|
}; |
171
|
|
|
|
172
|
2 |
|
$container["source_status"] = function($c) { |
173
|
2 |
|
return new SourceStatusGit($c["config"]->project_root()); |
174
|
|
|
}; |
175
|
|
|
|
176
|
3 |
|
return $container; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|