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 ;
@@ -99,36 +100,36 @@ public function getRequiredChecks(): array
99
100
* * cgi.verify.student.executing
100
101
* * cgi.verify.student-execute.fail (if the student's solution fails to execute)
101
102
*
102
- * @param Input $input The command line arguments passed to the command .
103
+ * @param ExecutionContext $context The current execution context, containing the exercise, input and working directories .
103
104
* @return CgiResult The result of the check.
104
105
*/
105
- public function verify (Input $ input ): ResultInterface
106
+ public function verify (ExecutionContext $ context ): ResultInterface
106
107
{
107
108
$ scenario = $ this ->exercise ->defineTestScenario ();
108
109
109
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this ->exercise , $ input ));
110
-
110
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this ->exercise , $ context ->getInput ()));
111
111
$ result = new CgiResult (
112
112
array_map (
113
- function (RequestInterface $ request ) use ($ input ) {
114
- return $ this ->doVerify ($ request , $ input );
113
+ function (RequestInterface $ request ) use ($ context ) {
114
+ return $ this ->doVerify ($ request , $ context );
115
115
},
116
116
$ scenario ->getExecutions ()
117
117
)
118
118
);
119
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this ->exercise , $ input ));
119
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this ->exercise , $ context -> getInput () ));
120
120
return $ result ;
121
121
}
122
122
123
- private function doVerify (RequestInterface $ request , Input $ input ): CgiResultInterface
123
+ private function doVerify (RequestInterface $ request , ExecutionContext $ context ): CgiResultInterface
124
124
{
125
125
try {
126
126
/** @var CgiExecuteEvent $event */
127
127
$ event = $ this ->eventDispatcher ->dispatch (
128
- new CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this ->exercise , $ input , $ request )
128
+ new CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
129
129
);
130
130
$ solutionResponse = $ this ->executePhpFile (
131
- $ input ,
131
+ $ context ,
132
+ $ context ->getReferenceExecutionDirectory (),
132
133
$ this ->exercise ->getSolution ()->getEntryPoint ()->getAbsolutePath (),
133
134
$ event ->getRequest (),
134
135
'reference '
@@ -138,7 +139,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
138
139
new CgiExecuteEvent (
139
140
'cgi.verify.reference-execute.fail ' ,
140
141
$ this ->exercise ,
141
- $ input ,
142
+ $ context -> getInput () ,
142
143
$ request ,
143
144
['exception ' => $ e ]
144
145
)
@@ -149,11 +150,12 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
149
150
try {
150
151
/** @var CgiExecuteEvent $event */
151
152
$ event = $ this ->eventDispatcher ->dispatch (
152
- new CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this ->exercise , $ input , $ request )
153
+ new CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
153
154
);
154
155
$ userResponse = $ this ->executePhpFile (
155
- $ input ,
156
- $ input ->getRequiredArgument ('program ' ),
156
+ $ context ,
157
+ $ context ->getStudentExecutionDirectory (),
158
+ $ context ->getEntryPoint (),
157
159
$ event ->getRequest (),
158
160
'student '
159
161
);
@@ -162,7 +164,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
162
164
new CgiExecuteEvent (
163
165
'cgi.verify.student-execute.fail ' ,
164
166
$ this ->exercise ,
165
- $ input ,
167
+ $ context -> getInput () ,
166
168
$ request ,
167
169
['exception ' => $ e ]
168
170
)
@@ -202,16 +204,17 @@ private function getHeaders(ResponseInterface $response): array
202
204
* @return ResponseInterface
203
205
*/
204
206
private function executePhpFile (
205
- Input $ input ,
207
+ ExecutionContext $ context ,
208
+ string $ workingDirectory ,
206
209
string $ fileName ,
207
210
RequestInterface $ request ,
208
211
string $ type
209
212
): ResponseInterface {
210
- $ process = $ this ->getPhpProcess (dirname ( $ fileName ), basename ( $ fileName) , $ request );
213
+ $ process = $ this ->getPhpProcess ($ workingDirectory , $ fileName , $ request );
211
214
212
215
$ process ->start ();
213
216
$ this ->eventDispatcher ->dispatch (
214
- new CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type ), $ this ->exercise , $ input , $ request )
217
+ new CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type ), $ this ->exercise , $ context -> getInput () , $ request )
215
218
);
216
219
$ process ->wait ();
217
220
@@ -280,25 +283,24 @@ private function getPhpProcess(string $workingDirectory, string $fileName, Reque
280
283
* * cgi.run.student-execute.pre
281
284
* * cgi.run.student.executing
282
285
*
283
- * @param Input $input The command line arguments passed to the command .
286
+ * @param ExecutionContext $context The current execution context, containing the exercise, input and working directories .
284
287
* @param OutputInterface $output A wrapper around STDOUT.
285
288
* @return bool If the solution was successfully executed, eg. exit code was 0.
286
289
*/
287
- public function run (Input $ input , OutputInterface $ output ): bool
290
+ public function run (ExecutionContext $ context , OutputInterface $ output ): bool
288
291
{
289
292
$ scenario = $ this ->exercise ->defineTestScenario ();
290
293
291
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.start ' , $ this ->exercise , $ input ));
292
-
294
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.start ' , $ this ->exercise , $ context ->getInput ()));
293
295
$ success = true ;
294
296
foreach ($ scenario ->getExecutions () as $ i => $ request ) {
295
297
/** @var CgiExecuteEvent $event */
296
298
$ event = $ this ->eventDispatcher ->dispatch (
297
- new CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this ->exercise , $ input , $ request )
299
+ new CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this ->exercise , $ context -> getInput () , $ request )
298
300
);
299
301
$ process = $ this ->getPhpProcess (
300
- dirname ( $ input -> getRequiredArgument ( ' program ' ) ),
301
- $ input -> getRequiredArgument ( ' program ' ),
302
+ $ context -> getStudentExecutionDirectory ( ),
303
+ $ context -> getEntryPoint ( ),
302
304
$ event ->getRequest ()
303
305
);
304
306
@@ -307,7 +309,7 @@ public function run(Input $input, OutputInterface $output): bool
307
309
new CgiExecuteEvent (
308
310
'cgi.run.student.executing ' ,
309
311
$ this ->exercise ,
310
- $ input ,
312
+ $ context -> getInput () ,
311
313
$ request ,
312
314
['output ' => $ output ]
313
315
)
@@ -324,10 +326,10 @@ public function run(Input $input, OutputInterface $output): bool
324
326
$ output ->lineBreak ();
325
327
326
328
$ this ->eventDispatcher ->dispatch (
327
- new CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this ->exercise , $ input , $ request )
329
+ new CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this ->exercise , $ context -> getInput () , $ request )
328
330
);
329
331
}
330
- $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this ->exercise , $ input ));
332
+ $ this ->eventDispatcher ->dispatch (new CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this ->exercise , $ context -> getInput () ));
331
333
return $ success ;
332
334
}
333
335
}
0 commit comments