Completed
Push — master ( 31785d...5192f1 )
by Richard
06:45
created

Config::analysis_ignore()   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
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
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 Symfony\Component\Config\Definition\Processor;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
17
/**
18
 * Configuration for the app and engine.
19
 */
20
class Config implements ConfigurationInterface {
21
    /**
22
     * @var string
23
     */
24
    protected $path;
25
26
    /**
27
     * @var array
28
     */
29
    protected $values;
30
31
    /**
32
     * @var array
33
     */
34
    protected $defaults =
35
        [ "analysis" =>
36
            [ "ignore"  => []
37
            ]
38
        , "rules" =>
39
            [ "schemas" =>
40
                [ \Lechimp\Dicto\Rules\DependOn::class
41
                , \Lechimp\Dicto\Rules\Invoke::class
42
                , \Lechimp\Dicto\Rules\ContainText::class
43
                ]
44
            , "properties" =>
45
                [ \Lechimp\Dicto\Variables\Name::class
46
                , \Lechimp\Dicto\Variables\In::class
47
                ]
48
            , "variables" =>
49
                [ \Lechimp\Dicto\Variables\Classes::class
50
                , \Lechimp\Dicto\Variables\Functions::class
51
                , \Lechimp\Dicto\Variables\Globals::class
52
                , \Lechimp\Dicto\Variables\Files::class
53
                , \Lechimp\Dicto\Variables\Methods::class
54
                , \Lechimp\Dicto\Variables\ErrorSuppressor::class
55
                , \Lechimp\Dicto\Variables\Exit_::class
56
                , \Lechimp\Dicto\Variables\Die_::class
57
                ]
58
            ]
59
        , "runtime" =>
60
            [ "check_assertions" => false
61
            ]
62
        ];
63
64
    /**
65
     * Build the configuration from nested arrays using a processor.
66
     *
67
     * @param   string  $path
68
     */
69 14
    public function __construct($path, array $values) {
70 14
        assert('is_string($path)');
71 14
        if (substr($path, strlen($path) - 1, 1) == "/") {
72 1
            $path = substr($path, 0, strlen($path) - 1);
73 1
        }
74 14
        $this->path = $path;
75 14
        $processor = new \Symfony\Component\Config\Definition\Processor();
76 14
        $values = array_merge([$this->defaults], $values);
77 14
        $this->values = $processor->processConfiguration($this, $values);
78 14
    }
79
80
    /**
81
     * Definition of configuration for symfony.
82
     *
83
     * @inheritdocs
84
     */
85 14
    public function getConfigTreeBuilder() {
86
        // TODO: maybe change definition in a way that does not append
87
        //       to rules.*-arrays
88 14
        $tree_builder = new TreeBuilder();
89 14
        $root = $tree_builder->root("dicto");
90
        $root
91 14
            ->children()
92 14
                ->arrayNode("project")
93 14
                    ->children()
94 14
                        ->scalarNode("root")
95 14
                            ->isRequired()
96 14
                        ->end()
97 14
                        ->scalarNode("storage")
98 14
                            ->isRequired()
99 14
                        ->end()
100 14
                        ->scalarNode("rules")
101 14
                            ->isRequired()
102 14
                        ->end()
103 14
                    ->end()
104 14
                ->end()
105 14
                ->arrayNode("analysis")
106 14
                    ->children()
107 14
                        ->arrayNode("ignore")
108 14
                            ->prototype("scalar")
109 14
                            ->end()
110 14
                            ->isRequired()
111 14
                        ->end()
112 14
                    ->end()
113 14
                ->end()
114 14
                ->arrayNode("rules")
115 14
                    ->children()
116 14
                        ->arrayNode("schemas")
117 14
                            ->prototype("scalar")
118 14
                            ->end()
119 14
                            ->isRequired()
120 14
                        ->end()
121 14
                        ->arrayNode("properties")
122 14
                            ->prototype("scalar")
123 14
                            ->end()
124 14
                            ->isRequired()
125 14
                        ->end()
126 14
                        ->arrayNode("variables")
127 14
                            ->prototype("scalar")
128 14
                            ->end()
129 14
                            ->isRequired()
130 14
                        ->end()
131 14
                    ->end()
132 14
                ->end()
133 14
                ->arrayNode("runtime")
134 14
                    ->children()
135 14
                        ->booleanNode("check_assertions")
136 14
                            ->isRequired()
137 14
                        ->end()
138 14
                    ->end()
139 14
                ->end()
140 14
            ->end()
141 14
        ->end();
142
143 14
        return $tree_builder;
144
    }
145
146
    /**
147
     * @return  string
148
     */
149 4
    public function path() {
150 4
        return $this->path;
151
    }
152
153 9
    protected function maybe_prepend_path($path) {
154 9
        assert('is_string($path)');
155 9
        if (substr($path, 0, 2) === "./") {
156 3
            return $this->path()."/".substr($path, 2);
157
        }
158 6
        return $path;
159
    }
160
161
    /**
162
     * @return  string
163
     */
164 4
    public function project_rules() {
165 4
        return $this->maybe_prepend_path($this->values["project"]["rules"]);
166
    }
167
168
    /**
169
     * @return  string
170
     */
171 8
    public function project_root() {
172 8
        return $this->maybe_prepend_path($this->values["project"]["root"]);
173
    }
174
175
    /**
176
     * @return  string
177
     */
178 7
    public function project_storage() {
179 7
        return $this->maybe_prepend_path($this->values["project"]["storage"]);
180
    }
181
182
    /**
183
     * @return  string[]
184
     */
185 6
    public function analysis_ignore() {
186 6
        return $this->values["analysis"]["ignore"];
187
    }
188
189
    /**
190
     * @return  string[]
191
     */
192 3
    public function rules_schemas() {
193 3
        return $this->values["rules"]["schemas"];
194
    }
195
196
    /**
197
     * @return  string[]
198
     */
199 3
    public function rules_properties() {
200 3
        return $this->values["rules"]["properties"];
201
    }
202
203
    /**
204
     * @return  string[]
205
     */
206 3
    public function rules_variables() {
207 3
        return $this->values["rules"]["variables"];
208
    }
209
210
    /**
211
     * @return  bool
212
     */
213 2
    public function runtime_check_assertions() {
214 2
        return $this->values["runtime"]["check_assertions"];
215
    }
216
}
217