Completed
Push — master ( 5bfa5e...03b9ab )
by Richard
03:43
created

DIC::result_database_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
                , $c["schemas"]
78 2
                );
79
        };
80
81
        $this["analyzer_factory"] = function($c) {
82
            return new \Lechimp\Dicto\Analysis\AnalyzerFactory
83 1
                ( $c["log"]
84 1
                , $c["ruleset"]
85 1
                );
86
        };
87
88
        $this["php_parser"] = function() {
89
            $lexer = new \PhpParser\Lexer\Emulative
90 2
                (["usedAttributes" => ["comments", "startLine", "endLine", "startFilePos"]]);
91 2
            return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer);
92
        };
93
94
        $this["result_database"] = function($c) {
95 3
            $config = $c["config"];
96 3
            if ($config->analysis_store_results()) {
97 3
                $path = $this->result_database_path($config);
98 3
                $connection = DB\DB::sqlite_connection($path);
99 3
            }
100
            else {
101 1
                $connection = DB\DB::sqlite_connection();
102
            }
103 3
            $db = new Report\ResultDB($connection);
104 3
            $db->maybe_init_database_schema();
105 3
            return $db;
106
        };
107
108
        $this["report_generator"] = function($c) {
109 1
            return new Report\Generator($c["report_queries"]);
110
        };
111
112
        $this["report_queries"] = function($c) {
113 1
            return new Report\Queries($c["result_database"]);
114
        };
115
116
        $this["source_status"] = function($c) {
117 2
            return new SourceStatusGit($c["config"]->project_root());
118
        };
119
120
        $this["schemas"] = function($c) {
121 3
            return $this->load_schemas($c["config"]->rules_schemas());
122
        };
123
124
        $this["properties"] = function($c) {
125 2
            return $this->load_properties($c["config"]->rules_properties());
126
        };
127
128 2
        $this["variables"] = function($c) {
129 2
            return $this->load_variables($c["config"]->rules_variables());
130
        };
131 12
    }
132
133
    /**
134
     * Loads the schemas defined in the config.
135
     *
136
     * @param   array   $schema_classes
137
     * @return  R\Schema[]
138
     */
139 4
    protected function load_schemas(array $schema_classes) {
140 4
        $schemas = array();
141 4
        foreach ($schema_classes as $schema_class) {
142 4
            $schema = new $schema_class;
143 4
            if (!($schema instanceof R\Schema)) {
144
                throw new \RuntimeException("'$schema_class' is not a Schema-class.");
145
            }
146 4
            $schemas[] = $schema;
147 4
        }
148 4
        return $schemas;
149
    }
150
151
    /**
152
     * Loads the properties defined in the config.
153
     *
154
     * @param   array   $property_classes
155
     * @return  R\Schema[]
156
     */
157 3
    protected function load_properties(array $property_classes) {
158 3
        $properties = array();
159 3
        foreach ($property_classes as $property_class) {
160 3
            $property = new $property_class;
161 3
            if (!($property instanceof V\Property)) {
162
                throw new \RuntimeException("'$property_class' is not a Schema-class.");
163
            }
164 3
            $properties[] = $property;
165 3
        }
166 3
        return $properties;
167
    }
168
169
    /**
170
     * Loads the variables defined in the config.
171
     *
172
     * @param   array   $variable_classes
173
     * @return  R\Schema[]
174
     */
175 3
    protected function load_variables(array $variable_classes) {
176 3
        $variables = array();
177 3
        foreach ($variable_classes as $variable_class) {
178 3
            $variable = new $variable_class;
179 3
            if (!($variable instanceof V\Variable)) {
180
                throw new \RuntimeException("'$variable_class' is not a Schema-class.");
181
            }
182 3
            $variables[] = $variable;
183 3
        }
184 3
        return $variables;
185
    }
186
187
    // TODO: This should totally go to config.
188 3
    protected function result_database_path(Config $c) {
189 3
        return $c->project_storage()."/results.sqlite";
190
    }
191
}
192