-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExerciseRenderer.php
More file actions
139 lines (119 loc) 路 4.05 KB
/
Copy pathExerciseRenderer.php
File metadata and controls
139 lines (119 loc) 路 4.05 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop;
use Colors\Color;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\PhpWorkshop\Exception\ProblemFileDoesNotExistException;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\UserState\Serializer;
use PhpSchool\PhpWorkshop\UserState\UserState;
/**
* This class is used to render the exercise problem to the student, it also sets the current exercise
* on to the user state object.
*/
class ExerciseRenderer
{
/**
* @var string
*/
private $appName;
/**
* @var ExerciseRepository
*/
private $exerciseRepository;
/**
* @var MarkdownRenderer
*/
private $markdownRenderer;
/**
* @var Color
*/
private $color;
/**
* @var OutputInterface
*/
private $output;
/**
* @var UserState
*/
private $userState;
/**
* @var Serializer
*/
private $userStateSerializer;
/**
* @param string $appName
* @param ExerciseRepository $exerciseRepository
* @param UserState $userState
* @param Serializer $userStateSerializer
* @param MarkdownRenderer $markdownRenderer
* @param Color $color
* @param OutputInterface $output
*/
public function __construct(
string $appName,
ExerciseRepository $exerciseRepository,
UserState $userState,
Serializer $userStateSerializer,
MarkdownRenderer $markdownRenderer,
Color $color,
OutputInterface $output,
) {
$this->appName = $appName;
$this->exerciseRepository = $exerciseRepository;
$this->markdownRenderer = $markdownRenderer;
$this->color = $color;
$this->output = $output;
$this->userState = $userState;
$this->userStateSerializer = $userStateSerializer;
}
/**
* @param CliMenu $menu
*/
public function __invoke(CliMenu $menu): void
{
$item = $menu->getSelectedItem();
$exercise = $this->exerciseRepository->findByName($item->getText());
$exercises = $this->exerciseRepository->findAll();
$this->userState->setCurrentExercise($item->getText());
$this->userStateSerializer->serialize($this->userState);
$numExercises = count($exercises);
$exerciseIndex = ((int) array_search($exercise, $exercises, true)) + 1;
$output = "\n";
$output .= $this->color->__invoke(' LEARN YOU THE PHP FOR MUCH WIN! ')->magenta()->bold() . "\n";
$output .= $this->color->__invoke('*********************************')->magenta()->bold() . "\n";
$output .= "\n";
$output .= $this->color->__invoke(" " . $exercise->getName())->yellow()->bold() . "\n";
$output .= $this->color->__invoke(sprintf(" Exercise %d of %d\n\n", $exerciseIndex, $numExercises))->yellow();
$problemFile = $exercise->getProblem();
if (!is_readable($problemFile)) {
throw ProblemFileDoesNotExistException::fromFile($problemFile);
}
$content = (string) file_get_contents($problemFile);
$doc = $this->markdownRenderer->render($content);
$doc = str_replace('{appname}', $this->appName, $doc);
$output .= $doc;
$output .= "\n";
$output .= $this->helpLine('To print these instructions again, run', 'print');
$output .= $this->helpLine('To execute your program in a test environment, run', 'run program.php');
$output .= $this->helpLine('To verify your program, run', 'verify program.php');
$output .= $this->helpLine('For help run', 'help');
$output .= "\n\n";
$this->output->write($output);
}
/**
* @param string $text
* @param string $cmd
* @return string
*/
private function helpLine(string $text, string $cmd): string
{
$cmd = $this->color->__invoke(sprintf('%s %s', $this->appName, $cmd))->yellow()->__toString();
return sprintf(
" %s %s: %s\n",
$this->color->__invoke("禄")->bold()->__toString(),
$text,
$cmd,
);
}
}