Completed
Push — master ( b16d12...7a3fe6 )
by Richard
04:07
created

Config::runtime_check_assertions()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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\Report;
14
15
use Symfony\Component\Config\Definition\Processor;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
19
/**
20
 * Configuration for the app and engine.
21
 */
22
class Config implements ConfigurationInterface {
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * @var array
30
     */
31
    protected $values;
32
33
    /**
34
     * @var Report\Config[]|null
35
     */
36
    protected $reports = null;
37
38
    /**
39
     * @var array
40
     */
41
    protected $defaults =
42
        [ "analysis" =>
43
            [ "ignore"  => []
44
            , "store_index" => false
45
            , "report_stdout" => true 
46
            , "report_database" => true
47
            ]
48
        , "rules" =>
49
            [ "schemas" =>
50
                [ \Lechimp\Dicto\Rules\DependOn::class
51
                , \Lechimp\Dicto\Rules\Invoke::class
52
                , \Lechimp\Dicto\Rules\ContainText::class
53
                ]
54
            , "properties" =>
55
                [ \Lechimp\Dicto\Variables\Name::class
56
                , \Lechimp\Dicto\Variables\In::class
57
                ]
58
            , "variables" =>
59
                [ \Lechimp\Dicto\Variables\Namespaces::class
60
                , \Lechimp\Dicto\Variables\Classes::class
61
                , \Lechimp\Dicto\Variables\Interfaces::class
62
                , \Lechimp\Dicto\Variables\Traits::class
63
                , \Lechimp\Dicto\Variables\Functions::class
64
                , \Lechimp\Dicto\Variables\Globals::class
65
                , \Lechimp\Dicto\Variables\Files::class
66
                , \Lechimp\Dicto\Variables\Methods::class
67
                , \Lechimp\Dicto\Variables\ErrorSuppressor::class
68
                , \Lechimp\Dicto\Variables\Exit_::class
69
                , \Lechimp\Dicto\Variables\Die_::class
70
                , \Lechimp\Dicto\Variables\Eval_::class
71
                ]
72
            ]
73
        , "runtime" =>
74
            [ "check_assertions" => false
75
            ]
76
        ];
77
78
    /**
79
     * Build the configuration from nested arrays using a processor.
80
     *
81
     * @param   string  $path
82
     */
83 31
    public function __construct($path, array $values) {
84 31
        assert('is_string($path)');
85 31
        if (substr($path, strlen($path) - 1, 1) == "/") {
86 1
            $path = substr($path, 0, strlen($path) - 1);
87 1
        }
88 31
        $this->path = $path;
89 31
        $processor = new \Symfony\Component\Config\Definition\Processor();
90 31
        $values = array_merge([$this->defaults], $values);
91 31
        $this->values = $processor->processConfiguration($this, $values);
92 31
    }
93
94
    /**
95
     * Definition of configuration for symfony.
96
     *
97
     * @inheritdocs
98
     */
99 31
    public function getConfigTreeBuilder() {
100
        // TODO: maybe change definition in a way that does not append
101
        //       to rules.*-arrays
102 31
        $tree_builder = new TreeBuilder();
103 31
        $root = $tree_builder->root("dicto");
104
        $root
105 31
            ->children()
106 31
                ->arrayNode("project")
107 31
                    ->children()
108 31
                        ->scalarNode("root")
109 31
                            ->isRequired()
110 31
                        ->end()
111 31
                        ->scalarNode("storage")
112 31
                            ->isRequired()
113 31
                        ->end()
114 31
                        ->scalarNode("rules")
115 31
                            ->isRequired()
116 31
                        ->end()
117 31
                    ->end()
118 31
                ->end()
119 31
                ->arrayNode("analysis")
120 31
                    ->children()
121 31
                        ->arrayNode("ignore")
122 31
                            ->prototype("scalar")
123 31
                            ->end()
124 31
                            ->isRequired()
125 31
                        ->end()
126 31
                        ->booleanNode("store_index")
127 31
                            ->isRequired()
128 31
                        ->end()
129 31
                        ->booleanNode("report_stdout")
130 31
                            ->isRequired()
131 31
                        ->end()
132 31
                        ->booleanNode("report_database")
133 31
                            ->isRequired()
134 31
                        ->end()
135 31
                    ->end()
136 31
                ->end()
137 31
                ->arrayNode("rules")
138 31
                    ->children()
139 31
                        ->arrayNode("schemas")
140 31
                            ->prototype("scalar")
141 31
                            ->end()
142 31
                            ->isRequired()
143 31
                        ->end()
144 31
                        ->arrayNode("properties")
145 31
                            ->prototype("scalar")
146 31
                            ->end()
147 31
                            ->isRequired()
148 31
                        ->end()
149 31
                        ->arrayNode("variables")
150 31
                            ->prototype("scalar")
151 31
                            ->end()
152 31
                            ->isRequired()
153 31
                        ->end()
154 31
                    ->end()
155 31
                ->end()
156 31
                ->arrayNode("runtime")
157 31
                    ->children()
158 31
                        ->booleanNode("check_assertions")
159 31
                            ->isRequired()
160 31
                        ->end()
161 31
                    ->end()
162 31
                ->end()
163 31
                ->arrayNode("reports")
164 31
                    ->prototype("array")
165 31
                        ->children()
166 31
                            ->scalarNode("name")
167 31
                                ->defaultValue(null)
168 31
                            ->end()
169 31
                            ->scalarNode("class")
170 31
                                ->isRequired()
171 31
                            ->end()
172 31
                            ->scalarNode("target")
173 31
                                ->isRequired()
174 31
                            ->end()
175 31
                            ->scalarNode("source")
176 31
                                ->defaultValue(null)
177 31
                            ->end()
178 31
                            ->variableNode("config")
179 31
                                ->defaultValue([])
180 31
                            ->end()
181 31
                        ->end()
182 31
                    ->end()
183 31
                ->end()
184 31
            ->end()
185 31
        ->end();
186
187 31
        return $tree_builder;
188
    }
189
190
    /**
191
     * @return  string
192
     */
193 5
    public function path() {
194 5
        return $this->path;
195
    }
196
197 16
    protected function maybe_prepend_path($path) {
198 16
        assert('is_string($path)');
199 16
        if (substr($path, 0, 2) === "./") {
200 3
            return $this->path()."/".substr($path, 2);
201
        }
202 13
        return $path;
203
    }
204
205
    /**
206
     * @return  string
207
     */
208 4
    public function project_rules() {
209 4
        return $this->maybe_prepend_path($this->values["project"]["rules"]);
210
    }
211
212
    /**
213
     * @return  string
214
     */
215 12
    public function project_root() {
216 12
        return $this->maybe_prepend_path($this->values["project"]["root"]);
217
    }
218
219
    /**
220
     * @return  string
221
     */
222 14
    public function project_storage() {
223 14
        return $this->maybe_prepend_path($this->values["project"]["storage"]);
224
    }
225
226
    /**
227
     * @return  string[]
228
     */
229 10
    public function analysis_ignore() {
230 10
        return $this->values["analysis"]["ignore"];
231
    }
232
233
    /**
234
     * @return  bool
235
     */
236 9
    public function analysis_store_index() {
237 9
        return $this->values["analysis"]["store_index"];
238
    }
239
240
    /**
241
     * @return  bool
242
     */
243 5
    public function analysis_report_stdout() {
244 5
        return $this->values["analysis"]["report_stdout"];
245
    }
246
247
    /**
248
     * @return  bool
249
     */
250 5
    public function analysis_report_database() {
251 5
        return $this->values["analysis"]["report_database"];
252
    }
253
254
    /**
255
     * @return  string[]
256
     */
257 3
    public function rules_schemas() {
258 3
        return $this->values["rules"]["schemas"];
259
    }
260
261
    /**
262
     * @return  string[]
263
     */
264 3
    public function rules_properties() {
265 3
        return $this->values["rules"]["properties"];
266
    }
267
268
    /**
269
     * @return  string[]
270
     */
271 3
    public function rules_variables() {
272 3
        return $this->values["rules"]["variables"];
273
    }
274
275
    /**
276
     * @return  bool
277
     */
278 2
    public function runtime_check_assertions() {
279 2
        return $this->values["runtime"]["check_assertions"];
280
    }
281
282
    /**
283
     * @return Report\Config[]
284
     */
285 2
    public function reports() {
286 2
        if ($this->reports !== null) {
287 2
            return $this->reports;
288
        }
289 1
        $this->reports = [];
290 1
        foreach ($this->values["reports"] as $rep) {
291 1
            $this->reports[] = new Report\Config
292 1
                ( $this->path()
293 1
                , $rep["class"]
294 1
                , $rep["target"]
295 1
                , $rep["config"]
296 1
                , $rep["name"]
297 1
                , $rep["source"]
298 1
                );
299 1
        }
300 1
        return $this->reports;
301
    }
302
}
303