-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-main-stub.php
131 lines (106 loc) · 3.22 KB
/
update-main-stub.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
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
<?php
define('BASE_NAME', 'uart');
try {
// hold a reference to the script name
$self = basename($argv[0]);
// list of stub files
$stubFiles = [];
$di = new DirectoryIterator(__DIR__);
foreach ($di as $item) {
// ensure that the current item is a file
if ($item->isFile() === false) {
continue;
}
// ensure that the current item is not this script
if ($item->getFilename() === $self) {
continue;
}
// ensure that only php files are being processed
if (substr($item->getFilename(), -4) !== '.php') {
continue;
}
$stubFiles[$item->getFilename()] = $item->getPathname();
}
// open a temporary file to be used during content processing
$tmpFile = tempnam(sys_get_temp_dir(), sprintf('php%s', BASE_NAME));
$handle = fopen($tmpFile, 'w');
assert(
is_resource($handle),
new RuntimeException('Failed to open temporary file!')
);
// write stub header
fileHeader($handle);
// guarantee the final stub generated will be consistent
ksort($stubFiles);
// global namespace
$namespace = '';
foreach ($stubFiles as $fileName => $stubFile) {
echo 'Processing ', $fileName, PHP_EOL;
// extract stub file content
$content = file($stubFile, FILE_IGNORE_NEW_LINES);
// write content to temporary file
fileContent($handle, $content, $namespace);
}
// close temporary file
fclose($handle);
// move temporary file to final stub file
assert(
rename($tmpFile, __DIR__ . sprintf('/../%s.stub.php', BASE_NAME)),
new RuntimeException('Failed to write stub file!')
);
} catch (Exception $exception) {
echo 'Error! ', $exception->getMessage(), PHP_EOL;
}
function fileHeader($handle): void {
fwrite($handle, '<?php');
fwrite($handle, "\n");
fwrite($handle, '/** @generate-function-entries */');
fwrite($handle, "\n");
}
function fileContent($handle, array $content, string &$namespace): void {
assert(
$content[0] === '<?php',
new RuntimeException('Trying to process a non-php file!')
);
$lineCnt = count($content);
$clsName = '';
$matches = [];
for ($j = 1; $j < $lineCnt; $j++) {
// namespace definition
if (preg_match('/^namespace ([a-zA-Z0-9_\\-]+);$/', $content[$j], $matches)) {
if ($namespace === $matches[1]) {
// skip "redefinition" of the same namespace
continue;
}
$namespace = $matches[1];
echo ' > new namespace: ', $namespace, PHP_EOL;
fwrite($handle, sprintf('namespace %s;', $namespace));
fwrite($handle, "\n");
continue;
}
// class definition
if (preg_match('/^ *(abstract|final|) *class ([a-zA-Z0-9_]+)/', $content[$j], $matches)) {
$clsName = $matches[2];
echo ' > new class: ', $clsName, PHP_EOL;
fwrite($handle, $content[$j]);
fwrite($handle, "\n");
continue;
}
// function definition that returns self
if (strpos($content[$j], 'function') !== false && strpos($content[$j], 'self') !== false) {
fwrite(
$handle,
str_replace(
'): self {',
sprintf('): %s {', $clsName),
$content[$j]
)
);
fwrite($handle, "\n");
continue;
}
// ordinary line
fwrite($handle, $content[$j]);
fwrite($handle, "\n");
}
}