Skip to content

Commit f50b360

Browse files
committed
Update events to use context
1 parent 0964eb3 commit f50b360

23 files changed

+361
-343
lines changed

Diff for: app/config.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@
255255
InitialCodeListener::class => function (ContainerInterface $c) {
256256
return new InitialCodeListener($c->get('currentWorkingDirectory'), $c->get(LoggerInterface::class));
257257
},
258-
PrepareSolutionListener::class => create(),
258+
PrepareSolutionListener::class => function (ContainerInterface $c) {
259+
return new PrepareSolutionListener(
260+
$c->get(ProcessFactory::class)
261+
);
262+
},
259263
CodePatchListener::class => function (ContainerInterface $c) {
260264
return new CodePatchListener(
261265
$c->get(CodePatcher::class),
@@ -460,21 +464,24 @@ function (CgiResult $result) use ($c) {
460464
],
461465
],
462466
'prepare-solution' => [
463-
'cli.verify.start' => [
467+
'cli.verify.reference-execute.pre' => [
464468
containerListener(PrepareSolutionListener::class),
465469
],
466470
'cli.run.start' => [
467471
containerListener(PrepareSolutionListener::class),
468472
],
469-
'cgi.verify.start' => [
473+
'cgi.verify.reference-execute.pre' => [
470474
containerListener(PrepareSolutionListener::class),
471475
],
472476
'cgi.run.start' => [
473477
containerListener(PrepareSolutionListener::class),
474478
],
475479
],
476480
'code-patcher' => [
477-
'verify.pre.execute' => [
481+
'cli.verify.start' => [
482+
containerListener(CodePatchListener::class, 'patch'),
483+
],
484+
'cgi.verify.start' => [
478485
containerListener(CodePatchListener::class, 'patch'),
479486
],
480487
'verify.post.execute' => [

Diff for: src/Event/CgiExecuteEvent.php

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

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CgiScenario;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
810
use PhpSchool\PhpWorkshop\Input\Input;
911
use Psr\Http\Message\RequestInterface;
1012

@@ -19,17 +21,17 @@ class CgiExecuteEvent extends CgiExerciseRunnerEvent
1921
/**
2022
* @param string $name The event name.
2123
* @param RequestInterface $request The request that will be performed.
22-
* @param array<mixed> $parameters The event parameters.
24+
* @param array<string, mixed> $parameters The event parameters.
2325
*/
2426
public function __construct(
2527
string $name,
26-
ExerciseInterface $exercise,
27-
Input $input,
28+
ExecutionContext $context,
29+
CgiScenario $scenario,
2830
RequestInterface $request,
2931
array $parameters = []
3032
) {
3133
$parameters['request'] = $request;
32-
parent::__construct($name, $exercise, $input, $parameters);
34+
parent::__construct($name, $context, $scenario, $parameters);
3335
$this->request = $request;
3436
}
3537

Diff for: src/Event/CgiExerciseRunnerEvent.php

+20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,29 @@
33
namespace PhpSchool\PhpWorkshop\Event;
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CgiScenario;
67
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
78
use PhpSchool\PhpWorkshop\Input\Input;
89

910
class CgiExerciseRunnerEvent extends ExerciseRunnerEvent
1011
{
12+
private CgiScenario $scenario;
13+
14+
/**
15+
* @param array<string, mixed> $parameters
16+
*/
17+
public function __construct(
18+
string $name,
19+
ExecutionContext $context,
20+
CgiScenario $scenario,
21+
array $parameters = []
22+
) {
23+
$this->scenario = $scenario;
24+
parent::__construct($name, $context, $parameters);
25+
}
26+
27+
public function getScenario(): CgiScenario
28+
{
29+
return $this->scenario;
30+
}
1131
}

Diff for: src/Event/CliExecuteEvent.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace PhpSchool\PhpWorkshop\Event;
66

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
810
use PhpSchool\PhpWorkshop\Input\Input;
9-
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
11+
use PhpSchool\PhpWorkshop\Utils\Collection;
1012

1113
/**
1214
* An event to represent events which occur throughout the verification and running process in
@@ -15,24 +17,24 @@
1517
class CliExecuteEvent extends CliExerciseRunnerEvent
1618
{
1719
/**
18-
* @var ArrayObject<int, string>
20+
* @var Collection<int, string>
1921
*/
20-
private ArrayObject $args;
22+
private Collection $args;
2123

2224
/**
2325
* @param string $name The event name.
24-
* @param ArrayObject<int, string> $args The arguments that should be/have been passed to the program.
25-
* @param array<mixed> $parameters The event parameters.
26+
* @param Collection<int, string> $args The arguments that should be/have been passed to the program.
27+
* @param array<string, mixed> $parameters The event parameters.
2628
*/
2729
public function __construct(
2830
string $name,
29-
ExerciseInterface $exercise,
30-
Input $input,
31-
ArrayObject $args,
31+
ExecutionContext $context,
32+
CliScenario $scenario,
33+
Collection $args,
3234
array $parameters = []
3335
) {
3436
$parameters['args'] = $args;
35-
parent::__construct($name, $exercise, $input, $parameters);
37+
parent::__construct($name, $context, $scenario, $parameters);
3638
$this->args = $args;
3739
}
3840

@@ -59,9 +61,9 @@ public function appendArg(string $arg): void
5961
/**
6062
* Get the arguments to be passed to the program.
6163
*
62-
* @return ArrayObject<int, string>
64+
* @return Collection<int, string>
6365
*/
64-
public function getArgs(): ArrayObject
66+
public function getArgs(): Collection
6567
{
6668
return $this->args;
6769
}

Diff for: src/Event/CliExerciseRunnerEvent.php

+20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,29 @@
33
namespace PhpSchool\PhpWorkshop\Event;
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
67
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
78
use PhpSchool\PhpWorkshop\Input\Input;
89

910
class CliExerciseRunnerEvent extends ExerciseRunnerEvent
1011
{
12+
private CliScenario $scenario;
13+
14+
/**
15+
* @param array<string, mixed> $parameters
16+
*/
17+
public function __construct(
18+
string $name,
19+
ExecutionContext $context,
20+
CliScenario $scenario,
21+
array $parameters = []
22+
) {
23+
$this->scenario = $scenario;
24+
parent::__construct($name, $context, $parameters);
25+
}
26+
27+
public function getScenario(): CliScenario
28+
{
29+
return $this->scenario;
30+
}
1131
}

Diff for: src/Event/EventDispatcher.php

-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class EventDispatcher
1717
* @var array<string, array<callable>>
1818
*/
1919
private array $listeners = [];
20-
21-
/**
22-
* @var ResultAggregator
23-
*/
2420
private ResultAggregator $resultAggregator;
2521

2622
public function __construct(ResultAggregator $resultAggregator)

Diff for: src/Event/ExerciseRunnerEvent.php

+14-19
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,47 @@
55
namespace PhpSchool\PhpWorkshop\Event;
66

77
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
89
use PhpSchool\PhpWorkshop\Input\Input;
910

1011
/**
1112
* An event which is dispatched during exercise running
1213
*/
1314
class ExerciseRunnerEvent extends Event
1415
{
15-
/**
16-
* @var ExerciseInterface
17-
*/
18-
private $exercise;
19-
20-
/**
21-
* @var Input
22-
*/
23-
private $input;
16+
private ExecutionContext $context;
2417

2518
/**
2619
* @param string $name
27-
* @param ExerciseInterface $exercise
28-
* @param Input $input
29-
* @param array<mixed> $parameters
20+
* @param array<string, mixed> $parameters
3021
*/
31-
public function __construct(string $name, ExerciseInterface $exercise, Input $input, array $parameters = [])
22+
public function __construct(string $name, ExecutionContext $context, array $parameters = [])
3223
{
33-
$parameters['input'] = $input;
34-
$parameters['exercise'] = $exercise;
24+
$this->context = $context;
25+
26+
$parameters['input'] = $context->getInput();
27+
$parameters['exercise'] = $context->getExercise();
3528
parent::__construct($name, $parameters);
29+
}
3630

37-
$this->exercise = $exercise;
38-
$this->input = $input;
31+
public function getContext(): ExecutionContext
32+
{
33+
return $this->context;
3934
}
4035

4136
/**
4237
* @return Input
4338
*/
4439
public function getInput(): Input
4540
{
46-
return $this->input;
41+
return $this->context->getInput();
4742
}
4843

4944
/**
5045
* @return ExerciseInterface
5146
*/
5247
public function getExercise(): ExerciseInterface
5348
{
54-
return $this->exercise;
49+
return $this->context->getExercise();
5550
}
5651
}

Diff for: src/ExerciseCheck/SelfCheck.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpSchool\PhpWorkshop\ExerciseCheck;
66

7+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
78
use PhpSchool\PhpWorkshop\Input\Input;
89
use PhpSchool\PhpWorkshop\Result\ResultInterface;
910

@@ -21,8 +22,8 @@ interface SelfCheck
2122
* The method is passed the absolute file path to the student's solution and should return a result
2223
* object which indicates the success or not of the check.
2324
*
24-
* @param Input $input The command line arguments passed to the command.
25+
* @param ExecutionContext $context The current execution context.
2526
* @return ResultInterface The result of the check.
2627
*/
27-
public function check(Input $input): ResultInterface;
28+
public function check(ExecutionContext $context): ResultInterface;
2829
}

Diff for: src/ExerciseDispatcher.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
137137
$this->requireCheck($requiredCheck);
138138
}
139139

140-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.start', $exercise, $input));
140+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.start', $context));
141141

142142
$this->validateChecks($this->checksToRunBefore, $exercise);
143143
$this->validateChecks($this->checksToRunAfter, $exercise);
@@ -150,22 +150,22 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
150150
}
151151
}
152152

153-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.pre.execute', $exercise, $input));
153+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.pre.execute', $context));
154154

155155
try {
156156
$this->results->add($runner->verify($input));
157157
} finally {
158-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $exercise, $input));
158+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $context));
159159
}
160160

161161
foreach ($this->checksToRunAfter as $check) {
162162
$this->results->add($check->check($exercise, $input));
163163
}
164164

165-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $exercise, $input));
165+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $context));
166166
$exercise->tearDown();
167167

168-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $exercise, $input));
168+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $context));
169169
return $this->results;
170170
}
171171

@@ -191,14 +191,14 @@ public function run(ExerciseInterface $exercise, Input $input, OutputInterface $
191191
throw CouldNotRunException::fromFailure($result);
192192
}
193193

194-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $exercise, $input));
194+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $context));
195195

196196
try {
197197
$exitStatus = $this->runnerManager
198198
->getRunner($exercise)
199199
->run($input, $output);
200200
} finally {
201-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $exercise, $input));
201+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $context));
202202
}
203203

204204
return $exitStatus;

0 commit comments

Comments
 (0)