Completed
Push — master ( e64760...31785d )
by Richard
05:42
created

Config::rules_properties()   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 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
        ];
60
61
    /**
62
     * Build the configuration from nested arrays using a processor.
63
     *
64
     * @param   string  $path
65
     */
66 13
    public function __construct($path, array $values) {
67 13
        assert('is_string($path)');
68 13
        if (substr($path, strlen($path) - 1, 1) == "/") {
69 1
            $path = substr($path, 0, strlen($path) - 1);
70 1
        }
71 13
        $this->path = $path;
72 13
        $processor = new \Symfony\Component\Config\Definition\Processor();
73 13
        $values = array_merge([$this->defaults], $values);
74 13
        $this->values = $processor->processConfiguration($this, $values);
75 13
    }
76
77
    /**
78
     * Definition of configuration for symfony.
79
     *
80
     * @inheritdocs
81
     */
82 13
    public function getConfigTreeBuilder() {
83
        // TODO: maybe change definition in a way that does not append
84
        //       to rules.*-arrays
85 13
        $tree_builder = new TreeBuilder();
86 13
        $root = $tree_builder->root("dicto");
87
        $root
88 13
            ->children()
89 13
                ->arrayNode("project")
90 13
                    ->children()
91 13
                        ->scalarNode("root")
92 13
                            ->isRequired()
93 13
                        ->end()
94 13
                        ->scalarNode("storage")
95 13
                            ->isRequired()
96 13
                        ->end()
97 13
                        ->scalarNode("rules")
98 13
                            ->isRequired()
99 13
                        ->end()
100 13
                    ->end()
101 13
                ->end()
102 13
                ->arrayNode("analysis")
103 13
                    ->children()
104 13
                        ->arrayNode("ignore")
105 13
                            ->prototype("scalar")
106 13
                            ->end()
107 13
                            ->isRequired()
108 13
                        ->end()
109 13
                    ->end()
110 13
                ->end()
111 13
                ->arrayNode("rules")
112 13
                    ->children()
113 13
                        ->arrayNode("schemas")
114 13
                            ->prototype("scalar")
115 13
                            ->end()
116 13
                            ->isRequired()
117 13
                        ->end()
118 13
                        ->arrayNode("properties")
119 13
                            ->prototype("scalar")
120 13
                            ->end()
121 13
                            ->isRequired()
122 13
                        ->end()
123 13
                        ->arrayNode("variables")
124 13
                            ->prototype("scalar")
125 13
                            ->end()
126 13
                            ->isRequired()
127 13
                        ->end()
128 13
                    ->end()
129 13
                ->end()
130 13
            ->end()
131 13
        ->end();
132
133 13
        return $tree_builder;
134
    }
135
136
    /**
137
     * @return  string
138
     */
139 4
    public function path() {
140 4
        return $this->path;
141
    }
142
143 9
    protected function maybe_prepend_path($path) {
144 9
        assert('is_string($path)');
145 9
        if (substr($path, 0, 2) === "./") {
146 3
            return $this->path()."/".substr($path, 2);
147
        }
148 6
        return $path;
149
    }
150
151
    /**
152
     * @return  string
153
     */
154 4
    public function project_rules() {
155 4
        return $this->maybe_prepend_path($this->values["project"]["rules"]);
156
    }
157
158
    /**
159
     * @return  string
160
     */
161 8
    public function project_root() {
162 8
        return $this->maybe_prepend_path($this->values["project"]["root"]);
163
    }
164
165
    /**
166
     * @return  string
167
     */
168 7
    public function project_storage() {
169 7
        return $this->maybe_prepend_path($this->values["project"]["storage"]);
170
    }
171
172
    /**
173
     * @return  string[]
174
     */
175 6
    public function analysis_ignore() {
176 6
        return $this->values["analysis"]["ignore"];
177
    }
178
179
    /**
180
     * @return  string[]
181
     */
182 3
    public function rules_schemas() {
183 3
        return $this->values["rules"]["schemas"];
184
    }
185
186
    /**
187
     * @return  string[]
188
     */
189 3
    public function rules_properties() {
190 3
        return $this->values["rules"]["properties"];
191
    }
192
193
    /**
194
     * @return  string[]
195
     */
196 3
    public function rules_variables() {
197 3
        return $this->values["rules"]["variables"];
198
    }
199
}
200