1 | <?php |
||
27 | class App { |
||
28 | 16 | public function __construct() { |
|
31 | |||
32 | /** |
||
33 | * Run the app. |
||
34 | * |
||
35 | * @param array $params from commandline |
||
36 | * @return null |
||
37 | */ |
||
38 | 1 | public function run(array $params) { |
|
59 | |||
60 | /** |
||
61 | * Create and initialize the DI-container. |
||
62 | * |
||
63 | * @param string $config_file_path |
||
64 | * @param array &$configs |
||
65 | * @return Container |
||
66 | */ |
||
67 | 10 | protected function create_dic($config_file_path, array &$configs) { |
|
68 | 10 | array('is_string($rule_file_path)'); |
|
69 | |||
70 | 10 | $container = new Container(); |
|
71 | |||
72 | $container["config"] = function () use ($config_file_path, &$configs) { |
||
73 | 9 | return new Config($config_file_path, $configs); |
|
74 | }; |
||
75 | |||
76 | $container["ruleset"] = function($c) { |
||
77 | 1 | $rule_file_path = $c["config"]->project_rules(); |
|
78 | 1 | if (!file_exists($rule_file_path)) { |
|
79 | throw new \RuntimeException("Unknown rule-file '$rule_file_path'"); |
||
80 | } |
||
81 | 1 | $ruleset = $c["rule_loader"]->load_rules_from($rule_file_path); |
|
82 | 1 | return $ruleset; |
|
83 | }; |
||
84 | |||
85 | $container["rule_loader"] = function($c) { |
||
86 | 1 | return new RuleLoader($c["rule_parser"]); |
|
87 | }; |
||
88 | |||
89 | $container["rule_parser"] = function($c) { |
||
90 | return new RuleParser |
||
91 | 1 | ( $c["variables"] |
|
92 | 1 | , $c["schemas"] |
|
93 | 1 | , $c["properties"] |
|
94 | 1 | ); |
|
95 | }; |
||
96 | |||
97 | $container["engine"] = function($c) { |
||
98 | return new Engine |
||
99 | 1 | ( $c["log"] |
|
100 | 1 | , $c["config"] |
|
101 | 1 | , $c["database_factory"] |
|
102 | 5 | , $c["indexer_factory"] |
|
103 | 1 | , $c["analyzer_factory"] |
|
104 | 1 | , $c["report_generator"] |
|
105 | 1 | , $c["source_status"] |
|
106 | 1 | ); |
|
107 | }; |
||
108 | |||
109 | $container["log"] = function () { |
||
110 | 2 | return new CLILogger(); |
|
111 | }; |
||
112 | |||
113 | $container["database_factory"] = function() { |
||
114 | 3 | return new DBFactory(); |
|
115 | }; |
||
116 | |||
117 | $container["indexer_factory"] = function($c) { |
||
118 | return new \Lechimp\Dicto\Indexer\IndexerFactory |
||
119 | 2 | ( $c["log"] |
|
120 | 2 | , $c["php_parser"] |
|
121 | 2 | , array |
|
122 | 2 | ( new \Lechimp\Dicto\Rules\ContainText() |
|
123 | 2 | , new \Lechimp\Dicto\Rules\DependOn() |
|
124 | 2 | , new \Lechimp\Dicto\Rules\Invoke() |
|
125 | 2 | ) |
|
126 | 2 | ); |
|
127 | }; |
||
128 | |||
129 | $container["analyzer_factory"] = function($c) { |
||
130 | return new \Lechimp\Dicto\Analysis\AnalyzerFactory |
||
131 | 1 | ( $c["log"] |
|
132 | 1 | , $c["ruleset"] |
|
133 | 1 | ); |
|
134 | }; |
||
135 | |||
136 | $container["php_parser"] = function() { |
||
137 | $lexer = new \PhpParser\Lexer\Emulative |
||
138 | 2 | (["usedAttributes" => ["comments", "startLine", "endLine", "startFilePos"]]); |
|
139 | 2 | return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer); |
|
140 | }; |
||
141 | |||
142 | $container["report_generator"] = function($c) { |
||
143 | 5 | return $this->build_report_generator($c); |
|
144 | }; |
||
145 | |||
146 | $container["stdout_report_generator"] = function() { |
||
147 | 3 | return new CLIReportGenerator(); |
|
148 | }; |
||
149 | |||
150 | $container["database_report_generator"] = function($c) { |
||
151 | 2 | $path = $this->result_database_path($c["config"]); |
|
152 | 2 | return $c["database_factory"]->get_result_db($path); |
|
153 | }; |
||
154 | |||
155 | $container["source_status"] = function($c) { |
||
156 | 2 | return new SourceStatusGit($c["config"]->project_root()); |
|
157 | }; |
||
158 | |||
159 | $container["schemas"] = function($c) { |
||
160 | 2 | return $this->load_schemas($c["config"]->rules_schemas()); |
|
161 | }; |
||
162 | |||
163 | $container["properties"] = function($c) { |
||
164 | 2 | return $this->load_properties($c["config"]->rules_properties()); |
|
165 | }; |
||
166 | |||
167 | 2 | $container["variables"] = function($c) { |
|
168 | 2 | return $this->load_variables($c["config"]->rules_variables()); |
|
169 | }; |
||
170 | |||
171 | 10 | return $container; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Configure php runtime. |
||
176 | * |
||
177 | * @param Config $config |
||
178 | * @return null |
||
179 | */ |
||
180 | 2 | protected function configure_runtime(Config $config) { |
|
192 | |||
193 | /** |
||
194 | * Load extra configs from yaml files. |
||
195 | * |
||
196 | * @param array $config_file_paths |
||
197 | * @return array |
||
198 | */ |
||
199 | 10 | protected function load_configs(array $config_file_paths) { |
|
213 | |||
214 | /** |
||
215 | * Loads the schemas defined in the config. |
||
216 | * |
||
217 | * @param array $schema_classes |
||
218 | * @return R\Schema[] |
||
219 | */ |
||
220 | 3 | protected function load_schemas(array $schema_classes) { |
|
231 | |||
232 | /** |
||
233 | * Loads the properties defined in the config. |
||
234 | * |
||
235 | * @param array $property_classes |
||
236 | * @return R\Schema[] |
||
237 | */ |
||
238 | 3 | protected function load_properties(array $property_classes) { |
|
249 | |||
250 | /** |
||
251 | * Loads the variables defined in the config. |
||
252 | * |
||
253 | * @param array $variable_classes |
||
254 | * @return R\Schema[] |
||
255 | */ |
||
256 | 3 | protected function load_variables(array $variable_classes) { |
|
267 | |||
268 | /** |
||
269 | * Build the report generators. |
||
270 | * |
||
271 | * @param Container $c |
||
272 | * @return ReportGenerator |
||
273 | */ |
||
274 | 5 | public function build_report_generator(Container $c) { |
|
294 | |||
295 | 2 | protected function result_database_path(Config $c) { |
|
298 | } |
||
299 |