-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateConstants.php
More file actions
94 lines (71 loc) · 2.55 KB
/
generateConstants.php
File metadata and controls
94 lines (71 loc) · 2.55 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
<?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 = [];
$constantNames = [];
foreach ($xml->xpath('//ns:import/ns:options/ns:option') as $option) {
$name = \strtoupper(\str_replace(['.', ':'], '_', (string)$option['name']));
$defaultValue = (string)$option->defaultvalue;
$optionType = (string)$option->optiontype;
$constantNames[] = $name;
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");
$phpstanPath = 'phpstan.neon.dist';
if (!empty($constantNames) && \file_exists($phpstanPath)) {
$phpstan = \file_get_contents($phpstanPath);
if ($phpstan === false) {
exit(0);
}
$dynamicConstantNames = \array_values(\array_unique($constantNames));
$dynamicBlock = " dynamicConstantNames:\n";
foreach ($dynamicConstantNames as $name) {
$dynamicBlock .= " - {$name}\n";
}
$pattern = '/^\s*dynamicConstantNames:\s*\n(?:\s*-\s*.*\n)*/m';
if (\preg_match($pattern, $phpstan)) {
$phpstan = \preg_replace($pattern, $dynamicBlock, $phpstan);
} else {
$lines = \preg_split("/\r\n|\n|\r/", $phpstan);
$insertAfter = null;
foreach ($lines as $index => $line) {
if (\preg_match('/^\s*level:\s*/', $line)) {
$insertAfter = $index;
break;
}
}
if ($insertAfter === null) {
foreach ($lines as $index => $line) {
if (\trim($line) === 'parameters:') {
$insertAfter = $index;
break;
}
}
}
$blockLines = \explode("\n", \rtrim($dynamicBlock, "\n"));
if ($insertAfter === null) {
$lines = \array_merge($blockLines, $lines);
} else {
\array_splice($lines, $insertAfter + 1, 0, $blockLines);
}
$phpstan = \implode("\n", $lines);
}
\file_put_contents($phpstanPath, \rtrim($phpstan) . "\n");
}
echo "constants.php has been generated successfully.";