Skip to content

Commit af9a0ca

Browse files
committed
Use new runner events in cgi + cli runner
1 parent fcbbeba commit af9a0ca

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/ExerciseRunner/CgiRunner.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
use PhpSchool\PhpWorkshop\Event\CgiExecuteEvent;
99
use PhpSchool\PhpWorkshop\Event\Event;
1010
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
11+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
1112
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
1213
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1314
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
14-
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
1515
use PhpSchool\PhpWorkshop\Input\Input;
1616
use PhpSchool\PhpWorkshop\Output\OutputInterface;
17-
use PhpSchool\PhpWorkshop\Result\CgiOutFailure;
1817
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
1918
use PhpSchool\PhpWorkshop\Result\CgiOutResult;
2019
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -237,7 +236,8 @@ private function getProcess($fileName, RequestInterface $request)
237236
*/
238237
public function verify(Input $input)
239238
{
240-
return new CgiOutResult(
239+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.start', $this->exercise, $input));
240+
$result = new CgiOutResult(
241241
$this->getName(),
242242
array_map(
243243
function (RequestInterface $request) use ($input) {
@@ -246,6 +246,8 @@ function (RequestInterface $request) use ($input) {
246246
$this->exercise->getRequests()
247247
)
248248
);
249+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.finish', $this->exercise, $input));
250+
return $result;
249251
}
250252

251253
/**
@@ -267,6 +269,7 @@ function (RequestInterface $request) use ($input) {
267269
*/
268270
public function run(Input $input, OutputInterface $output)
269271
{
272+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.start', $this->exercise, $input));
270273
$success = true;
271274
foreach ($this->exercise->getRequests() as $i => $request) {
272275
$event = $this->eventDispatcher->dispatch(
@@ -295,6 +298,7 @@ public function run(Input $input, OutputInterface $output)
295298

296299
$output->lineBreak();
297300
}
301+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input));
298302
return $success;
299303
}
300304
}

src/ExerciseRunner/CliRunner.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
99
use PhpSchool\PhpWorkshop\Event\Event;
1010
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
11+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
1112
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
1213
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1314
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
14-
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
1515
use PhpSchool\PhpWorkshop\Input\Input;
1616
use PhpSchool\PhpWorkshop\Output\OutputInterface;
1717
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -127,6 +127,18 @@ private function getPhpProcess($fileName, ArrayObject $args)
127127
* @return ResultInterface The result of the check.
128128
*/
129129
public function verify(Input $input)
130+
{
131+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.start', $this->exercise, $input));
132+
$result = $this->doVerify($input);
133+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.finish', $this->exercise, $input));
134+
return $result;
135+
}
136+
137+
/**
138+
* @param Input $input
139+
* @return ResultInterface
140+
*/
141+
private function doVerify(Input $input)
130142
{
131143
//arrays are not pass-by-ref
132144
$args = new ArrayObject($this->exercise->getArgs());
@@ -175,6 +187,7 @@ public function verify(Input $input)
175187
*/
176188
public function run(Input $input, OutputInterface $output)
177189
{
190+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.start', $this->exercise, $input));
178191
/** @var CliExecuteEvent $event */
179192
$event = $this->eventDispatcher->dispatch(
180193
new CliExecuteEvent('cli.run.student-execute.pre', new ArrayObject($this->exercise->getArgs()))
@@ -200,6 +213,7 @@ public function run(Input $input, OutputInterface $output)
200213
$output->writeLine($outputBuffer);
201214
});
202215

216+
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.finish', $this->exercise, $input));
203217
return $process->isSuccessful();
204218
}
205219
}

0 commit comments

Comments
 (0)