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
|
4 |
|
public function __construct() { |
27
|
4 |
|
ini_set('xdebug.max_nesting_level', 200); |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Run the app. |
32
|
|
|
* |
33
|
|
|
* @param array $params from commandline |
34
|
|
|
* @return null |
35
|
|
|
*/ |
36
|
2 |
|
public function run(array $params) { |
37
|
1 |
|
if (count($params) < 3) { |
38
|
|
|
throw new \RuntimeException( |
39
|
|
|
"Expected path to rule-file as first parameter and path to config as second parameter."); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// drop program name |
43
|
2 |
|
array_shift($params); |
44
|
|
|
|
45
|
|
|
// the rest of the params are paths to configs |
46
|
1 |
|
list($config_file_path, $configs) = $this->load_configs($params); |
47
|
|
|
|
48
|
1 |
|
$dic = $this->create_dic($config_file_path, $configs); |
49
|
|
|
|
50
|
1 |
|
$dic["engine"]->run(); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Load extra configs from yaml files. |
55
|
|
|
* |
56
|
|
|
* @param array $config_file_paths |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
3 |
|
protected function load_configs(array $config_file_paths) { |
60
|
3 |
|
$configs_array = array(); |
61
|
3 |
|
$config_file_path = null; |
62
|
3 |
|
foreach ($config_file_paths as $config_file) { |
63
|
3 |
|
if (!file_exists($config_file)) { |
64
|
|
|
throw new \RuntimeException("Unknown config-file '$config_file'"); |
65
|
|
|
} |
66
|
3 |
|
if ($config_file_path === null) { |
67
|
3 |
|
$config_file_path = $config_file; |
68
|
3 |
|
} |
69
|
3 |
|
$configs_array[] = Yaml::parse(file_get_contents($config_file)); |
70
|
3 |
|
} |
71
|
3 |
|
return array($config_file_path, $configs_array); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create and initialize the DI-container. |
76
|
|
|
* |
77
|
|
|
* @param string $config_file_path |
78
|
|
|
* @param array &$configs |
79
|
|
|
* @return Container |
80
|
|
|
*/ |
81
|
3 |
|
protected function create_dic($config_file_path, array &$configs) { |
82
|
3 |
|
array('is_string($rule_file_path)'); |
83
|
|
|
|
84
|
3 |
|
$container = new Container(); |
85
|
|
|
|
86
|
|
|
$container["config"] = function () use ($config_file_path, &$configs) { |
87
|
2 |
|
return new Config($config_file_path, $configs); |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
$container["ruleset"] = function($c) { |
91
|
1 |
|
$rule_file_path = $c["config"]->project_rules(); |
92
|
1 |
|
if (!file_exists($rule_file_path)) { |
93
|
|
|
throw new \RuntimeException("Unknown rule-file '$rule_file_path'"); |
94
|
|
|
} |
95
|
1 |
|
$ruleset = $c["rule_loader"]->load_rules_from($rule_file_path); |
96
|
1 |
|
return $ruleset; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
$container["rule_loader"] = function($c) { |
100
|
1 |
|
return new RuleLoader($c["rule_parser"]); |
101
|
|
|
}; |
102
|
|
|
|
103
|
|
|
$container["rule_parser"] = function() { |
104
|
|
|
// TODO: Move this stuff to the config. |
105
|
|
|
return new RuleParser |
106
|
1 |
|
( array |
107
|
1 |
|
( new V\Classes() |
108
|
1 |
|
, new V\Functions() |
109
|
1 |
|
, new V\Globals() |
110
|
1 |
|
, new V\Files() |
111
|
1 |
|
, new V\Methods() |
112
|
1 |
|
, new V\LanguageConstruct("@", "ErrorSuppressor") |
113
|
1 |
|
, new V\LanguageConstruct("exit", "Exit") |
114
|
1 |
|
, new V\LanguageConstruct("die", "Die") |
115
|
|
|
// TODO: Add some language constructs here... |
116
|
1 |
|
) |
117
|
1 |
|
, array |
118
|
1 |
|
( new R\ContainText() |
119
|
1 |
|
, new R\DependOn() |
120
|
1 |
|
, new R\Invoke() |
121
|
1 |
|
) |
122
|
1 |
|
, array |
123
|
1 |
|
( new V\Name() |
124
|
1 |
|
, new V\In() |
125
|
1 |
|
) |
126
|
1 |
|
); |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
$container["engine"] = function($c) { |
130
|
|
|
return new Engine |
131
|
1 |
|
( $c["log"] |
132
|
1 |
|
, $c["config"] |
133
|
1 |
|
, $c["database_factory"] |
134
|
1 |
|
, $c["indexer_factory"] |
135
|
1 |
|
, $c["analyzer_factory"] |
136
|
1 |
|
, $c["source_status"] |
137
|
1 |
|
); |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
$container["log"] = function () { |
141
|
2 |
|
return new CLILogger(); |
142
|
|
|
}; |
143
|
|
|
|
144
|
|
|
$container["database_factory"] = function() { |
145
|
1 |
|
return new DBFactory(); |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
$container["indexer_factory"] = function($c) { |
149
|
|
|
return new \Lechimp\Dicto\Indexer\IndexerFactory |
150
|
2 |
|
( $c["log"] |
151
|
2 |
|
, $c["php_parser"] |
152
|
2 |
|
, array |
153
|
2 |
|
( new \Lechimp\Dicto\Rules\ContainText() |
154
|
2 |
|
, new \Lechimp\Dicto\Rules\DependOn() |
155
|
2 |
|
, new \Lechimp\Dicto\Rules\Invoke() |
156
|
2 |
|
) |
157
|
2 |
|
); |
158
|
|
|
}; |
159
|
|
|
|
160
|
|
|
$container["analyzer_factory"] = function($c) { |
161
|
|
|
return new \Lechimp\Dicto\Analysis\AnalyzerFactory |
162
|
1 |
|
( $c["log"] |
163
|
1 |
|
, $c["ruleset"] |
164
|
1 |
|
); |
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
$container["php_parser"] = function() { |
168
|
2 |
|
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
169
|
|
|
}; |
170
|
|
|
|
171
|
|
|
$container["report_generator"] = function() { |
172
|
|
|
return new CLIReportGenerator(); |
173
|
|
|
}; |
174
|
|
|
|
175
|
2 |
|
$container["source_status"] = function($c) { |
176
|
2 |
|
return new SourceStatusGit($c["config"]->project_root()); |
177
|
|
|
}; |
178
|
|
|
|
179
|
3 |
|
return $container; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|