Skip to content

Commit 42adaad

Browse files
committed
Check for php-cgi on instantiation, and die with message if unable to collect version information.
Add RuntimeException catch to application for case of failures.
1 parent 546d606 commit 42adaad

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/Application.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ public function run()
166166
)
167167
);
168168
return 1;
169+
} catch(\RuntimeException $e) {
170+
$container
171+
->get(OutputInterface::class)
172+
->printError(
173+
sprintf(
174+
'%s',
175+
$e->getMessage()
176+
)
177+
);
178+
return 1;
169179
}
170180
return $exitCode;
171181
}

src/ExerciseRunner/CgiRunner.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ class CgiRunner implements ExerciseRunnerInterface
4343
*/
4444
public function __construct(CgiExercise $exercise, EventDispatcher $eventDispatcher)
4545
{
46-
// Suppress shell output. Anything non-zero is a failure.
47-
@system('php-cgi --version > /dev/null 2>&1', $failedToRun);
48-
if ($failedToRun) {
49-
die('Could not load php-cgi binary. Please install php-cgi using your package manager.' . PHP_EOL);
46+
if (strpos(PHP_OS, 'WIN') !== false) {
47+
// Check if in path. 2> nul > nul equivalent to 2>&1 /dev/null
48+
$silence = (PHP_OS == 'CYGWIN' ? '> /dev/null 2>&1' : '2> nul > nul');
49+
system(sprintf('php-cgi --version %s', $silence), $failedToRun);
50+
if ($failedToRun) {
51+
$newPath = realpath(sprintf('%s/%s', dirname(PHP_BINARY), 'php-cgi.exe'));
52+
// Try one more time, relying on being in the php binary's directory (where it should be on Windows)
53+
system(sprintf('%s --version %s', $newPath, $silence), $stillFailedToRun);
54+
if ($stillFailedToRun) {
55+
throw new \RuntimeException('Could not load php-cgi binary. Please install php-cgi using your package manager.');
56+
}
57+
}
58+
} else {
59+
@system('php-cgi --version > /dev/null 2>&1', $failedToRun);
60+
if ($failedToRun) {
61+
throw new \RuntimeException('Could not load php-cgi binary. Please install php-cgi using your package manager.');
62+
}
5063
}
5164
$this->eventDispatcher = $eventDispatcher;
5265
$this->exercise = $exercise;

0 commit comments

Comments
 (0)