-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractExercise.php
More file actions
93 lines (83 loc) · 2.73 KB
/
AbstractExercise.php
File metadata and controls
93 lines (83 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop\Exercise;
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
use ReflectionClass;
/**
* This abstract class implements many of the methods described in `PhpSchool\PhpWorkshop\Exercise\ExerciseInterface`.
* It serves as a good base for an exercise, providing useful defaults for many of the methods.
*/
abstract class AbstractExercise
{
/**
* Get the name of the exercise, like `Hello World!`.
*
* @return string
*/
abstract public function getName(): string;
/**
* This returns a single file solution named `solution.php` which
* should exist in `workshop-root/exercises/<exercise-name>/solution/`.
*
* This method can be overwritten if the solution consists of multiple files,
* see [Directory Solution](https://www.phpschool.io/docs/reference/exercise-solutions#directory-solution) for
* more details.
*
* @return SolutionInterface
*/
public function getSolution(): SolutionInterface
{
return SingleFileSolution::fromFile(
(string) realpath(
sprintf(
'%s/../../exercises/%s/solution/solution.php',
dirname((string) (new ReflectionClass(static::class))->getFileName()),
self::normaliseName($this->getName())
)
)
);
}
/**
* This returns the problem file path, which is assumed to exist in
* `workshop-root/exercises/<exercise-name>/problem/` as a file named `problem.md`.
*
* @return string
*/
public function getProblem(): string
{
$name = self::normaliseName($this->getName());
$dir = dirname((string) (new ReflectionClass(static::class))->getFileName());
return sprintf('%s/../../exercises/%s/problem/problem.md', $dir, $name);
}
/**
* Allows to perform some cleanup after the exercise solution's have been executed, for example
* remove files, close DB connections.
*
* @return void
*/
public function tearDown(): void
{
}
/**
* @param string $name
* @return string
*/
public static function normaliseName(string $name): string
{
return (string) preg_replace('/[^A-Za-z\-]+/', '', str_replace(' ', '-', strtolower($name)));
}
/**
* @return list<class-string>
*/
public function getRequiredChecks(): array
{
return [];
}
public function defineListeners(EventDispatcher $dispatcher): void
{
}
}