-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathbuild.php
More file actions
77 lines (65 loc) · 2.14 KB
/
Copy pathbuild.php
File metadata and controls
77 lines (65 loc) · 2.14 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
<?php
// combine src and vendor directories into a single file using phpfilemerger
$mergerUrl = 'https://raw.githubusercontent.com/mevdschee/phpfilemerger/refs/heads/main/phpfilemerger.php';
$mergerFile = __DIR__ . '/phpfilemerger.php';
$header = <<<EOF
<?php
/**
* PHP-CRUD-API v2 License: MIT
* Maurits van der Schee: maurits@vdschee.nl
* https://github.com/mevdschee/php-crud-api
*
* Dependencies:
* - vendor/psr/*: PHP-FIG
* https://github.com/php-fig
* - vendor/nyholm/*: Tobias Nyholm
* https://github.com/Nyholm
**/
EOF;
function download(string $url, string $file)
{
echo "downloading phpfilemerger.php\n";
$data = file_get_contents($url);
if ($data === false) {
fwrite(STDERR, "failed to download $url\n");
exit(1);
}
file_put_contents($file, $data);
}
function merge(string $merger, string $entry, string $output, bool $excludeEntry)
{
// run from this directory so phpfilemerger auto-detects the project root
// from composer.json and keeps the inlined file paths relative
$command = sprintf(
'%s %s merge %s --output %s%s --quiet',
escapeshellarg(PHP_BINARY),
escapeshellarg($merger),
escapeshellarg($entry),
escapeshellarg($output),
$excludeEntry ? ' --exclude-entry' : ''
);
passthru($command, $status);
if ($status !== 0) {
fwrite(STDERR, "phpfilemerger failed for $output\n");
exit($status);
}
}
function replaceHeader(string $file, string $header)
{
// swap phpfilemerger's generated header for the project header
$data = file_get_contents($file);
$end = strpos($data, "*/");
$data = $header . substr($data, $end + 2);
file_put_contents($file, $data);
}
chdir(__DIR__);
if (!file_exists($mergerFile)) {
download($mergerUrl, $mergerFile);
}
$start = microtime(true);
merge($mergerFile, 'src/index.php', 'api.php', false);
merge($mergerFile, 'src/index.php', 'api.include.php', true);
replaceHeader('api.php', $header);
replaceHeader('api.include.php', $header);
$time = (microtime(true) - $start) * 1000;
echo sprintf("combined into 'api.php' and 'api.include.php' in %d ms\n", $time);