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
|
|
|
* Build the configuration from nested arrays using a processor. |
33
|
|
|
* |
34
|
|
|
* @param string $path |
35
|
|
|
*/ |
36
|
9 |
|
public function __construct($path, array $values) { |
37
|
9 |
|
assert('is_string($path)'); |
38
|
9 |
|
if (substr($path, strlen($path) - 1, 1) == "/") { |
39
|
1 |
|
$path = substr($path, 0, strlen($path) - 1); |
40
|
2 |
|
} |
41
|
9 |
|
$this->path = $path; |
42
|
9 |
|
$processor = new \Symfony\Component\Config\Definition\Processor(); |
43
|
9 |
|
$this->values = $processor->processConfiguration($this, $values); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Definition of configuration for symfony. |
48
|
|
|
* |
49
|
|
|
* @inheritdocs |
50
|
|
|
*/ |
51
|
9 |
|
public function getConfigTreeBuilder() { |
52
|
9 |
|
$tree_builder = new TreeBuilder(); |
53
|
9 |
|
$root = $tree_builder->root("dicto"); |
54
|
|
|
$root |
55
|
9 |
|
->children() |
56
|
9 |
|
->arrayNode("project") |
57
|
9 |
|
->children() |
58
|
9 |
|
->scalarNode("root") |
59
|
9 |
|
->isRequired() |
60
|
9 |
|
->end() |
61
|
9 |
|
->scalarNode("storage") |
62
|
9 |
|
->isRequired() |
63
|
9 |
|
->end() |
64
|
9 |
|
->scalarNode("rules") |
65
|
9 |
|
->isRequired() |
66
|
9 |
|
->end() |
67
|
9 |
|
->end() |
68
|
9 |
|
->end() |
69
|
9 |
|
->arrayNode("analysis") |
70
|
9 |
|
->children() |
71
|
9 |
|
->arrayNode("ignore") |
72
|
9 |
|
->prototype("scalar") |
73
|
9 |
|
->end() |
74
|
9 |
|
->defaultValue(array()) |
75
|
9 |
|
->end() |
76
|
9 |
|
->end() |
77
|
9 |
|
->addDefaultsIfNotSet() |
78
|
9 |
|
->end() |
79
|
9 |
|
->end() |
80
|
9 |
|
->end(); |
81
|
|
|
|
82
|
9 |
|
return $tree_builder; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
4 |
|
public function path() { |
89
|
4 |
|
return $this->path; |
90
|
|
|
} |
91
|
|
|
|
92
|
9 |
|
protected function maybe_prepend_path($path) { |
93
|
9 |
|
assert('is_string($path)'); |
94
|
9 |
|
if (substr($path, 0, 2) === "./") { |
95
|
3 |
|
return $this->path()."/".substr($path, 2); |
96
|
|
|
} |
97
|
6 |
|
return $path; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
4 |
|
public function project_rules() { |
104
|
4 |
|
return $this->maybe_prepend_path($this->values["project"]["rules"]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
8 |
|
public function project_root() { |
111
|
8 |
|
return $this->maybe_prepend_path($this->values["project"]["root"]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
7 |
|
public function project_storage() { |
118
|
7 |
|
return $this->maybe_prepend_path($this->values["project"]["storage"]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string[] |
123
|
|
|
*/ |
124
|
5 |
|
public function analysis_ignore() { |
125
|
5 |
|
return $this->values["analysis"]["ignore"]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|