This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-main-stub.php
97 lines (77 loc) · 2.26 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
<?php
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(), 'phpspi');
$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);
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);
}
// close temporary file
fclose($handle);
// move temporary file to final stub file
assert(
rename($tmpFile, __DIR__ . '/../phpspi.stub.php'),
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\n");
fwrite($handle, '/** @generate-function-entries */');
fwrite($handle, "\n\n");
fwrite($handle, 'namespace SPI;');
fwrite($handle, "\n\n");
}
function fileContent($handle, array $content): void {
assert(
$content[0] === '<?php',
new RuntimeException('Trying to process a non-php file!')
);
$lines = count($content);
$marker = false;
for ($i = 1; $i < $lines; $i++) {
if (strpos($content[$i], 'namespace') !== false) {
$marker = true;
continue;
}
if ($marker && $content[$i] !== '') {
break;
}
}
fwrite($handle, implode("\n", array_slice($content, $i)));
fwrite($handle, "\n\n");
}