18
18
use PhpSchool \PhpWorkshop \Exception \SolutionExecutionException ;
19
19
use PhpSchool \PhpWorkshop \Exercise \CgiExercise ;
20
20
use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
21
+ use PhpSchool \PhpWorkshop \ExerciseRunner \Context \ExecutionContext ;
21
22
use PhpSchool \PhpWorkshop \Input \Input ;
22
23
use PhpSchool \PhpWorkshop \Output \OutputInterface ;
23
24
use PhpSchool \PhpWorkshop \Process \ProcessFactory ;
@@ -63,7 +64,8 @@ class CgiRunner implements ExerciseRunnerInterface
63
64
public function __construct (
64
65
private CgiExercise $ exercise ,
65
66
private EventDispatcher $ eventDispatcher ,
66
- private ProcessFactory $ processFactory
67
+ private ProcessFactory $ processFactory ,
68
+ private EnvironmentManager $ environmentManager
67
69
) {
68
70
}
69
71
@@ -99,37 +101,42 @@ public function getRequiredChecks(): array
99
101
* * cgi.verify.student.executing
100
102
* * cgi.verify.student-execute.fail (if the student's solution fails to execute)
101
103
*
102
- * @param Input $input The command line arguments passed to the command .
104
+ * @param ExecutionContext $context The current execution context, containing the exercise, input and working directories .
103
105
* @return CgiResult The result of the check.
104
106
*/
105
- public function verify (Input $ input ): ResultInterface
107
+ public function verify (ExecutionContext $ context ): ResultInterface
106
108
{
107
109
$ scenario = $ this ->exercise ->defineTestScenario ();
108
110
109
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this ->exercise , $ input ));
111
+ $ this ->environmentManager ->prepareStudent ($ context , $ scenario );
112
+ $ this ->environmentManager ->prepareReference ($ context , $ scenario );
113
+
114
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this ->exercise , $ context ->getInput ()));
110
115
111
116
$ result = new CgiResult (
112
117
array_map (
113
- function (RequestInterface $ request ) use ($ input ) {
114
- return $ this ->doVerify ($ request , $ input );
118
+ function (RequestInterface $ request ) use ($ context ) {
119
+ return $ this ->doVerify ($ request , $ context );
115
120
},
116
121
$ scenario ->getExecutions ()
117
122
)
118
123
);
119
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this ->exercise , $ input ));
124
+
125
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this ->exercise , $ context ->getInput ()));
120
126
return $ result ;
121
127
}
122
128
123
- private function doVerify (RequestInterface $ request , Input $ input ): CgiResultInterface
129
+ private function doVerify (RequestInterface $ request , ExecutionContext $ context ): CgiResultInterface
124
130
{
125
131
try {
126
132
/** @var CgiExecuteEvent $event */
127
133
$ event = $ this ->eventDispatcher ->dispatch (
128
- new CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this ->exercise , $ input , $ request )
134
+ new CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
129
135
);
130
136
$ solutionResponse = $ this ->executePhpFile (
131
- $ input ,
132
- $ this ->exercise ->getSolution ()->getEntryPoint ()->getAbsolutePath (),
137
+ $ context ,
138
+ $ context ->getReferenceExecutionDirectory (),
139
+ $ this ->exercise ->getSolution ()->getEntryPoint ()->getRelativePath (),
133
140
$ event ->getRequest (),
134
141
'reference '
135
142
);
@@ -138,7 +145,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
138
145
new CgiExecuteEvent (
139
146
'cgi.verify.reference-execute.fail ' ,
140
147
$ this ->exercise ,
141
- $ input ,
148
+ $ context -> getInput () ,
142
149
$ request ,
143
150
['exception ' => $ e ]
144
151
)
@@ -149,11 +156,12 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
149
156
try {
150
157
/** @var CgiExecuteEvent $event */
151
158
$ event = $ this ->eventDispatcher ->dispatch (
152
- new CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this ->exercise , $ input , $ request )
159
+ new CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
153
160
);
154
161
$ userResponse = $ this ->executePhpFile (
155
- $ input ,
156
- $ input ->getRequiredArgument ('program ' ),
162
+ $ context ,
163
+ $ context ->getStudentExecutionDirectory (),
164
+ $ context ->getEntryPoint (),
157
165
$ event ->getRequest (),
158
166
'student '
159
167
);
@@ -162,7 +170,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
162
170
new CgiExecuteEvent (
163
171
'cgi.verify.student-execute.fail ' ,
164
172
$ this ->exercise ,
165
- $ input ,
173
+ $ context -> getInput () ,
166
174
$ request ,
167
175
['exception ' => $ e ]
168
176
)
@@ -202,16 +210,17 @@ private function getHeaders(ResponseInterface $response): array
202
210
* @return ResponseInterface
203
211
*/
204
212
private function executePhpFile (
205
- Input $ input ,
213
+ ExecutionContext $ context ,
214
+ string $ workingDirectory ,
206
215
string $ fileName ,
207
216
RequestInterface $ request ,
208
217
string $ type
209
218
): ResponseInterface {
210
- $ process = $ this ->getPhpProcess (dirname ( $ fileName ), basename ( $ fileName) , $ request );
219
+ $ process = $ this ->getPhpProcess ($ workingDirectory , $ fileName , $ request );
211
220
212
221
$ process ->start ();
213
222
$ this ->eventDispatcher ->dispatch (
214
- new CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type ), $ this ->exercise , $ input , $ request )
223
+ new CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type ), $ this ->exercise , $ context -> getInput () , $ request )
215
224
);
216
225
$ process ->wait ();
217
226
@@ -280,25 +289,27 @@ private function getPhpProcess(string $workingDirectory, string $fileName, Reque
280
289
* * cgi.run.student-execute.pre
281
290
* * cgi.run.student.executing
282
291
*
283
- * @param Input $input The command line arguments passed to the command .
292
+ * @param ExecutionContext $context The current execution context, containing the exercise, input and working directories .
284
293
* @param OutputInterface $output A wrapper around STDOUT.
285
294
* @return bool If the solution was successfully executed, eg. exit code was 0.
286
295
*/
287
- public function run (Input $ input , OutputInterface $ output ): bool
296
+ public function run (ExecutionContext $ context , OutputInterface $ output ): bool
288
297
{
289
298
$ scenario = $ this ->exercise ->defineTestScenario ();
290
299
291
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.start ' , $ this ->exercise , $ input ));
300
+ $ this ->environmentManager ->prepareStudent ($ context , $ scenario );
301
+
302
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.start ' , $ this ->exercise , $ context ->getInput ()));
292
303
293
304
$ success = true ;
294
305
foreach ($ scenario ->getExecutions () as $ i => $ request ) {
295
306
/** @var CgiExecuteEvent $event */
296
307
$ event = $ this ->eventDispatcher ->dispatch (
297
- new CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this ->exercise , $ input , $ request )
308
+ new CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
298
309
);
299
310
$ process = $ this ->getPhpProcess (
300
- dirname ( $ input -> getRequiredArgument ( ' program ' ) ),
301
- $ input -> getRequiredArgument ( ' program ' ),
311
+ $ context -> getStudentExecutionDirectory ( ),
312
+ $ context -> getEntryPoint ( ),
302
313
$ event ->getRequest ()
303
314
);
304
315
@@ -307,7 +318,7 @@ public function run(Input $input, OutputInterface $output): bool
307
318
new CgiExecuteEvent (
308
319
'cgi.run.student.executing ' ,
309
320
$ this ->exercise ,
310
- $ input ,
321
+ $ context -> getInput () ,
311
322
$ request ,
312
323
['output ' => $ output ]
313
324
)
@@ -324,10 +335,11 @@ public function run(Input $input, OutputInterface $output): bool
324
335
$ output ->lineBreak ();
325
336
326
337
$ this ->eventDispatcher ->dispatch (
327
- new CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this ->exercise , $ input , $ request )
338
+ new CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this ->exercise , $ context -> getInput () , $ request )
328
339
);
329
340
}
330
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this ->exercise , $ input ));
341
+
342
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this ->exercise , $ context ->getInput ()));
331
343
return $ success ;
332
344
}
333
345
}
0 commit comments