|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Config; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Config\Entry\Generic; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Config section |
|
8
|
|
|
* |
|
9
|
|
|
* @property Entry\Boolean enable |
|
10
|
|
|
* @property Entry\Boolean allowoverridebinpath |
|
11
|
|
|
* @property Entry\Boolean allowoverridechroot |
|
12
|
|
|
* @property Entry\Boolean allowoverrideuser |
|
13
|
|
|
* @property Entry\Boolean allowoverridegroup |
|
14
|
|
|
* @property Entry\Boolean allowoverridecwd |
|
15
|
|
|
* @property Entry\Generic cwd |
|
16
|
|
|
* @property Entry\Generic indexfiles |
|
17
|
|
|
* @property Entry\Generic mastersocket |
|
18
|
|
|
* @package Core |
|
19
|
|
|
* @subpackage Config |
|
20
|
|
|
* |
|
21
|
|
|
* @author Vasily Zorin <[email protected]> |
|
22
|
|
|
* @dynamic_fields |
|
23
|
|
|
*/ |
|
24
|
|
|
class Section implements \ArrayAccess |
|
25
|
|
|
{ |
|
26
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
27
|
|
|
|
|
28
|
|
|
/** @var */ |
|
29
|
|
|
public $source; |
|
30
|
|
|
/** @var */ |
|
31
|
|
|
public $revision; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor |
|
35
|
|
|
* @param hash |
|
36
|
|
|
* @return object |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($arr = []) |
|
39
|
|
|
{ |
|
40
|
|
|
foreach ($arr as $k => $v) { |
|
41
|
|
|
if (!is_object($v)) { |
|
42
|
|
|
$e = new Generic; |
|
43
|
|
|
$e->setHumanValue($v); |
|
44
|
|
|
$this->{$k} = $e; |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->{$k} = $v; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Count elements |
|
53
|
|
|
* @return integer |
|
54
|
|
|
*/ |
|
55
|
|
|
public function count() |
|
56
|
|
|
{ |
|
57
|
|
|
return count($this) - 2; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* toArray handler |
|
62
|
|
|
* @return hash |
|
|
|
|
|
|
63
|
|
|
*/ |
|
64
|
|
|
public function toArray() |
|
65
|
|
|
{ |
|
66
|
|
|
$arr = []; |
|
67
|
|
|
foreach ($this as $k => $entry) { |
|
|
|
|
|
|
68
|
|
|
if (!$entry instanceof Generic) { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
$arr[$k] = $entry->value; |
|
72
|
|
|
} |
|
73
|
|
|
return $arr; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Checks if property exists |
|
78
|
|
|
* @param string Property name |
|
79
|
|
|
* @return boolean Exists? |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
public function offsetExists($prop) |
|
83
|
|
|
{ |
|
84
|
|
|
$prop = $this->getRealPropertyName($prop); |
|
85
|
|
|
return property_exists($this, $prop); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get real property name |
|
90
|
|
|
* @param string Property name |
|
91
|
|
|
* @return string Real property name |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getRealPropertyName($prop) |
|
94
|
|
|
{ |
|
95
|
|
|
return str_replace('-', '', strtolower($prop)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get property by name |
|
100
|
|
|
* @param string Property name |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public function offsetGet($prop) |
|
104
|
|
|
{ |
|
105
|
|
|
$prop = $this->getRealPropertyName($prop); |
|
106
|
|
|
return isset($this->{$prop}) ? $this->{$prop}->value : null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set property |
|
111
|
|
|
* @param string Property name |
|
112
|
|
|
* @param mixed Value |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function offsetSet($prop, $value) |
|
116
|
|
|
{ |
|
117
|
|
|
$prop = $this->getRealPropertyName($prop); |
|
118
|
|
|
$this->{$prop} = $value; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Unset property |
|
123
|
|
|
* @param string Property name |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function offsetUnset($prop) |
|
127
|
|
|
{ |
|
128
|
|
|
$prop = $this->getRealPropertyName($prop); |
|
129
|
|
|
unset($this->{$prop}); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Impose default config |
|
134
|
|
|
* @param array {"setting": "value"} |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function imposeDefault($settings = []) |
|
138
|
|
|
{ |
|
139
|
|
|
foreach ($settings as $name => $value) { |
|
140
|
|
|
$name = strtolower(str_replace('-', '', $name)); |
|
141
|
|
|
if (!isset($this->{$name})) { |
|
142
|
|
|
if (is_scalar($value)) { |
|
143
|
|
|
$this->{$name} = new Generic($value); |
|
144
|
|
|
} else { |
|
145
|
|
|
$this->{$name} = $value; |
|
146
|
|
|
} |
|
147
|
|
|
} elseif ($value instanceof Section) { |
|
148
|
|
|
$value->imposeDefault($value); |
|
|
|
|
|
|
149
|
|
|
} else { |
|
150
|
|
|
$current = $this->{$name}; |
|
151
|
|
|
if (!is_object($value)) { |
|
152
|
|
|
$this->{$name} = new Generic($value); |
|
153
|
|
|
} else { |
|
154
|
|
|
$this->{$name} = $value; |
|
155
|
|
|
} |
|
156
|
|
|
$this->{$name}->setHumanValue($current->value); |
|
157
|
|
|
$this->{$name}->source = $current->source; |
|
158
|
|
|
$this->{$name}->revision = $current->revision; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.