Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40d2675

Browse files
Seperate listeners and checks
1 parent 15563cf commit 40d2675

14 files changed

+115
-65
lines changed

‎src/Exercise/AbstractExercise.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpSchool\PhpWorkshop\Exercise;
66

7+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
8+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
79
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
810
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
911
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
@@ -78,12 +80,14 @@ public static function normaliseName(string $name): string
7880
}
7981

8082
/**
81-
* This method is implemented as empty by default, if you want to add additional checks or listen
82-
* to events, you should override this method.
83-
*
84-
* @param ExerciseDispatcher $dispatcher
83+
* @return list<class-string>
8584
*/
86-
public function configure(ExerciseDispatcher $dispatcher): void
85+
public function getRequiredChecks(): array
86+
{
87+
return [];
88+
}
89+
90+
public function defineListeners(EventDispatcher $dispatcher): void
8791
{
8892
}
8993
}

‎src/Exercise/ExerciseInterface.php‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpSchool\PhpWorkshop\Exercise;
66

7+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
78
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
89

910
/**
@@ -34,14 +35,16 @@ public function getType(): ExerciseType;
3435
public function getProblem(): string;
3536

3637
/**
37-
* This is where the exercise specifies the extra checks it may require. It is also
38-
* possible to grab the event dispatcher from the exercise dispatcher and listen to any
39-
* events. This method is automatically invoked just before verifying/running an student's solution
40-
* to an exercise.
38+
* Subscribe to events triggered throughout the verification process
39+
*/
40+
public function defineListeners(EventDispatcher $dispatcher): void;
41+
42+
/**
43+
* This is where the exercise specifies the extra checks it may require.
4144
*
42-
* @param ExerciseDispatcher $dispatcher
45+
* @return array<class-string>
4346
*/
44-
public function configure(ExerciseDispatcher$dispatcher): void;
47+
public function getRequiredChecks(): array;
4548

4649
/**
4750
* A short description of the exercise.

‎src/ExerciseDispatcher.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ public function requireCheck(string $requiredCheck): void
129129
*/
130130
public function verify(ExerciseInterface $exercise, Input $input): ResultAggregator
131131
{
132-
$exercise->configure($this);
133-
134132
$runner = $this->runnerManager->getRunner($exercise);
135133

136-
foreach ($runner->getRequiredChecks() as $requiredCheck) {
134+
$exercise->defineListeners($this->eventDispatcher);
135+
136+
foreach ([...$runner->getRequiredChecks(), ...$exercise->getRequiredChecks()] as $requiredCheck) {
137137
$this->requireCheck($requiredCheck);
138138
}
139139

@@ -181,7 +181,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
181181
*/
182182
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
183183
{
184-
$exercise->configure($this);
184+
$exercise->defineListeners($this->eventDispatcher);
185185

186186
/** @var PhpLintCheck $lint */
187187
$lint = $this->checkRepository->getByClass(PhpLintCheck::class);

‎test/Asset/CgiExerciseImpl.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

5+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
57
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
68
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
79
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
@@ -62,7 +64,12 @@ public function getType(): ExerciseType
6264
return ExerciseType::CGI();
6365
}
6466

65-
public function configure(ExerciseDispatcher $dispatcher): void
67+
public function getRequiredChecks(): array
68+
{
69+
return [];
70+
}
71+
72+
public function defineListeners(EventDispatcher $dispatcher): void
6673
{
6774
}
6875
}

‎test/Asset/CliExerciseImpl.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

5+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
57
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
68
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
79
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
@@ -66,7 +68,12 @@ public function getType(): ExerciseType
6668
return ExerciseType::CLI();
6769
}
6870

69-
public function configure(ExerciseDispatcher $dispatcher): void
71+
public function getRequiredChecks(): array
72+
{
73+
return [];
74+
}
75+
76+
public function defineListeners(EventDispatcher $dispatcher): void
7077
{
7178
}
7279
}

‎test/Asset/CliExerciseMissingInterface.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

5+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
57
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
68
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
79
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
@@ -31,4 +33,9 @@ public function getType(): ExerciseType
3133
{
3234
return ExerciseType::CLI();
3335
}
36+
37+
public function getRequiredChecks(): array
38+
{
39+
return [];
40+
}
3441
}

‎test/Asset/ComposerExercise.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

55
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
67
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
78
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
89
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
@@ -53,8 +54,12 @@ public function getType(): ExerciseType
5354
return ExerciseType::CLI();
5455
}
5556

56-
public function configure(ExerciseDispatcher $dispatcher): void
57+
public function getRequiredChecks(): array
58+
{
59+
return [ComposerCheck::class];
60+
}
61+
62+
public function defineListeners(EventDispatcher $dispatcher): void
5763
{
58-
$dispatcher->requireCheck(ComposerCheck::class);
5964
}
6065
}

‎test/Asset/ExerciseWithInitialCode.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

5+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
57
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
68
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
79
use PhpSchool\PhpWorkshop\Exercise\ProvidesInitialCode;
@@ -41,13 +43,17 @@ public function getType(): ExerciseType
4143
// TODO: Implement getType() method.
4244
}
4345

44-
public function configure(ExerciseDispatcher$dispatcher): void
46+
public function getInitialCode(): SolutionInterface
4547
{
46-
// TODO: Implement configure() method.
48+
return SingleFileSolution::fromFile(__DIR__ . '/initial-code/init-solution.php');
4749
}
4850

49-
public function getInitialCode(): SolutionInterface
51+
public function getRequiredChecks(): array
52+
{
53+
return [];
54+
}
55+
56+
public function defineListeners(EventDispatcher $dispatcher): void
5057
{
51-
return SingleFileSolution::fromFile(__DIR__ . '/initial-code/init-solution.php');
5258
}
5359
}

‎test/Asset/FileComparisonExercise.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

55
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
6+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
7+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
68
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
79
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
810
use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck;
@@ -66,13 +68,17 @@ public function getType(): ExerciseType
6668
return ExerciseType::CLI();
6769
}
6870

69-
public function configure(ExerciseDispatcher$dispatcher): void
71+
public function getFilesToCompare(): array
7072
{
71-
$dispatcher->requireCheck(ComposerCheck::class);
73+
return$this->files;
7274
}
7375

74-
public function getFilesToCompare(): array
76+
public function getRequiredChecks(): array
77+
{
78+
return [FileComparisonCheck::class];
79+
}
80+
81+
public function defineListeners(EventDispatcher $dispatcher): void
7582
{
76-
return $this->files;
7783
}
7884
}

‎test/Asset/FunctionRequirementsExercise.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace PhpSchool\PhpWorkshopTest\Asset;
44

55
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
6+
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
7+
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
8+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
69
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
710
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
811
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
@@ -45,11 +48,6 @@ public function getType(): ExerciseType
4548
return ExerciseType::CLI();
4649
}
4750

48-
public function configure(ExerciseDispatcher $dispatcher): void
49-
{
50-
$dispatcher->requireCheck(ComposerCheck::class);
51-
}
52-
5351
/**
5452
* @return string[]
5553
*/
@@ -65,4 +63,13 @@ public function getBannedFunctions(): array
6563
{
6664
return ['file'];
6765
}
66+
67+
public function getRequiredChecks(): array
68+
{
69+
return [FunctionRequirementsCheck::class];
70+
}
71+
72+
public function defineListeners(EventDispatcher $dispatcher): void
73+
{
74+
}
6875
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /