Skip to content

Commit 6c91628

Browse files
committed
Update events to use context
1 parent f0e28d5 commit 6c91628

26 files changed

+436
-350
lines changed

Diff for: app/config.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@
267267
InitialCodeListener::class => function (ContainerInterface $c) {
268268
return new InitialCodeListener($c->get('currentWorkingDirectory'), $c->get(LoggerInterface::class));
269269
},
270-
PrepareSolutionListener::class => create(),
270+
PrepareSolutionListener::class => function (ContainerInterface $c) {
271+
return new PrepareSolutionListener(
272+
$c->get(ProcessFactory::class)
273+
);
274+
},
271275
CodePatchListener::class => function (ContainerInterface $c) {
272276
return new CodePatchListener(
273277
$c->get(CodePatcher::class),
@@ -472,21 +476,24 @@ function (CgiResult $result) use ($c) {
472476
],
473477
],
474478
'prepare-solution' => [
475-
'cli.verify.start' => [
479+
'cli.verify.reference-execute.pre' => [
476480
containerListener(PrepareSolutionListener::class),
477481
],
478482
'cli.run.start' => [
479483
containerListener(PrepareSolutionListener::class),
480484
],
481-
'cgi.verify.start' => [
485+
'cgi.verify.reference-execute.pre' => [
482486
containerListener(PrepareSolutionListener::class),
483487
],
484488
'cgi.run.start' => [
485489
containerListener(PrepareSolutionListener::class),
486490
],
487491
],
488492
'code-patcher' => [
489-
'verify.pre.execute' => [
493+
'cli.verify.start' => [
494+
containerListener(CodePatchListener::class, 'patch'),
495+
],
496+
'cgi.verify.start' => [
490497
containerListener(CodePatchListener::class, 'patch'),
491498
],
492499
'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
@@ -117,7 +117,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
117117
$this->requireCheck($requiredCheck);
118118
}
119119

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

122122
$this->validateChecks($this->checksToRunBefore, $exercise);
123123
$this->validateChecks($this->checksToRunAfter, $exercise);
@@ -130,22 +130,22 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
130130
}
131131
}
132132

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

135135
try {
136136
$this->results->add($runner->verify($context));
137137
} finally {
138-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $exercise, $input));
138+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $context));
139139
}
140140

141141
foreach ($this->checksToRunAfter as $check) {
142142
$this->results->add($check->check($context));
143143
}
144144

145-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $exercise, $input));
145+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $context));
146146
$exercise->tearDown();
147147

148-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $exercise, $input));
148+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $context));
149149
return $this->results;
150150
}
151151

@@ -173,14 +173,14 @@ public function run(ExerciseInterface $exercise, Input $input, OutputInterface $
173173
throw CouldNotRunException::fromFailure($result);
174174
}
175175

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

178178
try {
179179
$exitStatus = $this->runnerManager
180180
->getRunner($exercise)
181181
->run($context, $output);
182182
} finally {
183-
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $exercise, $input));
183+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $context));
184184
}
185185

186186
return $exitStatus;

0 commit comments

Comments
 (0)