-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateConstants.php
More file actions
132 lines (106 loc) · 3.99 KB
/
generateConstants.php
File metadata and controls
132 lines (106 loc) · 3.99 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
<?php
/*
* Copyright by SoftCreatR.dev.
*
* License: https://softcreatr.dev/license-terms
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* The above copyright notice and this disclaimer notice shall be included in all
* copies or substantial portions of the Software.
*/
$phpHeader = <<<'PHP'
<?php
/*
* Copyright by SoftCreatR.dev.
*
* License: https://softcreatr.dev/license-terms
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* The above copyright notice and this disclaimer notice shall be included in all
* copies or substantial portions of the Software.
*/
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', $phpHeader . "\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.";