-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateConstants.php
38 lines (28 loc) · 1.01 KB
/
generateConstants.php
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
<?php
if (!\file_exists('option.xml')) {
echo "option.xml not found.";
exit(0);
}
$xmlString = \file_get_contents('option.xml');
try {
$xml = new SimpleXMLElement($xmlString);
} catch (Exception $e) {
exit(0);
}
$namespaces = $xml->getNamespaces(true);
$xml->registerXPathNamespace('ns', $namespaces['']);
$constants = ["const WCF_N = 1;"];
foreach ($xml->xpath('//ns:option') as $option) {
$name = \strtoupper(\str_replace(['.', ':'], '_', (string)$option['name']));
$defaultValue = (string)$option->defaultvalue;
$optionType = (string)$option->optiontype;
if ($defaultValue === '') {
$constants[] = "const {$name} = '';";
} elseif ($optionType === 'boolean' || $optionType === 'integer') {
$constants[] = "const {$name} = " . (int)$defaultValue . ";";
} else {
$constants[] = "const {$name} = '{$defaultValue}';";
}
}
\file_put_contents('constants.php', "<?php\n\n" . \implode("\n", $constants) . "\n");
echo "constants.php has been generated successfully.";