Repository navigation
Expand file tree
/
Copy pathspawn-program.php
More file actions
67 lines (60 loc) · 1.97 KB
/
Copy pathspawn-program.php
File metadata and controls
67 lines (60 loc) · 1.97 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
<?php
/**
* In-process sugar-ssh-server example that mounts a SugarCraft Program
* inside a sugar-pty subprocess (the in-process equivalent of the
* pre-PTY-upgrade BubbleTea + HostSshd setup).
*
* Two pieces:
*
* 1. This file (the supervisor) — registers Spawn middleware
* with a factory that returns the path to a tiny wrapper
* script that runs the actual Program.
*
* 2. `run-program.php` (the wrapper) — a few lines that
* autoload sugar-core, instantiate the user's Model, and
* call `(new Program($model))->run()`. Lives next to this
* file in production deployments.
*
* The wrapper runs as a subprocess under the sugar-pty PTY, so
* Ctrl+C / Ctrl+Z / SIGWINCH all behave correctly via the
* controlling-terminal claim. The supervisor middleware layer
* (Logger, Auth, RateLimit) runs in the parent, before the
* wrapper is exec'd — same composition model as the legacy
* BubbleTea pattern, just with process isolation.
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\SshServer\Middleware\Logger;
use SugarCraft\SshServer\Middleware\Spawn;
use SugarCraft\SshServer\Server;
use SugarCraft\SshServer\Session;
$wrapper = __DIR__ . '/run-program.php';
exit(Server::new()
->use(new Logger())
->use(new Spawn(fn (Session $s) => [
'cmd' => [PHP_BINARY, $wrapper, $s->user, (string) $s->cols, (string) $s->rows],
'env' => [
'TERM' => $s->term,
'LANG' => $s->lang,
'PATH' => '/usr/bin:/bin',
],
]))
->serve());
/*
* Example wrapper (`run-program.php`):
*
* ----------
* #!/usr/bin/env php
* <?php
* declare(strict_types=1);
*
* require __DIR__ . '/../vendor/autoload.php';
*
* use SugarCraft\Core\Program;
*
* [$user, $cols, $rows] = [$argv[1] ?? 'guest', (int) ($argv[2] ?? 80), (int) ($argv[3] ?? 24)];
*
* $model = new MyApp(user: $user, cols: $cols, rows: $rows);
* (new Program($model))->run();
* ----------
*/