Skip to content

Commit ad4eda1

Browse files
committed
WIP Execution context
1 parent 739f691 commit ad4eda1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+492
-205
lines changed

app/config.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,7 @@
212212
$c->get('appName')
213213
);
214214

215-
// return new \PhpSchool\PhpWorkshop\Process\HostProcessFactory(
216-
// (new \Symfony\Component\Process\ExecutableFinder())->find('php'),
217-
// (new \Symfony\Component\Process\ExecutableFinder())->find('php-cgi'),
218-
// '/usr/local/bin/composer',
219-
// );
215+
return new \PhpSchool\PhpWorkshop\Process\HostProcessFactory();
220216
},
221217

222218
//commands
@@ -484,13 +480,13 @@ function (CgiResult $result) use ($c) {
484480
],
485481
],
486482
'prepare-solution' => [
487-
'cli.verify.start' => [
483+
'cli.verify.reference-execute.pre' => [
488484
containerListener(PrepareSolutionListener::class),
489485
],
490486
'cli.run.start' => [
491487
containerListener(PrepareSolutionListener::class),
492488
],
493-
'cgi.verify.start' => [
489+
'cgi.verify.reference-execute.pre' => [
494490
containerListener(PrepareSolutionListener::class),
495491
],
496492
'cgi.run.start' => [

src/Check/CodeExistsCheck.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PhpParser\Parser;
1212
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1313
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
14+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
15+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
1416
use PhpSchool\PhpWorkshop\Input\Input;
1517
use PhpSchool\PhpWorkshop\Result\Failure;
1618
use PhpSchool\PhpWorkshop\Result\ResultInterface;
@@ -37,15 +39,15 @@ public function getName(): string
3739
* Check solution provided contains code
3840
* Note: We don't care if it's valid code at this point
3941
*/
40-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
42+
public function check(ExecutionContext $context): ResultInterface
4143
{
4244
$noopHandler = new class implements ErrorHandler {
4345
public function handleError(Error $error): void
4446
{
4547
}
4648
};
4749

48-
$code = (string) file_get_contents($input->getRequiredArgument('program'));
50+
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
4951
$statements = $this->parser->parse($code, $noopHandler);
5052

5153
$empty = null === $statements || empty($statements);

src/Check/CodeParseCheck.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PhpParser\Parser;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
12+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
1113
use PhpSchool\PhpWorkshop\Input\Input;
1214
use PhpSchool\PhpWorkshop\Result\Failure;
1315
use PhpSchool\PhpWorkshop\Result\ResultInterface;
@@ -49,14 +51,14 @@ public function getName(): string
4951
* @param Input $input The command line arguments passed to the command.
5052
* @return ResultInterface The result of the check.
5153
*/
52-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
54+
public function check(ExecutionContext $context): ResultInterface
5355
{
54-
$code = (string) file_get_contents($input->getRequiredArgument('program'));
56+
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
5557

5658
try {
5759
$this->parser->parse($code);
5860
} catch (Error $e) {
59-
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getRequiredArgument('program'));
61+
return Failure::fromCheckAndCodeParseFailure($this, $e, $context->input->getRequiredArgument('program'));
6062
}
6163

6264
return Success::fromCheck($this);

src/Check/ComposerCheck.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1111
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
12+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
13+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
1214
use PhpSchool\PhpWorkshop\Input\Input;
1315
use PhpSchool\PhpWorkshop\Result\ComposerFailure;
1416
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -39,26 +41,26 @@ public function getName(): string
3941
* @return ResultInterface The result of the check.
4042
* @noinspection SpellCheckingInspection
4143
*/
42-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
44+
public function check(ExecutionContext $context): ResultInterface
4345
{
44-
if (!$exercise instanceof ComposerExerciseCheck) {
46+
if (!$context->exercise instanceof ComposerExerciseCheck) {
4547
throw new InvalidArgumentException();
4648
}
4749

48-
if (!file_exists(sprintf('%s/composer.json', dirname($input->getRequiredArgument('program'))))) {
50+
if (!file_exists(sprintf('%s/composer.json', dirname($context->input->getRequiredArgument('program'))))) {
4951
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'composer.json');
5052
}
5153

52-
if (!file_exists(sprintf('%s/composer.lock', dirname($input->getRequiredArgument('program'))))) {
54+
if (!file_exists(sprintf('%s/composer.lock', dirname($context->input->getRequiredArgument('program'))))) {
5355
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'composer.lock');
5456
}
5557

56-
if (!file_exists(sprintf('%s/vendor', dirname($input->getRequiredArgument('program'))))) {
58+
if (!file_exists(sprintf('%s/vendor', dirname($context->input->getRequiredArgument('program'))))) {
5759
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'vendor');
5860
}
5961

60-
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($input->getRequiredArgument('program'))));
61-
$missingPackages = array_filter($exercise->getRequiredPackages(), function ($package) use ($lockFile) {
62+
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($context->input->getRequiredArgument('program'))));
63+
$missingPackages = array_filter($context->exercise->getRequiredPackages(), function ($package) use ($lockFile) {
6264
return !$lockFile->hasInstalledPackage($package);
6365
});
6466

src/Check/FileComparisonCheck.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1111
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
1212
use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck;
13+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
14+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
1315
use PhpSchool\PhpWorkshop\Input\Input;
1416
use PhpSchool\PhpWorkshop\Result\Failure;
1517
use PhpSchool\PhpWorkshop\Result\FileComparisonFailure;
@@ -38,17 +40,17 @@ public function getName(): string
3840
* @param Input $input The command line arguments passed to the command.
3941
* @return ResultInterface The result of the check.
4042
*/
41-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
43+
public function check(ExecutionContext $context): ResultInterface
4244
{
43-
if (!$exercise instanceof FileComparisonExerciseCheck) {
45+
if (!$context->exercise instanceof FileComparisonExerciseCheck) {
4446
throw new InvalidArgumentException();
4547
}
4648

47-
foreach ($exercise->getFilesToCompare() as $key => $file) {
49+
foreach ($context->exercise->getFilesToCompare() as $key => $file) {
4850
[$options, $file] = $this->getOptionsAndFile($key, $file);
4951

50-
$studentFile = Path::join(dirname($input->getRequiredArgument('program')), $file);
51-
$referenceFile = Path::join($exercise->getSolution()->getBaseDirectory(), $file);
52+
$studentFile = Path::join(dirname($context->input->getRequiredArgument('program')), $file);
53+
$referenceFile = Path::join($context->referenceEnvironment->workingDirectory, $file);
5254

5355
if (!file_exists($referenceFile)) {
5456
throw SolutionFileDoesNotExistException::fromExpectedFile($file);

src/Check/FileExistsCheck.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
88
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
911
use PhpSchool\PhpWorkshop\Input\Input;
1012
use PhpSchool\PhpWorkshop\Result\Failure;
1113
use PhpSchool\PhpWorkshop\Result\ResultInterface;
@@ -31,15 +33,15 @@ public function getName(): string
3133
* @param Input $input The command line arguments passed to the command.
3234
* @return ResultInterface The result of the check.
3335
*/
34-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
36+
public function check(ExecutionContext $context): ResultInterface
3537
{
36-
if (file_exists($input->getRequiredArgument('program'))) {
38+
if (file_exists($context->input->getRequiredArgument('program'))) {
3739
return Success::fromCheck($this);
3840
}
3941

4042
return Failure::fromCheckAndReason(
4143
$this,
42-
sprintf('File: "%s" does not exist', $input->getRequiredArgument('program'))
44+
sprintf('File: "%s" does not exist', $context->input->getRequiredArgument('program'))
4345
);
4446
}
4547

src/Check/FunctionRequirementsCheck.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1313
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1414
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
15+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
16+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
1517
use PhpSchool\PhpWorkshop\Input\Input;
1618
use PhpSchool\PhpWorkshop\NodeVisitor\FunctionVisitor;
1719
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -55,16 +57,16 @@ public function getName(): string
5557
* @param Input $input The command line arguments passed to the command.
5658
* @return ResultInterface The result of the check.
5759
*/
58-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
60+
public function check(ExecutionContext $context): ResultInterface
5961
{
60-
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
62+
if (!$context->exercise instanceof FunctionRequirementsExerciseCheck) {
6163
throw new InvalidArgumentException();
6264
}
6365

64-
$requiredFunctions = $exercise->getRequiredFunctions();
65-
$bannedFunctions = $exercise->getBannedFunctions();
66+
$requiredFunctions = $context->exercise->getRequiredFunctions();
67+
$bannedFunctions = $context->exercise->getBannedFunctions();
6668

67-
$code = (string) file_get_contents($input->getRequiredArgument('program'));
69+
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
6870

6971
try {
7072
$ast = $this->parser->parse($code) ?? [];

src/Check/PhpLintCheck.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
88
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
911
use PhpSchool\PhpWorkshop\Input\Input;
1012
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1113
use PhpSchool\PhpWorkshop\Result\Success;
@@ -34,10 +36,10 @@ public function getName(): string
3436
* @param Input $input The command line arguments passed to the command.
3537
* @return ResultInterface The result of the check.
3638
*/
37-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
39+
public function check(ExecutionContext $context): ResultInterface
3840
{
3941
$finder = new ExecutableFinder();
40-
$process = new Process([$finder->find('php'), '-l', $input->getArgument('program')]);
42+
$process = new Process([$finder->find('php'), '-l', $context->input->getArgument('program')]);
4143
$process->run();
4244

4345
if ($process->isSuccessful()) {

src/Check/SimpleCheckInterface.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
88
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
911
use PhpSchool\PhpWorkshop\Input\Input;
1012
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1113

@@ -46,11 +48,10 @@ public function canRun(ExerciseType $exerciseType): bool;
4648
* successful then an instance of `PhpSchool\PhpWorkshop\Result\FailureInterface`
4749
* should be returned.
4850
*
49-
* @param ExerciseInterface $exercise The exercise to check against.
50-
* @param Input $input The command line arguments passed to the command.
51+
* @param ExecutionContext $context The current run context, containing the exercise, input and environments.
5152
* @return ResultInterface The result of the check.
5253
*/
53-
public function check(ExerciseInterface $exercise, Input $input): ResultInterface;
54+
public function check(ExecutionContext $context): ResultInterface;
5455

5556
/**
5657
* Either `static::CHECK_BEFORE` | `static::CHECK_AFTER`.

src/Event/CgiExecuteEvent.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace PhpSchool\PhpWorkshop\Event;
66

7+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\CgiContext;
78
use Psr\Http\Message\RequestInterface;
89

910
/**
1011
* An event to represent events which occur throughout the verification and running process in
1112
* `\PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner`.
1213
*/
13-
class CgiExecuteEvent extends Event
14+
class CgiExecuteEvent extends ExerciseRunnerEvent
1415
{
1516
/**
1617
* @var RequestInterface
@@ -22,10 +23,10 @@ class CgiExecuteEvent extends Event
2223
* @param RequestInterface $request The request that will be performed.
2324
* @param array<mixed> $parameters The event parameters.
2425
*/
25-
public function __construct(string $name, RequestInterface $request, array $parameters = [])
26+
public function __construct(string $name, CgiContext $context, RequestInterface $request, array $parameters = [])
2627
{
2728
$parameters['request'] = $request;
28-
parent::__construct($name, $parameters);
29+
parent::__construct($name, $context, $parameters);
2930
$this->request = $request;
3031
}
3132

src/Event/CliExecuteEvent.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
namespace PhpSchool\PhpWorkshop\Event;
66

7+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\ExerciseRunner\CliExecutionContext;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\CliContext;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
711
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
812
use PhpSchool\PhpWorkshop\Utils\Collection;
913

1014
/**
1115
* An event to represent events which occur throughout the verification and running process in
1216
* `\PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner`.
1317
*/
14-
class CliExecuteEvent extends Event
18+
class CliExecuteEvent extends ExerciseRunnerEvent
1519
{
1620
/**
1721
* @var Collection<int, string>
@@ -23,10 +27,10 @@ class CliExecuteEvent extends Event
2327
* @param Collection<int, string> $args The arguments that should be/have been passed to the program.
2428
* @param array<mixed> $parameters The event parameters.
2529
*/
26-
public function __construct(string $name, Collection $args, array $parameters = [])
30+
public function __construct(string $name, CliContext $context, Collection $args, array $parameters = [])
2731
{
2832
$parameters['args'] = $args;
29-
parent::__construct($name, $parameters);
33+
parent::__construct($name, $context, $parameters);
3034
$this->args = $args;
3135
}
3236

src/Event/Event.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace PhpSchool\PhpWorkshop\Event;
66

77
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
8+
use PhpSchool\PhpWorkshop\ExerciseRunner\CliEnvironment;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\RunnerContext;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Environment;
811

912
/**
1013
* A generic `PhpSchool\PhpWorkshop\Event\EventInterface` implementation.
@@ -25,7 +28,7 @@ class Event implements EventInterface
2528
* @param string $name The event name.
2629
* @param array<mixed> $parameters The event parameters.
2730
*/
28-
public function __construct(string $name, array $parameters = [])
31+
public function __construct(string $name, public RunnerContext $context, array $parameters = [])
2932
{
3033
$this->name = $name;
3134
$this->parameters = $parameters;

0 commit comments

Comments
 (0)