-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
executable file
·133 lines (110 loc) · 4.43 KB
/
Copy pathserver.php
File metadata and controls
executable file
·133 lines (110 loc) · 4.43 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
<?php
// ============================================================
// BLOCO 1: garantir que este processo rode com Swoole.
// Deve ser a PRIMEIRA coisa do arquivo, antes de qualquer uso
// de classes Swoole.
// ============================================================
(function () {
if (extension_loaded('swoole')) {
return; // já estamos rodando com Swoole, ok
}
$cwd = __DIR__;
$localPhp = $cwd . '/php';
// Verifica se o ./php local existe e tem Swoole
if (!file_exists($localPhp) || !is_executable($localPhp)) {
fwrite(STDERR, "[server.php] Swoole não encontrado e ./php não está disponível. Instale o Swoole ou adicione-o ao PATH.\n");
exit(1);
}
$hasSwoole = trim((string) shell_exec(escapeshellarg($localPhp) . ' --ri swoole 2>/dev/null'));
if (empty($hasSwoole)) {
fwrite(STDERR, "[server.php] ./php também não possui Swoole. Não é possível continuar.\n");
exit(1);
}
// ---------------------------------------------------------
// Persistência: nas próximas vezes que o usuário entrar
// nesta pasta e rodar "php ...", o ./php local será usado.
// ---------------------------------------------------------
$escapedCwd = str_replace('"', '\\"', $cwd);
$marker = '# filemanager-local-php';
$rcBlock = "\n$marker\n"
. "if [ \"\$PWD\" = \"$escapedCwd\" ] || echo \"\$PWD\" | grep -q \"^$escapedCwd\"; then\n"
. " export PATH=\"$escapedCwd:\$PATH\"\n"
. "fi\n";
// .bashrc / .zshrc / .profile
$home = getenv('HOME') ?: '/root';
foreach (['.bashrc', '.zshrc', '.profile'] as $rc) {
$rcPath = "$home/$rc";
if (file_exists($rcPath)) {
$content = file_get_contents($rcPath);
if (strpos($content, $marker) === false) {
file_put_contents($rcPath, $content . $rcBlock);
}
}
}
// .envrc para quem usa direnv
$envrc = $cwd . '/.envrc';
$envrcLine = "export PATH=\"$escapedCwd:\$PATH\"\n";
if (!file_exists($envrc) || strpos((string) file_get_contents($envrc), $envrcLine) === false) {
file_put_contents($envrc, $envrcLine, FILE_APPEND);
}
fwrite(STDOUT, "[server.php] Sistema PHP sem Swoole. Re-executando com ./php local e persistindo PATH...\n");
// ---------------------------------------------------------
// Re-executa este mesmo script com o ./php local.
// pcntl_exec substitui o processo atual (sem fork).
// ---------------------------------------------------------
if (function_exists('pcntl_exec')) {
pcntl_exec($localPhp, $GLOBALS['argv']);
// se pcntl_exec retornou, algo deu errado — fallback
}
// Fallback: passthru (cria subprocesso filho)
$args = implode(' ', array_map('escapeshellarg', array_slice($GLOBALS['argv'], 0)));
passthru(escapeshellarg($localPhp) . ' ' . $args, $exitCode);
exit($exitCode);
})();
// ============================================================
// BLOCO 2: a partir daqui o Swoole está garantido.
// ============================================================
\Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
use plugins\Start\console as consoleDeclares;
use Swoole\Coroutine as co;
function portAlive(mixed $port): bool
{
$host = "0.0.0.0";
$fp = @fsockopen($host, $port, $errno, $errstr, 5);
var_dump($fp);
if (!$fp) {
return false;
}
fclose($fp);
return true;
}
include 'libspech/plugins/autoloader.php';
\libspech\Cli\cli::pcl("Running Tests...");
\co\run(function () {
global $argv;
if (@$argv[1] !== '--fix')
\libspech\Cli\cli::pcl(shell_exec('php run-tests.php'));
$fixs = 'fixs.json';
if (file_exists($fixs)) {
$r = json_decode(file_get_contents($fixs), true)['fixes'];
print "Running fixes...\n";
foreach ($r as $fix) {
foreach ($fix['commands'] as $command) {
print shell_exec($command);
}
}
}
});
require_once __DIR__ . '/vendor/autoload.php';
include_once 'plugins/autoload.php';
for (; ;) {
print "Starting server...\n";
$sharedPid = null;
$pidRunner = null;
Co\run(function () use (&$sharedPid, &$pidRunner) {
\plugins\terminal::asyncShell('php ' . __DIR__ . "/middleware.php", (new consoleDeclares()), $sharedPid);
});
Co\run(fn() => co::sleep(3));
print "Restarting $sharedPid and $pidRunner...\n";
\plugins\terminal::pKill($sharedPid);
}