Completed
Push — master ( 00392a...f7852e )
by Richard
06:17
created

App   C

Complexity

Total Complexity 9

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Test Coverage

Coverage 79.73%

Importance

Changes 16
Bugs 0 Features 2
Metric Value
c 16
b 0
f 2
dl 0
loc 155
wmc 9
lcom 1
cbo 23
ccs 59
cts 74
cp 0.7973
rs 5.5

5 Methods

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