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\Analysis\CombinedListener; |
14
|
|
|
use Lechimp\Dicto\Analysis\Listener; |
15
|
|
|
use Lechimp\Dicto\App\RuleLoader; |
16
|
|
|
use Lechimp\Dicto\DB; |
17
|
|
|
use Lechimp\Dicto\Definition\RuleParser; |
18
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
19
|
|
|
use Lechimp\Dicto\Rules as R; |
20
|
|
|
use Lechimp\Dicto\Variables as V; |
21
|
|
|
use Lechimp\Dicto\Report; |
22
|
|
|
use Pimple\Container; |
23
|
|
|
use PhpParser\ParserFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The dependency injection container for the app. |
27
|
|
|
*/ |
28
|
|
|
class DIC extends Container { |
29
|
12 |
|
public function __construct(Config $config) { |
30
|
12 |
|
$this["config"] = $config; |
31
|
|
|
|
32
|
|
|
$this["ruleset"] = function($c) { |
33
|
1 |
|
$rule_file_path = $c["config"]->project_rules(); |
34
|
1 |
|
if (!file_exists($rule_file_path)) { |
35
|
|
|
throw new \RuntimeException("Unknown rule-file '$rule_file_path'"); |
36
|
|
|
} |
37
|
1 |
|
$ruleset = $c["rule_loader"]->load_rules_from($rule_file_path); |
38
|
1 |
|
return $ruleset; |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
$this["rule_loader"] = function($c) { |
42
|
1 |
|
return new RuleLoader($c["rule_parser"]); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
$this["rule_parser"] = function($c) { |
46
|
|
|
return new RuleParser |
47
|
1 |
|
( $c["variables"] |
48
|
1 |
|
, $c["schemas"] |
49
|
1 |
|
, $c["properties"] |
50
|
1 |
|
); |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
$this["engine"] = function($c) { |
54
|
|
|
return new Engine |
55
|
1 |
|
( $c["log"] |
56
|
1 |
|
, $c["config"] |
57
|
1 |
|
, $c["indexdb_factory"] |
58
|
1 |
|
, $c["indexer_factory"] |
59
|
1 |
|
, $c["analyzer_factory"] |
60
|
1 |
|
, $c["result_database"] |
61
|
1 |
|
, $c["source_status"] |
62
|
1 |
|
); |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
$this["log"] = function () { |
66
|
2 |
|
return new CLILogger(); |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
$this["indexdb_factory"] = function() { |
70
|
1 |
|
return new DB\IndexDBFactory(); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
$this["indexer_factory"] = function($c) { |
74
|
|
|
return new \Lechimp\Dicto\Indexer\IndexerFactory |
75
|
2 |
|
( $c["log"] |
76
|
2 |
|
, $c["php_parser"] |
77
|
2 |
|
, array |
78
|
2 |
|
( new \Lechimp\Dicto\Rules\ContainText() |
79
|
2 |
|
, new \Lechimp\Dicto\Rules\DependOn() |
80
|
2 |
|
, new \Lechimp\Dicto\Rules\Invoke() |
81
|
2 |
|
) |
82
|
2 |
|
); |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
$this["analyzer_factory"] = function($c) { |
86
|
|
|
return new \Lechimp\Dicto\Analysis\AnalyzerFactory |
87
|
1 |
|
( $c["log"] |
88
|
1 |
|
, $c["ruleset"] |
89
|
1 |
|
); |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
$this["php_parser"] = function() { |
93
|
|
|
$lexer = new \PhpParser\Lexer\Emulative |
94
|
2 |
|
(["usedAttributes" => ["comments", "startLine", "endLine", "startFilePos"]]); |
95
|
2 |
|
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer); |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
$this["result_database"] = function($c) { |
99
|
|
|
// TODO: adjust this to the new analysis.store_results config parameter. |
100
|
3 |
|
$config = $c["config"]; |
101
|
3 |
|
if ($config->analysis_store_results()) { |
102
|
3 |
|
$path = $this->result_database_path($config); |
103
|
3 |
|
$connection = DB\DB::sqlite_connection($path); |
104
|
3 |
|
} |
105
|
|
|
else { |
106
|
1 |
|
$connection = DB\DB::sqlite_connection(); |
107
|
|
|
} |
108
|
3 |
|
$db = new Report\ResultDB($connection); |
109
|
3 |
|
$db->maybe_init_database_schema(); |
110
|
3 |
|
return $db; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
$this["report_generator"] = function($c) { |
114
|
1 |
|
return new Report\Generator($c["report_queries"]); |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
$this["report_queries"] = function($c) { |
118
|
1 |
|
return new Report\Queries($c["result_database"]); |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
$this["source_status"] = function($c) { |
122
|
2 |
|
return new SourceStatusGit($c["config"]->project_root()); |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
$this["schemas"] = function($c) { |
126
|
2 |
|
return $this->load_schemas($c["config"]->rules_schemas()); |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
$this["properties"] = function($c) { |
130
|
2 |
|
return $this->load_properties($c["config"]->rules_properties()); |
131
|
|
|
}; |
132
|
|
|
|
133
|
2 |
|
$this["variables"] = function($c) { |
134
|
2 |
|
return $this->load_variables($c["config"]->rules_variables()); |
135
|
|
|
}; |
136
|
12 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Loads the schemas defined in the config. |
140
|
|
|
* |
141
|
|
|
* @param array $schema_classes |
142
|
|
|
* @return R\Schema[] |
143
|
|
|
*/ |
144
|
3 |
|
protected function load_schemas(array $schema_classes) { |
145
|
3 |
|
$schemas = array(); |
146
|
3 |
|
foreach ($schema_classes as $schema_class) { |
147
|
3 |
|
$schema = new $schema_class; |
148
|
3 |
|
if (!($schema instanceof R\Schema)) { |
149
|
|
|
throw new \RuntimeException("'$schema_class' is not a Schema-class."); |
150
|
|
|
} |
151
|
3 |
|
$schemas[] = $schema; |
152
|
3 |
|
} |
153
|
3 |
|
return $schemas; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Loads the properties defined in the config. |
158
|
|
|
* |
159
|
|
|
* @param array $property_classes |
160
|
|
|
* @return R\Schema[] |
161
|
|
|
*/ |
162
|
3 |
|
protected function load_properties(array $property_classes) { |
163
|
3 |
|
$properties = array(); |
164
|
3 |
|
foreach ($property_classes as $property_class) { |
165
|
3 |
|
$property = new $property_class; |
166
|
3 |
|
if (!($property instanceof V\Property)) { |
167
|
|
|
throw new \RuntimeException("'$property_class' is not a Schema-class."); |
168
|
|
|
} |
169
|
3 |
|
$properties[] = $property; |
170
|
3 |
|
} |
171
|
3 |
|
return $properties; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Loads the variables defined in the config. |
176
|
|
|
* |
177
|
|
|
* @param array $variable_classes |
178
|
|
|
* @return R\Schema[] |
179
|
|
|
*/ |
180
|
3 |
|
protected function load_variables(array $variable_classes) { |
181
|
3 |
|
$variables = array(); |
182
|
3 |
|
foreach ($variable_classes as $variable_class) { |
183
|
3 |
|
$variable = new $variable_class; |
184
|
3 |
|
if (!($variable instanceof V\Variable)) { |
185
|
|
|
throw new \RuntimeException("'$variable_class' is not a Schema-class."); |
186
|
|
|
} |
187
|
3 |
|
$variables[] = $variable; |
188
|
3 |
|
} |
189
|
3 |
|
return $variables; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// TODO: This should totally go to config. |
193
|
3 |
|
protected function result_database_path(Config $c) { |
194
|
3 |
|
return $c->project_storage()."/results.sqlite"; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|