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