Skip to content

Commit 211ec01

Browse files
committed
Allow to expose ports
1 parent 6f461ac commit 211ec01

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/Exercise/Scenario/ExerciseScenario.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ abstract class ExerciseScenario
99
*/
1010
private array $files = [];
1111

12+
/**
13+
* @var list<int>
14+
*/
15+
private array $exposedPorts = [];
16+
1217
public function withFile(string $relativeFileName, string $content): static
1318
{
1419
$this->files[$relativeFileName] = $content;
@@ -23,4 +28,19 @@ public function getFiles(): array
2328
{
2429
return $this->files;
2530
}
31+
32+
public function exposePort(int $port): static
33+
{
34+
$this->exposedPorts = [$port];
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* @return list<int>
41+
*/
42+
public function getExposedPorts(): array
43+
{
44+
return $this->exposedPorts;
45+
}
2646
}

src/ExerciseRunner/CliRunner.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
206206
$scenario = $this->exercise->defineTestScenario();
207207

208208
$this->environmentManager->prepareStudent($context, $scenario);
209+
$this->environmentManager->prepareReference($context, $scenario);
210+
209211

210212
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.run.start', $context, $scenario));
211213

@@ -222,6 +224,7 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
222224
$context->getStudentExecutionDirectory(),
223225
$context->getEntryPoint(),
224226
$args,
227+
$scenario->getExposedPorts()
225228
);
226229

227230
$process->start();
@@ -231,6 +234,7 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
231234
$process->wait(function ($outputType, $outputBuffer) use ($output) {
232235
$output->write($outputBuffer);
233236
});
237+
234238
$output->emptyLine();
235239

236240
if (!$process->isSuccessful()) {
@@ -254,7 +258,7 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
254258
*/
255259
private function executePhpFile(ExecutionContext $context, CliScenario $scenario, string $workingDirectory, string $fileName, Collection $args, string $type): string
256260
{
257-
$process = $this->getPhpProcess($workingDirectory, $fileName, $args);
261+
$process = $this->getPhpProcess($workingDirectory, $fileName, $args, $scenario->getExposedPorts());
258262

259263
$process->start();
260264
$this->eventDispatcher->dispatch(
@@ -272,10 +276,10 @@ private function executePhpFile(ExecutionContext $context, CliScenario $scenario
272276
/**
273277
* @param Collection<int, string> $args
274278
*/
275-
private function getPhpProcess(string $workingDirectory, string $fileName, Collection $args): Process
279+
private function getPhpProcess(string $workingDirectory, string $fileName, Collection $args, array $exposedPorts): Process
276280
{
277281
return $this->processFactory->create(
278-
new ProcessInput('php', [$fileName, ...$args->getArrayCopy()], $workingDirectory, []),
282+
new ProcessInput('php', [$fileName, ...$args->getArrayCopy()], $workingDirectory, [], $exposedPorts),
279283
);
280284
}
281285
}

src/Process/DockerProcessFactory.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ public function create(ProcessInput $processInput): Process
4040
$env[] = $key . '=' . $value;
4141
}
4242

43+
$ports = [];
44+
foreach ($processInput->getExposedPorts() as $port) {
45+
$ports[] = '-p';
46+
$ports[] = $port . ':' . $port;
47+
}
48+
4349
$env[] = '-e';
4450
$env[] = 'COMPOSER_HOME=/tmp/composer';
4551

46-
return new Process(
52+
$p = new Process(
4753
[
48-
...$this->baseComposeCommand($mounts, $env),
54+
...$this->baseComposeCommand($mounts, $env, $ports),
4955
'runtime',
5056
$processInput->getExecutable(),
5157
...$processInput->getArgs(),
@@ -59,14 +65,16 @@ public function create(ProcessInput $processInput): Process
5965
$processInput->getInput(),
6066
30,
6167
);
68+
69+
return $p;
6270
}
6371

6472
/**
6573
* @param array<string> $mounts
6674
* @param array<string> $env
6775
* @return array<string>
6876
*/
69-
private function baseComposeCommand(array $mounts, array $env): array
77+
private function baseComposeCommand(array $mounts, array $env, array $ports): array
7078
{
7179
$dockerPath = $this->executableFinder->find('docker');
7280
if ($dockerPath === null) {
@@ -85,6 +93,7 @@ private function baseComposeCommand(array $mounts, array $env): array
8593
getmyuid() . ':' . getmygid(),
8694
'--rm',
8795
...$env,
96+
...$ports,
8897
'-w',
8998
'/solution',
9099
...array_merge(...array_map(fn($mount) => ['-v', $mount], $mounts)),

src/Process/ProcessInput.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ class ProcessInput
77
/**
88
* @param list<string> $args
99
* @param array<string, string> $env
10+
* @param list<int> $exposedPorts
1011
*/
1112
public function __construct(
1213
private string $executable,
1314
private array $args,
1415
private string $workingDirectory,
1516
private array $env,
17+
private array $exposedPorts,
1618
private ?string $input = null,
1719
) {}
1820

@@ -42,6 +44,14 @@ public function getEnv(): array
4244
return $this->env;
4345
}
4446

47+
/**
48+
* @return list<int>
49+
*/
50+
public function getExposedPorts(): array
51+
{
52+
return $this->exposedPorts;
53+
}
54+
4555
public function getInput(): ?string
4656
{
4757
return $this->input;

0 commit comments

Comments
 (0)