Skip to content

Commit 9586d9d

Browse files
authored
Merge pull request #303 from iMattPro/fixes
Fix output streaming
2 parents 516c316 + e75ec8a commit 9586d9d

13 files changed

Lines changed: 309 additions & 119 deletions

‎bin/qi‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ if (PHP_VERSION_ID < 80000)
99
}
1010

1111
require __DIR__ . '/../src/QuickInstall/Sandbox/Application.php';
12+
require __DIR__ . '/../src/QuickInstall/Sandbox/Output.php';
13+
require __DIR__ . '/../src/QuickInstall/Sandbox/StreamOutput.php';
14+
require __DIR__ . '/../src/QuickInstall/Sandbox/BufferedOutput.php';
15+
require __DIR__ . '/../src/QuickInstall/Sandbox/ProcessRunner.php';
1216
require __DIR__ . '/../src/QuickInstall/Sandbox/CommandLine.php';
1317
require __DIR__ . '/../src/QuickInstall/Sandbox/Project.php';
1418
require __DIR__ . '/../src/QuickInstall/Sandbox/VersionMatrix.php';

‎src/QuickInstall/Sandbox/Application.php‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function init(): int
128128

129129
private function sourceList(): int
130130
{
131-
$sources = (new SourceService($this->project))->list();
131+
$sources = (new SourceService($this->project, $this->sandboxOutput()))->list();
132132

133133
if (!$sources)
134134
{
@@ -163,7 +163,7 @@ private function sourceRemove(array $args): int
163163
throw new InvalidArgumentException('Usage: qi source:remove <version|source> [--force]');
164164
}
165165

166-
$removed = (new SourceService($this->project))->remove($version, $cli->has('force'));
166+
$removed = (new SourceService($this->project, $this->sandboxOutput()))->remove($version, $cli->has('force'));
167167
echo "Removed source: {$removed['source']['source_key']}\n";
168168
if (!empty($removed['used_by']))
169169
{
@@ -175,7 +175,7 @@ private function sourceRemove(array $args): int
175175

176176
private function sourcePrune(): int
177177
{
178-
$removed = (new SourceService($this->project))->prune();
178+
$removed = (new SourceService($this->project, $this->sandboxOutput()))->prune();
179179
if (!$removed)
180180
{
181181
echo "No unused sources to prune\n";
@@ -199,7 +199,7 @@ private function sourceFetch(array $args): int
199199
throw new InvalidArgumentException('Usage: qi source:fetch <version|branch> [--git] [--url URL] [--allow-external]');
200200
}
201201

202-
$record = (new SourceService($this->project))->fetch($version, $cli->has('git'), $cli->option('url'), $cli->has('allow-external'));
202+
$record = (new SourceService($this->project, $this->sandboxOutput()))->fetch($version, $cli->has('git'), $cli->option('url'), $cli->has('allow-external'));
203203

204204
echo "Fetched phpBB source: {$record['path']}\n";
205205
return 0;
@@ -217,7 +217,7 @@ private function phpbbList(): int
217217
$row['resolves_to'],
218218
$row['notes'],
219219
];
220-
}, (new SourceService($this->project))->supportedVersions())
220+
}, (new SourceService($this->project, $this->sandboxOutput()))->supportedVersions())
221221
);
222222

223223
return 0;
@@ -240,7 +240,7 @@ private function boardCreate(array $args): int
240240
$debug = $cli->has('debug');
241241
$this->validateBoardCreateOptions($db, $port, $populate);
242242

243-
$created = (new BoardService($this->project))->create($name, $version, $db, $port, $populate, $debug, $cli->has('replace'));
243+
$created = (new BoardService($this->project, $this->sandboxOutput()))->create($name, $version, $db, $port, $populate, $debug, $cli->has('replace'));
244244
$paths = $created['paths'];
245245

246246
echo "Created board scaffold: $name\n";
@@ -266,7 +266,7 @@ private function boardCreate(array $args): int
266266

267267
private function boardList(): int
268268
{
269-
$boards = (new BoardService($this->project))->list();
269+
$boards = (new BoardService($this->project, $this->sandboxOutput()))->list();
270270
if (!$boards)
271271
{
272272
echo "No boards created\n";
@@ -408,7 +408,7 @@ private function supportsAnsi(): bool
408408
private function boardStart(array $args): int
409409
{
410410
$name = $this->boardName($args, 'Usage: qi board:start <name>');
411-
$board = (new BoardService($this->project))->start($name);
411+
$board = (new BoardService($this->project, $this->sandboxOutput()))->start($name);
412412
echo "Started board: $name\n";
413413
echo "URL: {$board['url']}\n";
414414
return 0;
@@ -417,15 +417,15 @@ private function boardStart(array $args): int
417417
private function boardStop(array $args): int
418418
{
419419
$name = $this->boardName($args, 'Usage: qi board:stop <name>');
420-
(new BoardService($this->project))->stop($name);
420+
(new BoardService($this->project, $this->sandboxOutput()))->stop($name);
421421
echo "Stopped board: $name\n";
422422
return 0;
423423
}
424424

425425
private function boardDestroy(array $args): int
426426
{
427427
$name = $this->boardName($args, 'Usage: qi board:destroy <name>');
428-
(new BoardService($this->project))->destroy($name);
428+
(new BoardService($this->project, $this->sandboxOutput()))->destroy($name);
429429
echo "Destroyed board: $name\n";
430430
return 0;
431431
}
@@ -452,7 +452,7 @@ private function boardSeed(array $args): int
452452
}
453453
$action = $cli->has('reset') ? 'reset' : ($cli->has('replace') ? 'replace' : 'seed');
454454

455-
(new BoardService($this->project))->seed($name, $preset, $seed, $action);
455+
(new BoardService($this->project, $this->sandboxOutput()))->seed($name, $preset, $seed, $action);
456456
echo ucfirst($action) . " completed for board: $name\n";
457457
return 0;
458458
}
@@ -538,7 +538,7 @@ private function extUnmount(array $args): int
538538

539539
private function refreshBoardIfRunning(string $board): void
540540
{
541-
$runner = new BoardRunner($this->project);
541+
$runner = new BoardRunner($this->project, $this->sandboxOutput());
542542
(new DockerComposeWriter($this->project))->write($board, $this->runtimeConfig($this->project->board($board)));
543543
if ($runner->status($board) === 'running')
544544
{
@@ -677,6 +677,12 @@ private function writeError(string $message): void
677677
fwrite($this->stderr, $message);
678678
}
679679

680+
private function sandboxOutput(): Output
681+
{
682+
$stdout = defined('STDOUT') && defined('STDERR') && $this->stderr === STDERR ? STDOUT : fopen('php://output', 'w');
683+
return new StreamOutput($stdout, $this->stderr);
684+
}
685+
680686
private function mountResources(string $type, object $manager, string $board, string $source, bool $copy, bool $recursive, bool $allowExternal): int
681687
{
682688
if ($recursive)

‎src/QuickInstall/Sandbox/BoardRunner.php‎

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@
1616
class BoardRunner
1717
{
1818
private Project $project;
19+
private Output $output;
20+
private ProcessRunner $processRunner;
1921

20-
public function __construct(Project $project)
22+
public function __construct(Project $project, ?Output $output = null, ?ProcessRunner $processRunner = null)
2123
{
2224
$this->project = $project;
25+
$this->output = $output ?: new BufferedOutput();
26+
$this->processRunner = $processRunner ?: new ProcessRunner($this->output);
2327
}
2428

2529
public function start(string $name): void
2630
{
2731
$board = $this->project->board($name);
2832
$this->run(['docker', 'compose', '-f', $this->project->composePath($name), 'up', '--build', '-d', '--force-recreate', '--remove-orphans', 'web']);
29-
echo "Waiting for phpBB install to finish...\n";
33+
$this->output->write("Waiting for phpBB install to finish...\n");
3034
$this->waitUntilInstalled($name);
3135
if (!empty($board['debug']))
3236
{
33-
echo "Enabling phpBB debug config...\n";
37+
$this->output->write("Enabling phpBB debug config...\n");
3438
$this->enableDebug($name);
3539
}
3640
if (($board['db'] ?? '') === 'sqlite' && ($board['populate'] ?? 'none') !== 'none')
@@ -141,7 +145,7 @@ protected function seedIfNeeded(string $name, string $preset): void
141145
$marker = $this->seedMarker($name, $preset);
142146
if (file_exists($marker))
143147
{
144-
echo "Populate preset already applied: $preset\n";
148+
$this->output->write("Populate preset already applied: $preset\n");
145149
return;
146150
}
147151

@@ -289,7 +293,7 @@ protected function waitUntilHttpReady(string $name, string $url): void
289293
return;
290294
}
291295

292-
echo "Waiting for board URL...\n";
296+
$this->output->write("Waiting for board URL...\n");
293297
$deadline = time() + 30;
294298
while (time() <= $deadline)
295299
{
@@ -302,8 +306,8 @@ protected function waitUntilHttpReady(string $name, string $url): void
302306
usleep(500000);
303307
}
304308

305-
echo "Warning: board container started, but $url was not reachable from the host after 30 seconds.\n";
306-
echo "Try opening it again in a few seconds, or run: docker compose -f " . $this->project->composePath($name) . " logs web\n";
309+
$this->output->write("Warning: board container started, but $url was not reachable from the host after 30 seconds.\n");
310+
$this->output->write("Try opening it again in a few seconds, or run: docker compose -f " . $this->project->composePath($name) . " logs web\n");
307311
}
308312

309313
protected function httpStatus(string $url): int
@@ -347,7 +351,7 @@ protected function runSeeder(string $name, string $preset, int $seed, string $ac
347351
$writer = new SeederWriter($this->project);
348352
$script = $writer->write($name);
349353

350-
echo "Running seed preset: $preset\n";
354+
$this->output->write("Running seed preset: $preset\n");
351355
$this->run(['docker', 'compose', '-f', $this->project->composePath($name), 'cp', $script, 'web:/tmp/qi_seed.php']);
352356
$this->run(['docker', 'compose', '-f', $this->project->composePath($name), 'exec', '-T', 'web', 'timeout', '300', 'php', '/tmp/qi_seed.php', $preset, (string) $seed, $action]);
353357
}
@@ -373,50 +377,12 @@ protected function writeSeedMarker(string $name, string $preset): void
373377

374378
protected function run(array $command): void
375379
{
376-
echo '$ ' . implode(' ', array_map('escapeshellarg', $command)) . "\n";
377-
378-
$descriptor = [
379-
0 => ['file', '/dev/null', 'r'],
380-
1 => defined('STDOUT') ? constant('STDOUT') : ['file', 'php://output', 'w'],
381-
2 => defined('STDERR') ? constant('STDERR') : ['file', 'php://stderr', 'w'],
382-
];
383-
384-
$process = proc_open($command, $descriptor, $pipes);
385-
if (!is_resource($process))
386-
{
387-
throw new RuntimeException('Unable to start command: ' . $command[0]);
388-
}
389-
390-
$status = proc_close($process);
391-
if ($status !== 0)
392-
{
393-
throw new RuntimeException("Command failed with exit code $status: {$command[0]}" . $this->commandHint($command, $status));
394-
}
380+
$this->processRunner->run($command);
395381
}
396382

397383
protected function capture(array $command): array
398384
{
399-
$descriptor = [
400-
0 => ['file', '/dev/null', 'r'],
401-
1 => ['pipe', 'w'],
402-
2 => ['pipe', 'w'],
403-
];
404-
405-
$process = proc_open($command, $descriptor, $pipes);
406-
if (!is_resource($process))
407-
{
408-
return ['exit_code' => 1, 'output' => ''];
409-
}
410-
411-
$output = stream_get_contents($pipes[1]) ?: '';
412-
$error = stream_get_contents($pipes[2]) ?: '';
413-
fclose($pipes[1]);
414-
fclose($pipes[2]);
415-
416-
return [
417-
'exit_code' => proc_close($process),
418-
'output' => $output . $error,
419-
];
385+
return $this->processRunner->capture($command);
420386
}
421387

422388
protected function lines(string $output): array
@@ -426,18 +392,4 @@ protected function lines(string $output): array
426392
}));
427393
}
428394

429-
protected function commandHint(array $command, int $status): string
430-
{
431-
if ($status === 124 && in_array('timeout', $command, true))
432-
{
433-
return "\nThe operation timed out. For seeding, try a smaller preset or use mariadb/mysql/postgres instead of sqlite.";
434-
}
435-
436-
if (($command[0] ?? '') === 'docker')
437-
{
438-
return "\nCheck that Docker Desktop is running and that the docker command works in this terminal.";
439-
}
440-
441-
return '';
442-
}
443395
}

‎src/QuickInstall/Sandbox/BoardService.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
class BoardService
1717
{
1818
private Project $project;
19+
private ?Output $output;
1920

20-
public function __construct(Project $project)
21+
public function __construct(Project $project, ?Output $output = null)
2122
{
2223
$this->project = $project;
24+
$this->output = $output;
2325
}
2426

2527
public function create(string $name, string $version = 'latest', string $db = 'mariadb', int $port = 8080, string $populate = 'none', bool $debug = false, bool $replace = false): array
@@ -153,12 +155,12 @@ public function seed(string $name, string $preset, int $seed, string $action): v
153155

154156
protected function createBoardRunner(): BoardRunner
155157
{
156-
return new BoardRunner($this->project);
158+
return new BoardRunner($this->project, $this->output);
157159
}
158160

159161
protected function createSourceProvider(): SourceProvider
160162
{
161-
return new SourceProvider($this->project);
163+
return new SourceProvider($this->project, $this->output);
162164
}
163165

164166
protected function createDockerComposeWriter(): DockerComposeWriter
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
*
4+
* QuickInstall sandbox buffered output
5+
*
6+
* @copyright (c) 2026 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace QuickInstall\Sandbox;
12+
13+
class BufferedOutput implements Output
14+
{
15+
private string $output = '';
16+
private string $errorOutput = '';
17+
18+
public function write(string $message): void
19+
{
20+
$this->output .= $message;
21+
}
22+
23+
public function error(string $message): void
24+
{
25+
$this->errorOutput .= $message;
26+
}
27+
28+
public function output(): string
29+
{
30+
return $this->output;
31+
}
32+
33+
public function errorOutput(): string
34+
{
35+
return $this->errorOutput;
36+
}
37+
38+
public function all(): string
39+
{
40+
return $this->output . $this->errorOutput;
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
*
4+
* QuickInstall sandbox output
5+
*
6+
* @copyright (c) 2026 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace QuickInstall\Sandbox;
12+
13+
interface Output
14+
{
15+
public function write(string $message): void;
16+
17+
public function error(string $message): void;
18+
}

0 commit comments

Comments
 (0)