1
|
|
|
<?php namespace Comodojo\Dispatcher\Components; |
2
|
|
|
|
3
|
|
|
use \Symfony\Component\Yaml\Yaml; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Comodojo Dispatcher |
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
8
|
|
|
* @author Marco Castiello <[email protected]> |
9
|
|
|
* @license GPL-3.0+ |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
16
|
|
|
* License, or (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Affero General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class Configuration { |
29
|
|
|
|
30
|
|
|
protected $attributes = array(); |
31
|
|
|
|
32
|
|
|
public function __construct( $configuration = array() ) { |
33
|
|
|
|
34
|
|
|
$this->attributes = array_merge($this->attributes, $configuration); |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
final public function get($property = null) { |
39
|
|
|
|
40
|
|
|
if ( is_null($property) ) { |
41
|
|
|
|
42
|
|
|
return $this->attributes; |
43
|
|
|
|
44
|
|
|
} else if (array_key_exists($property, $this->attributes)) { |
45
|
|
|
|
46
|
|
|
$value = $this->attributes[$property]; |
47
|
|
|
|
48
|
|
|
// substitution by backreference is cool but hard compute for a large set of values :( |
49
|
|
|
// |
50
|
|
|
// if ( is_scalar($value) && preg_match_all('/%(.+?)%/', $value, $matches, PREG_SET_ORDER) ) { |
|
|
|
|
51
|
|
|
// |
52
|
|
|
// $substitutions = array(); |
|
|
|
|
53
|
|
|
// |
54
|
|
|
// foreach ( $matches as $match ) { |
|
|
|
|
55
|
|
|
// |
56
|
|
|
// $backreference = $match[1]; |
|
|
|
|
57
|
|
|
// |
58
|
|
|
// if ( $backreference != $property && !isset($substitutions['/%'.$backreference.'%/']) ) { |
|
|
|
|
59
|
|
|
// |
60
|
|
|
// $substitutions['/%'.$backreference.'%/'] = $this->get($backreference); |
|
|
|
|
61
|
|
|
// |
62
|
|
|
// } |
63
|
|
|
// |
64
|
|
|
// } |
65
|
|
|
// |
66
|
|
|
// $value = preg_replace(array_keys($substitutions), array_values($substitutions), $value); |
|
|
|
|
67
|
|
|
// |
68
|
|
|
// } |
69
|
|
|
|
70
|
|
|
return $value; |
71
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
|
74
|
|
|
return null; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
final public function set($property, $value) { |
81
|
|
|
|
82
|
|
|
$this->attributes[$property] = $value; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
final public function isDefined($property) { |
89
|
|
|
|
90
|
|
|
return isset($this->attributes[$property]); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
final public function delete($property = null) { |
95
|
|
|
|
96
|
|
|
if ( is_null($property) ) { |
97
|
|
|
|
98
|
|
|
$this->attributes = array(); |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
|
102
|
|
|
} else if ( $this->isDefined($property) ) { |
103
|
|
|
|
104
|
|
|
unset($this->attributes[$property]); |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
final public function merge($properties) { |
117
|
|
|
|
118
|
|
|
$this->attributes = array_replace($this->attributes, $properties); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function loadFromYaml($yaml) { |
125
|
|
|
|
126
|
|
|
$configuration = Yaml::parse($yaml); |
127
|
|
|
|
128
|
|
|
return $this->merge($configuration); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.