6 require_once $_composer_autoload_path ?? __DIR__
."/../vendor/autoload.php";
9 fprintf(STDERR
, "Usage: %s <stub.php> [<ns>]\n", $argv[0]);
13 if (!($stub = realpath($argv[1]))) {
19 $namespace = $argv[2];
21 $namespace = basename($stub, ".stub.php");
24 $tmplck = tempnam(sys_get_temp_dir(), "mdref.");
25 $tmpdir = $tmplck . ".d";
26 mkdir($tmpdir) && chdir($tmpdir) ||
exit(-1);
29 $shutdown = function() use($tmpdir, $tmplck, &$running) {
31 chdir(__DIR__
) && rm_r($tmpdir, $tmplck);
33 register_shutdown_function($shutdown);
34 pcntl_signal(SIGINT
, $shutdown, false);
35 pcntl_signal(SIGTERM
, $shutdown, false);
37 $passthru = fn($cmd) => fn() => printf("%s\n", $cmd) && !passthru("$cmd 2>&1 >/dev/null", $rc) && !$rc;
38 $stub2ref = $passthru(
39 sprintf("%s/stub2ref %s %s %s",
41 escapeshellarg($namespace),
42 escapeshellarg($stub),
43 escapeshellarg($tmpdir)
46 $ref2html = $passthru(
47 sprintf("%s/ref2html . .",
51 $update = fn() => $stub2ref() && $ref2html();
54 $ifd = inotify_init();
55 inotify_add_watch($ifd, $stub, IN_MODIFY
);
56 stream_set_blocking($ifd, false);
58 file_put_contents("router.php", file_get_contents(__FILE__
, false, null, __COMPILER_HALT_OFFSET__
));
59 $php = popen(sprintf("%s -S localhost:0 -t . router.php 2>&1 | grep --line-buffered -Ev 'Accepted|Closing|GET'", PHP_BINARY
), "r");
61 stream_set_blocking($php, false);
64 $R = [$ifd, $php]; $W = []; $E = [];
65 if (stream_select($R, $W, $E, null)) {
69 while (($string = fgets($php))) echo $string;
74 while (inotify_read($ifd));
80 pcntl_signal_dispatch();
84 function rm_r(string ...$args) {
85 foreach ($args as $del) {
87 rm_r(...array_map(fn($sub) => "$del/$sub", array_slice(scandir($del), 2)));
89 } else if (file_exists($del)) {
98 $file = __DIR__
. urldecode($_SERVER["REQUEST_URI"]);
100 if (is_file($file)) {
104 if (is_dir($file) && file_exists($file."/index.html")) {
105 readfile($file."/index.html");
107 $file = rtrim($file, "/").".html";
108 if (file_exists($file)) {