1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\Skylab\Provider; |
4
|
|
|
|
5
|
|
|
use Cilex\Application; |
6
|
|
|
use Kunstmaan\Skylab\Entity\PermissionDefinition; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* ProjectConfigProvider |
10
|
|
|
*/ |
11
|
|
|
class ProjectConfigProvider extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Registers services on the given app. |
16
|
|
|
* |
17
|
|
|
* @param Application $app An Application instance |
18
|
|
|
*/ |
19
|
|
|
public function register(Application $app) |
20
|
|
|
{ |
21
|
|
|
$app['projectconfig'] = $this; |
22
|
|
|
$this->app = $app; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $projectname The project name |
27
|
|
|
* @return \ArrayObject |
28
|
|
|
*/ |
29
|
|
|
public function loadProjectConfig($projectname) |
30
|
|
|
{ |
31
|
|
|
$config = new \ArrayObject(); |
32
|
|
|
$config = $this->loadConfig($projectname, $config); |
33
|
|
|
$config = $this->loadOwnership($projectname, $config); |
34
|
|
|
$config = $this->loadPermissions($projectname, $config); |
35
|
|
|
$config = $this->loadBackup($projectname, $config); |
36
|
|
|
|
37
|
|
|
return $config; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $projectname |
42
|
|
|
* @param $config |
43
|
|
|
* @return \ArrayObject |
44
|
|
|
*/ |
45
|
|
|
private function loadConfig($projectname, \ArrayObject $config) |
46
|
|
|
{ |
47
|
|
|
$configPath = $this->fileSystemProvider->getProjectConfigDirectory($projectname) . "/config.xml"; |
48
|
|
|
if (file_exists($configPath.".local")){ |
49
|
|
|
$configPath = $configPath.".local"; |
50
|
|
|
} |
51
|
|
|
$xml = simplexml_load_file($configPath); |
52
|
|
|
foreach ($xml->{'var'} as $var) { |
53
|
|
|
$tag = (string) $var["name"]; |
54
|
|
|
switch ($tag) { |
55
|
|
|
case "project.skeletons": |
56
|
|
|
foreach ($var->{'item'} as $skel) { |
57
|
|
|
$config["skeletons"][(string) $skel["value"]] = (string) $skel["value"]; |
58
|
|
|
} |
59
|
|
|
break; |
60
|
|
|
case "project.aliases": |
61
|
|
|
foreach ($var->{'item'} as $alias) { |
62
|
|
|
$config["aliases"][] = (string) $alias["value"]; |
63
|
|
|
} |
64
|
|
|
break; |
65
|
|
|
default: |
66
|
|
|
$config[str_replace("project.", "", $tag)] = (string) $var["value"]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $config; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $projectname |
75
|
|
|
* @param \ArrayObject $config |
76
|
|
|
* @return \ArrayObject |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
private function loadOwnership($projectname, \ArrayObject $config) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$configPath = $this->fileSystemProvider->getProjectConfigDirectory($projectname) . "/ownership.xml"; |
81
|
|
|
$xml = simplexml_load_file($configPath); |
82
|
|
|
foreach ($xml->{'var'} as $var) { |
83
|
|
|
$name = (string) $var["name"]; |
84
|
|
|
$value = (string) $var["value"]; |
85
|
|
|
if (isset($config["permissions"][$name])) { |
86
|
|
|
$permissionDefinition = $config["permissions"][$name]; |
87
|
|
|
} else { |
88
|
|
|
$permissionDefinition = new PermissionDefinition(); |
89
|
|
|
} |
90
|
|
|
$permissionDefinition->setPath($name); |
91
|
|
|
$permissionDefinition->setOwnership($value); |
92
|
|
|
$config["permissions"][$name] = $permissionDefinition; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $config; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $value |
100
|
|
|
* @param \ArrayObject $config |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function searchReplacer($value, \ArrayObject $config) |
104
|
|
|
{ |
105
|
|
|
$replaceDictionary = new \ArrayObject(array( |
106
|
|
|
"config.superuser" => $this->app["config"]["users"]["superuser"], |
107
|
|
|
"config.supergroup" => $this->app["config"]["users"]["supergroup"], |
108
|
|
|
"config.wwwuser" => $this->app["config"]["users"]["wwwuser"], |
109
|
|
|
"project.group" => $config["name"], |
110
|
|
|
"project.user" => $config["name"], |
111
|
|
|
"project.ip" => "*", |
112
|
|
|
"project.url" => (isset($config["url"])?$config["url"]:""), |
113
|
|
|
"project.admin" => $this->app["config"]["apache"]["admin"], |
114
|
|
|
"project.dir" => $config["dir"], |
115
|
|
|
"config.projectsdir" => $this->app["config"]["projects"]["path"], |
116
|
|
|
"project.name" => $config["name"], |
117
|
|
|
"project.statsurl" => (isset($config["url"])?"stats." . $config["url"]:""), |
118
|
|
|
"project.port" => $this->app["config"]["nginx"]["port"], |
119
|
|
|
"project.rootpath" => "", |
120
|
|
|
"config.postgresuser" => $this->app["config"]["users"]["postgresuser"], |
121
|
|
|
"config.hostmachine" => $this->app["config"]["webserver"]["hostmachine"], |
122
|
|
|
)); |
123
|
|
|
|
124
|
|
|
preg_match_all("/@(\w*?\.\w*?)@/", $value, $hits); |
125
|
|
|
foreach ($hits[0] as $index => $hit) { |
126
|
|
|
$value = str_replace($hit, $replaceDictionary[$hits[1][$index]], $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $projectname |
134
|
|
|
* @param \ArrayObject $config |
135
|
|
|
* @return \ArrayObject |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
private function loadPermissions($projectname, \ArrayObject $config) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$configPath = $this->fileSystemProvider->getProjectConfigDirectory($projectname) . "/permissions.xml"; |
140
|
|
|
$xml = simplexml_load_file($configPath); |
141
|
|
|
foreach ($xml->{'var'} as $var) { |
142
|
|
|
$name = (string) $var["name"]; |
143
|
|
|
if (isset($config["permissions"][$name])) { |
144
|
|
|
$permissionDefinition = $config["permissions"][$name]; |
145
|
|
|
} else { |
146
|
|
|
$permissionDefinition = new PermissionDefinition(); |
147
|
|
|
} |
148
|
|
|
$permissionDefinition->setPath($name); |
149
|
|
|
foreach ($var->{'item'} as $item) { |
150
|
|
|
$value = (string) $item["value"]; |
151
|
|
|
$permissionDefinition->addAcl($value); |
152
|
|
|
} |
153
|
|
|
$config["permissions"][$name] = $permissionDefinition; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $config; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $projectname |
161
|
|
|
* @param \ArrayObject $config |
162
|
|
|
* @return \ArrayObject |
163
|
|
|
*/ |
164
|
|
|
private function loadBackup($projectname, \ArrayObject $config) |
165
|
|
|
{ |
166
|
|
|
$configPath = $this->fileSystemProvider->getProjectConfigDirectory($projectname) . "/backup.xml"; |
167
|
|
|
$xml = simplexml_load_file($configPath); |
168
|
|
|
foreach ($xml->{'var'}[0]->item as $item) { |
169
|
|
|
$value = (string) $item["value"]; |
170
|
|
|
$config["backupexcludes"][$value] = $value; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $config; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param \ArrayObject $project The project |
178
|
|
|
*/ |
179
|
|
|
public function writeProjectConfig(\ArrayObject $project) |
180
|
|
|
{ |
181
|
|
|
$this->dialogProvider->logTask("Writing configuration files"); |
182
|
|
|
$this->writeConfig($project); |
183
|
|
|
$this->writeOwnership($project); |
184
|
|
|
$this->writePermissions($project); |
185
|
|
|
$this->writeBackup($project); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param \ArrayObject $project |
190
|
|
|
*/ |
191
|
|
|
private function writeConfig(\ArrayObject $project) |
192
|
|
|
{ |
193
|
|
|
$configPath = $this->fileSystemProvider->getProjectConfigDirectory($project["name"]) . "/config.xml"; |
194
|
|
|
$this->dialogProvider->logConfig("Writing the project config to " . $configPath); |
195
|
|
|
$config = new \SimpleXMLElement('<?xml version="1.0" ?><config></config>'); |
196
|
|
|
/* @var $skeletonProvider SkeletonProvider */ |
197
|
|
|
$skeletonProvider = $this->app['skeleton']; |
198
|
|
|
$skels = $config->addChild('var'); |
199
|
|
|
$skels->addAttribute("name", "project.skeletons"); |
200
|
|
|
foreach ($project["skeletons"] as $skeletonname) { |
201
|
|
|
$this->addItem($skels, $skeletonname); |
202
|
|
|
$skeleton = $skeletonProvider->findSkeleton($skeletonname); |
203
|
|
|
if (!is_object($skeleton)) { |
204
|
|
|
$this->dialogProvider->logConfig("Skipping config for " . $skeletonname . " on " . $project["name"]); |
205
|
|
|
continue; |
206
|
|
|
} |
207
|
|
|
$config = $skeleton->writeConfig($project, $config); |
208
|
|
|
} |
209
|
|
|
$this->writeToFile($config, $configPath); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param \SimpleXMLElement $node |
214
|
|
|
* @param string $name |
215
|
|
|
* @param array $items |
216
|
|
|
* @return \SimpleXMLElement |
217
|
|
|
* |
218
|
|
|
*/ |
219
|
|
|
public function addVarWithItems(\SimpleXMLElement $node, $name, array $items) |
220
|
|
|
{ |
221
|
|
|
$var = $node->addChild('var'); |
222
|
|
|
$var->addAttribute("name", $name); |
223
|
|
|
foreach ($items as $value) { |
224
|
|
|
$item = $var->addChild('item'); |
225
|
|
|
$item->addAttribute("value", $value); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $node; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param \SimpleXMLElement $var |
233
|
|
|
* @param string $value |
234
|
|
|
* @return \SimpleXMLElement |
235
|
|
|
*/ |
236
|
|
|
public function addItem(\SimpleXMLElement $var, $value) |
237
|
|
|
{ |
238
|
|
|
$item = $var->addChild('item'); |
239
|
|
|
$item->addAttribute("value", $value); |
240
|
|
|
|
241
|
|
|
return $var; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param $xml |
246
|
|
|
* @param string $path |
247
|
|
|
*/ |
248
|
|
|
private function writeToFile($xml, $path) |
249
|
|
|
{ |
250
|
|
|
$dom = dom_import_simplexml($xml)->ownerDocument; |
251
|
|
|
$dom->preserveWhiteSpace = false; |
252
|
|
|
$dom->formatOutput = true; |
253
|
|
|
$this->fileSystemProvider->writeProtectedFile($path, $dom->saveXML()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param \ArrayObject $project |
258
|
|
|
*/ |
259
|
|
|
private function writeOwnership(\ArrayObject $project) |
260
|
|
|
{ |
261
|
|
|
$ownershipPath = $this->fileSystemProvider->getProjectConfigDirectory($project["name"]) . "/ownership.xml"; |
262
|
|
|
$this->dialogProvider->logConfig("Writing the project's ownership config to " . $ownershipPath); |
263
|
|
|
$ownership = new \SimpleXMLElement('<?xml version="1.0" ?><config></config>'); |
264
|
|
|
/** @var PermissionDefinition $permission */ |
265
|
|
|
foreach ($project["permissions"] as $permission) { |
266
|
|
|
$ownership = $this->addVar($ownership, $permission->getPath(), $permission->getOwnership()); |
267
|
|
|
} |
268
|
|
|
$this->writeToFile($ownership, $ownershipPath); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param \SimpleXMLElement $node |
273
|
|
|
* @param string $name |
274
|
|
|
* @param string $value |
275
|
|
|
* @return \SimpleXMLElement |
276
|
|
|
*/ |
277
|
|
|
public function addVar(\SimpleXMLElement $node, $name, $value) |
278
|
|
|
{ |
279
|
|
|
$var = $node->addChild('var'); |
280
|
|
|
$var->addAttribute("name", $name); |
281
|
|
|
$var->addAttribute("value", $value); |
282
|
|
|
|
283
|
|
|
return $node; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param \ArrayObject $project |
288
|
|
|
*/ |
289
|
|
|
private function writePermissions(\ArrayObject $project) |
290
|
|
|
{ |
291
|
|
|
$permissionsPath = $this->fileSystemProvider->getProjectConfigDirectory($project["name"]) . "/permissions.xml"; |
292
|
|
|
$this->dialogProvider->logConfig("Writing the project's permissions config to " . $permissionsPath); |
293
|
|
|
$permissions = new \SimpleXMLElement('<?xml version="1.0" ?><config></config>'); |
294
|
|
|
/** @var PermissionDefinition $permission */ |
295
|
|
|
foreach ($project["permissions"] as $permission) { |
296
|
|
|
$var = $permissions->addChild('var'); |
297
|
|
|
$var->addAttribute("name", $permission->getPath()); |
298
|
|
|
foreach ($permission->getAcl() as $acl) { |
299
|
|
|
$var = $this->addItem($var, $acl); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
$this->writeToFile($permissions, $permissionsPath); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param \ArrayObject $project |
307
|
|
|
*/ |
308
|
|
|
private function writeBackup(\ArrayObject $project) |
309
|
|
|
{ |
310
|
|
|
$backupPath = $this->fileSystemProvider->getProjectConfigDirectory($project["name"]) . "/backup.xml"; |
311
|
|
|
$this->dialogProvider->logConfig("Writing the project's backup excludes config to " . $backupPath); |
312
|
|
|
$backup = new \SimpleXMLElement('<?xml version="1.0" ?><config></config>'); |
313
|
|
|
$var = $backup->addChild('var'); |
314
|
|
|
$var->addAttribute("name", "backup.excludes"); |
315
|
|
|
foreach ($project["backupexcludes"] as $backupexclude) { |
316
|
|
|
$var = $this->addItem($var, $backupexclude); |
317
|
|
|
} |
318
|
|
|
$this->writeToFile($backup, $backupPath); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.