Completed
Push — master ( cc2765...340ff8 )
by Richard
08:19
created

App::load_configs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3.0261
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 Symfony\Component\Yaml\Yaml;
17
use Pimple\Container;
18
use PhpParser\ParserFactory;
19
20
/**
21
 * The App to be run from a script.
22
 */
23
class App {
24
    /**
25
     * @var RuleLoader
26
     */
27
    protected $rule_loader;
28
29 4
    public function __construct() {
30 4
        ini_set('xdebug.max_nesting_level', 200);
31 4
        $parser = new RuleParser();
32 4
        $this->rule_loader = new RuleLoader($parser);
33 4
    }
34
35
    /**
36
     * Run the app.
37
     *
38
     * @param   array   $params     from commandline
39
     * @return  null
40
     */
41 1
    public function run(array $params) {
42
        if (count($params) < 2) {
43 1
            throw new \RuntimeException(
44
                "Expected path to rule-file as first parameter.");
45
        }
46
47
        $ruleset = $this->load_rules_file($params[1]);
48
49
        // drop programm name and rule file path
50
        array_shift($params);
51
        array_shift($params);
52
53
        $configs = array();
54
        $this->load_configs($params, $configs);
55
56
        $dic = $this->create_dic($ruleset, $configs);
57
58
        $dic["engine"]->run();
59
    }
60
61
    /**
62
     * Load rules and initial config from a *.php-file.
63
     *
64
     * @param   string  $path
65
     * @return  array   ($ruleset, $config)
66
     */
67 4
    protected function load_rules_file($path) {
68 4
        if (!file_exists($path)) {
69
            throw new \RuntimeException("Unknown rule-file '$path'");
70
        }
71 4
        $ruleset = $this->rule_loader->load_rules_from($path);
72 4
        return $ruleset;
73
    }
74
75
    /**
76
     * Load extra configs from yaml files.
77
     *
78
     * @param   array   $config_file_paths
79
     * @param   array   &$configs_array
80
     * @return  null
81
     */
82 3
    protected function load_configs(array $config_file_paths, array &$configs_array) {
83 3
        foreach ($config_file_paths as $config_file) {
84 3
            if (!file_exists($config_file)) {
85
                throw new \RuntimeException("Unknown config-file '$config_file'");
86
            }
87 3
            $configs_array[] = Yaml::parse(file_get_contents($config_file));
88 3
        }
89 3
    }
90
91
    /**
92
     * Create and initialize the DI-container.
93
     *
94
     * @param   RuleSet     $ruleset
95
     * @param   array       &$configs
96
     * @return  Container
97
     */
98 3
    protected function create_dic(RuleSet $ruleset, array &$configs) {
99 3
        $container = new Container();
100
101
        $container["config"] = function ($c) use (&$configs) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102 2
            return new Config($configs);
103
        };
104
105 3
        $container["ruleset"] = $ruleset;
106
107
        $container["engine"] = function($c) {
108
            return new Engine
109 1
                ( $c["log"]
110 1
                , $c["config"]
111 1
                , $c["database_factory"]
112 1
                , $c["indexer_factory"]
113 1
                , $c["analyzer_factory"]
114 1
                , $c["source_status"]
115 1
                );
116
        };
117
118
        $container["log"] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119 2
            return new CLILogger();
120
        };
121
122
        $container["database_factory"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123 1
            return new DBFactory();
124
        };
125
126
        $container["indexer_factory"] = function($c) {
127
            return new \Lechimp\Dicto\Indexer\IndexerFactory
128 2
                ( $c["log"]
129 2
                , $c["php_parser"]
130 2
                , array
131 2
                    ( new \Lechimp\Dicto\Rules\ContainText()
132 2
                    , new \Lechimp\Dicto\Rules\DependOn()
133 2
                    , new \Lechimp\Dicto\Rules\Invoke()
134 2
                    )
135 2
                );
136
        };
137
138
        $container["analyzer_factory"] = function($c) {
139
            return new \Lechimp\Dicto\Analysis\AnalyzerFactory
140 1
                ( $c["log"]
141 1
                , $c["ruleset"]
142 1
                );
143
        };
144
145
        $container["php_parser"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146 2
            return (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
147
        };
148
149
        $container["report_generator"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
            return new CLIReportGenerator();
151
        };
152
153 2
        $container["source_status"] = function($c) {
154 2
            return new SourceStatusGit($c["config"]->project_root());
155
        };
156
157 3
        return $container;
158
    }
159
}
160