Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
use PhpSchool\PhpWorkshop\Output\StdOutput;
use PhpSchool\PhpWorkshop\Patch;
use PhpSchool\PhpWorkshop\Process\Composer;
use PhpSchool\PhpWorkshop\Process\DockerSandbox;
use PhpSchool\PhpWorkshop\Process\HostSandbox;
use PhpSchool\PhpWorkshop\Process\Sandbox;
use PhpSchool\PhpWorkshop\Process\Sandbox\Docker\DockerSandboxFactory;
use PhpSchool\PhpWorkshop\Process\Sandbox\Host\HostSandboxFactory;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxFactory;
use PhpSchool\PhpWorkshop\Result\Cgi\CgiResult;
use PhpSchool\PhpWorkshop\Result\Cgi\GenericFailure as CgiGenericFailure;
use PhpSchool\PhpWorkshop\Result\Cgi\RequestFailure as CgiRequestFailure;
Expand Down Expand Up @@ -199,27 +199,27 @@
$manager = new RunnerManager();
$manager->addFactory(new CliRunnerFactory(
$c->get(EventDispatcher::class),
$c->get(Sandbox::class),
$c->get(SandboxFactory::class),
$c->get(EnvironmentManager::class),
));
$manager->addFactory(new CgiRunnerFactory(
$c->get(EventDispatcher::class),
$c->get(Sandbox::class),
$c->get(SandboxFactory::class),
$c->get(EnvironmentManager::class),
));
$manager->addFactory(new CustomVerifyingRunnerFactory());
return $manager;
},

Sandbox::class => function (ContainerInterface $c) {
SandboxFactory::class => function (ContainerInterface $c) {
$sandbox = getenv('SANDBOX');

return match ($sandbox) {
'docker' => new DockerSandbox(
'docker' => new DockerSandboxFactory(
$c->get('basePath'),
$c->get('appName'),
),
default => new HostSandbox(),
default => new HostSandboxFactory(),
};
},

Expand Down
16 changes: 14 additions & 2 deletions src/Event/CliExecuteEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Process\SandboxExecution;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxExecution;
use PhpSchool\PhpWorkshop\Process\Sandbox\Sandbox;
use PhpSchool\PhpWorkshop\Utils\Collection;

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ public function appendArg(string $arg): void
}

/**
* The running program, eg. to connect to a port it listens on with getAddress(). Only on the executing events.
* The running program, eg. to connect to the port it listens on with getAddress(). Only on the executing events.
*/
public function getExecution(): SandboxExecution
{
Expand All @@ -68,6 +69,17 @@ public function getExecution(): SandboxExecution
return $execution;
}

/**
* Where the solution runs, eg. to pass its port in the args. Only on the pre events.
*/
public function getSandbox(): Sandbox
{
/** @var Sandbox $sandbox */
$sandbox = $this->getParameter('sandbox');

return $sandbox;
}

/**
* Get the arguments to be passed to the program.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CodeExecutionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpSchool\PhpWorkshop\Exception;

use RuntimeException;
use PhpSchool\PhpWorkshop\Process\SandboxExecution;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxExecution;

/**
* Represents the situation where some PHP code could not be executed successfully.
Expand Down
20 changes: 9 additions & 11 deletions src/Exercise/Scenario/ExerciseScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ abstract class ExerciseScenario
*/
private array $files = [];

/**
* @var list<int>
*/
private array $exposedPorts = [];
private bool $exposesPort = false;

public function withFile(string $relativeFileName, string $content): static
{
Expand All @@ -29,18 +26,19 @@ public function getFiles(): array
return $this->files;
}

public function exposePort(int $port): static
/**
* The code listens on a port, eg. a server the check connects to. The sandbox picks it, listeners get it from the
* pre execute events with getSandbox()->getPort().
*/
public function exposePort(): static
{
$this->exposedPorts[] = $port;
$this->exposesPort = true;

return $this;
}

/**
* @return list<int>
*/
public function getExposedPorts(): array
public function exposesPort(): bool
{
return $this->exposedPorts;
return $this->exposesPort;
}
}
44 changes: 20 additions & 24 deletions src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Process\Sandbox;
use PhpSchool\PhpWorkshop\Process\ProcessInput;
use PhpSchool\PhpWorkshop\Process\SandboxExecution;
use PhpSchool\PhpWorkshop\Process\Sandbox\Sandbox;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxFactory;
use PhpSchool\PhpWorkshop\Process\Sandbox\ProcessInput;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxExecution;
use PhpSchool\PhpWorkshop\Result\Cgi\CgiResult;
use PhpSchool\PhpWorkshop\Result\Cgi\RequestFailure;
use PhpSchool\PhpWorkshop\Result\Cgi\GenericFailure;
Expand Down Expand Up @@ -59,7 +60,7 @@ class CgiRunner implements ExerciseRunnerInterface
public function __construct(
private CgiExercise $exercise,
private EventDispatcher $eventDispatcher,
private Sandbox $sandbox,
private SandboxFactory $sandboxFactory,
private EnvironmentManager $environmentManager,
) {}

Expand Down Expand Up @@ -109,10 +110,13 @@ public function verify(ExecutionContext $context): ResultInterface

$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.verify.start', $context, $scenario));

$reference = $this->sandboxFactory->create($context->getReferenceExecutionDirectory(), $scenario);
$student = $this->sandboxFactory->create($context->getStudentExecutionDirectory(), $scenario);

$result = new CgiResult(
array_map(
function (RequestInterface $request) use ($context, $scenario) {
return $this->doVerify($context, $scenario, $request);
function (RequestInterface $request) use ($context, $scenario, $reference, $student) {
return $this->doVerify($context, $scenario, $request, $reference, $student);
},
$scenario->getExecutions(),
),
Expand All @@ -122,7 +126,7 @@ function (RequestInterface $request) use ($context, $scenario) {
return $result;
}

private function doVerify(ExecutionContext $context, CgiScenario $scenario, RequestInterface $request): CgiResultInterface
private function doVerify(ExecutionContext $context, CgiScenario $scenario, RequestInterface $request, Sandbox $reference, Sandbox $student): CgiResultInterface
{
try {
/** @var CgiExecuteEvent $event */
Expand All @@ -132,7 +136,7 @@ private function doVerify(ExecutionContext $context, CgiScenario $scenario, Requ
$solutionResponse = $this->executePhpFile(
$context,
$scenario,
$context->getReferenceExecutionDirectory(),
$reference,
$this->exercise->getSolution()->getEntryPoint()->getRelativePath(),
$event->getRequest(),
'reference',
Expand All @@ -158,7 +162,7 @@ private function doVerify(ExecutionContext $context, CgiScenario $scenario, Requ
$userResponse = $this->executePhpFile(
$context,
$scenario,
$context->getStudentExecutionDirectory(),
$student,
basename($context->getEntryPoint()),
$event->getRequest(),
'student',
Expand Down Expand Up @@ -210,12 +214,12 @@ private function getHeaders(ResponseInterface $response): array
private function executePhpFile(
ExecutionContext $context,
CgiScenario $scenario,
string $workingDirectory,
Sandbox $sandbox,
string $fileName,
RequestInterface $request,
string $type,
): ResponseInterface {
$execution = $this->getPhpExecution($workingDirectory, $fileName, $request, $scenario->getExposedPorts());
$execution = $this->getPhpExecution($sandbox, $fileName, $request);

$execution->start();
$this->eventDispatcher->dispatch(
Expand All @@ -237,10 +241,7 @@ private function executePhpFile(
return Message::parseResponse($output);
}

/**
* @param list<int> $exposedPorts
*/
private function getPhpExecution(string $workingDirectory, string $fileName, RequestInterface $request, array $exposedPorts): SandboxExecution
private function getPhpExecution(Sandbox $sandbox, string $fileName, RequestInterface $request): SandboxExecution
{
$env = [
'REQUEST_METHOD' => $request->getMethod(),
Expand All @@ -266,13 +267,11 @@ private function getPhpExecution(string $workingDirectory, string $fileName, Req
'-dhtml_errors=0',
'-dexpose_php=0',
],
$workingDirectory,
$env,
$exposedPorts,
$content,
);

return $this->sandbox->create($processInput);
return $sandbox->execute($processInput);
}

/**
Expand Down Expand Up @@ -302,18 +301,15 @@ public function run(ExecutionContext $context, OutputInterface $output): bool

$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.run.start', $context, $scenario));

$student = $this->sandboxFactory->create($context->getStudentExecutionDirectory(), $scenario);

$success = true;
foreach ($scenario->getExecutions() as $i => $request) {
/** @var CgiExecuteEvent $event */
$event = $this->eventDispatcher->dispatch(
new CgiExecuteEvent('cgi.run.student-execute.pre', $context, $scenario, $request),
);
$execution = $this->getPhpExecution(
$context->getStudentExecutionDirectory(),
basename($context->getEntryPoint()),
$event->getRequest(),
$scenario->getExposedPorts(),
);
$execution = $this->getPhpExecution($student, basename($context->getEntryPoint()), $event->getRequest());

$execution->start();
$this->eventDispatcher->dispatch(
Expand Down
48 changes: 23 additions & 25 deletions src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Process\Sandbox;
use PhpSchool\PhpWorkshop\Process\ProcessInput;
use PhpSchool\PhpWorkshop\Process\SandboxExecution;
use PhpSchool\PhpWorkshop\Process\Sandbox\Sandbox;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxFactory;
use PhpSchool\PhpWorkshop\Process\Sandbox\ProcessInput;
use PhpSchool\PhpWorkshop\Process\Sandbox\SandboxExecution;
use PhpSchool\PhpWorkshop\Result\Cli\RequestFailure;
use PhpSchool\PhpWorkshop\Result\Cli\CliResult;
use PhpSchool\PhpWorkshop\Result\Cli\GenericFailure;
Expand Down Expand Up @@ -59,7 +60,7 @@ class CliRunner implements ExerciseRunnerInterface
public function __construct(
private CliExercise $exercise,
private EventDispatcher $eventDispatcher,
private Sandbox $sandbox,
private SandboxFactory $sandboxFactory,
private EnvironmentManager $environmentManager,
) {}

Expand Down Expand Up @@ -108,10 +109,13 @@ public function verify(ExecutionContext $context): ResultInterface

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

$reference = $this->sandboxFactory->create($context->getReferenceExecutionDirectory(), $scenario);
$student = $this->sandboxFactory->create($context->getStudentExecutionDirectory(), $scenario);

$result = new CliResult(
array_map(
function (Collection $args) use ($context, $scenario) {
return $this->doVerify($context, $scenario, $args);
function (Collection $args) use ($context, $scenario, $reference, $student) {
return $this->doVerify($context, $scenario, $args, $reference, $student);
},
$scenario->getExecutions(),
),
Expand All @@ -125,17 +129,17 @@ function (Collection $args) use ($context, $scenario) {
/**
* @param Collection<int, string> $args
*/
private function doVerify(ExecutionContext $context, CliScenario $scenario, Collection $args): CliResultInterface
private function doVerify(ExecutionContext $context, CliScenario $scenario, Collection $args, Sandbox $reference, Sandbox $student): CliResultInterface
{
try {
/** @var CliExecuteEvent $event */
$event = $this->eventDispatcher->dispatch(
new CliExecuteEvent('cli.verify.reference-execute.pre', $context, $scenario, $args),
new CliExecuteEvent('cli.verify.reference-execute.pre', $context, $scenario, $args, ['sandbox' => $reference]),
);
$solutionOutput = $this->executePhpFile(
$context,
$scenario,
$context->getReferenceExecutionDirectory(),
$reference,
$this->exercise->getSolution()->getEntryPoint()->getRelativePath(),
$event->getArgs(),
'reference',
Expand All @@ -156,12 +160,12 @@ private function doVerify(ExecutionContext $context, CliScenario $scenario, Coll
try {
/** @var CliExecuteEvent $event */
$event = $this->eventDispatcher->dispatch(
new CliExecuteEvent('cli.verify.student-execute.pre', $context, $scenario, $args),
new CliExecuteEvent('cli.verify.student-execute.pre', $context, $scenario, $args, ['sandbox' => $student]),
);
$userOutput = $this->executePhpFile(
$context,
$scenario,
$context->getStudentExecutionDirectory(),
$student,
basename($context->getEntryPoint()),
$event->getArgs(),
'student',
Expand Down Expand Up @@ -211,21 +215,18 @@ public function run(ExecutionContext $context, OutputInterface $output): bool

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

$student = $this->sandboxFactory->create($context->getStudentExecutionDirectory(), $scenario);

$success = true;
foreach ($scenario->getExecutions() as $i => $args) {
/** @var CliExecuteEvent $event */
$event = $this->eventDispatcher->dispatch(
new CliExecuteEvent('cli.run.student-execute.pre', $context, $scenario, $args),
new CliExecuteEvent('cli.run.student-execute.pre', $context, $scenario, $args, ['sandbox' => $student]),
);

$args = $event->getArgs();

$execution = $this->getPhpExecution(
$context->getStudentExecutionDirectory(),
basename($context->getEntryPoint()),
$args,
$scenario->getExposedPorts(),
);
$execution = $this->getPhpExecution($student, basename($context->getEntryPoint()), $args);

$execution->start();
$this->eventDispatcher->dispatch(
Expand Down Expand Up @@ -256,9 +257,9 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
/**
* @param Collection<int, string> $args
*/
private function executePhpFile(ExecutionContext $context, CliScenario $scenario, string $workingDirectory, string $fileName, Collection $args, string $type): string
private function executePhpFile(ExecutionContext $context, CliScenario $scenario, Sandbox $sandbox, string $fileName, Collection $args, string $type): string
{
$execution = $this->getPhpExecution($workingDirectory, $fileName, $args, $scenario->getExposedPorts());
$execution = $this->getPhpExecution($sandbox, $fileName, $args);

$execution->start();
$this->eventDispatcher->dispatch(
Expand All @@ -275,12 +276,9 @@ private function executePhpFile(ExecutionContext $context, CliScenario $scenario

/**
* @param Collection<int, string> $args
* @param list<int> $exposedPorts
*/
private function getPhpExecution(string $workingDirectory, string $fileName, Collection $args, array $exposedPorts): SandboxExecution
private function getPhpExecution(Sandbox $sandbox, string $fileName, Collection $args): SandboxExecution
{
return $this->sandbox->create(
new ProcessInput('php', [$fileName, ...$args->getArrayCopy()], $workingDirectory, [], $exposedPorts),
);
return $sandbox->execute(new ProcessInput('php', [$fileName, ...$args->getArrayCopy()], []));
}
}
Loading
Loading