-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathConfiguration.php
More file actions
166 lines (149 loc) · 3.81 KB
/
Configuration.php
File metadata and controls
166 lines (149 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Supervisor\Configuration;
use Supervisor\Configuration\Section\SectionInterface;
/**
* Supervisor configuration parser and generator.
*
* @author Márk Sági-Kazár <[email protected]>
*/
class Configuration
{
/**
* Available sections.
*
* @var array
*/
protected $sectionMap = [
'eventlistener' => Section\EventListener::class,
'fcgi-program' => Section\FcgiProgram::class,
'group' => Section\Group::class,
'include' => Section\Includes::class,
'inet_http_server' => Section\InetHttpServer::class,
'program' => Section\Program::class,
'supervisorctl' => Section\Supervisorctl::class,
'supervisord' => Section\Supervisord::class,
'unix_http_server' => Section\UnixHttpServer::class,
'rpcinterface' => Section\RpcInterface::class,
];
/**
* Config sections.
*
* @var SectionInterface[]
*/
protected $sections = [];
/**
* Returns a specific section by name.
*
* @param string $section
*
* @return SectionInterface|null
*/
public function getSection(string $section): ?SectionInterface
{
if ($this->hasSection($section)) {
return $this->sections[$section];
}
return null;
}
/**
* Checks whether section exists in Configuration.
*
* @param string $section
*
* @return bool
*/
public function hasSection(string $section): bool
{
return array_key_exists($section, $this->sections);
}
/**
* Adds or overrides a section.
*
* @param SectionInterface $section
*/
public function addSection(SectionInterface $section): void
{
$this->sections[$section->getName()] = $section;
}
/**
* Removes a section by name.
*
* @param string $section
*
* @return bool Whether the section existed before this function call.
*/
public function removeSection($section): bool
{
if ($has = $this->hasSection($section)) {
unset($this->sections[$section]);
}
return $has;
}
/**
* Returns all sections.
*
* @return SectionInterface[]
*/
public function getSections(): array
{
return $this->sections;
}
/**
* Adds or overrides an array sections.
*
* @param SectionInterface[] $sections
*/
public function addSections(array $sections): void
{
foreach ($sections as $section) {
$this->addSection($section);
}
}
/**
* Resets Configuration.
*/
public function reset(): void
{
$this->sections = [];
}
/**
* Converts the configuration to array.
*
* @return array
*/
public function toArray(): array
{
$ini = [];
foreach ($this->sections as $sectionName => $section) {
$ini[$sectionName] = $section->getProperties();
}
return $ini;
}
/**
* Adds or overrides a default section mapping.
*
* @param string $section
* @param string $className
*/
public function mapSection($section, $className): void
{
if (false === class_exists($className)) {
throw new \InvalidArgumentException('This section class does not exist');
}
if (false === is_a($className, SectionInterface::class, true)) {
throw new \InvalidArgumentException('This section class must implement Supervisor\Configuration\Section');
}
$this->sectionMap[$section] = $className;
}
/**
* Finds a section class by name.
*
* @param string $section
*
* @return string|bool
*/
public function findSection($section)
{
return $this->sectionMap[$section] ?? false;
}
}