1818use PhpSchool \PhpWorkshop \Exception \SolutionExecutionException ;
1919use PhpSchool \PhpWorkshop \Exercise \CgiExercise ;
2020use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
21+ use PhpSchool \PhpWorkshop \ExerciseRunner \Context \ExecutionContext ;
2122use PhpSchool \PhpWorkshop \Input \Input ;
2223use PhpSchool \PhpWorkshop \Output \OutputInterface ;
2324use PhpSchool \PhpWorkshop \Process \ProcessFactory ;
@@ -99,33 +100,34 @@ public function getRequiredChecks(): array
99100 * * cgi.verify.student.executing
100101 * * cgi.verify.student-execute.fail (if the student's solution fails to execute)
101102 *
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 .
103104 * @return CgiResult The result of the check.
104105 */
105- public function verify (Input $ input ): ResultInterface
106+ public function verify (ExecutionContext $ context ): ResultInterface
106107 {
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 () ));
108109 $ result = new CgiResult (
109110 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 );
112113 },
113114 $ this ->exercise ->getRequests ()
114115 )
115116 );
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 () ));
117118 return $ result ;
118119 }
119120
120- private function doVerify (RequestInterface $ request , Input $ input ): CgiResultInterface
121+ private function doVerify (RequestInterface $ request , ExecutionContext $ context ): CgiResultInterface
121122 {
122123 try {
123124 /** @var CgiExecuteEvent $event */
124125 $ 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 )
126127 );
127128 $ solutionResponse = $ this ->executePhpFile (
128- $ input ,
129+ $ context ,
130+ $ context ->getReferenceExecutionDirectory (),
129131 $ this ->exercise ->getSolution ()->getEntryPoint ()->getAbsolutePath (),
130132 $ event ->getRequest (),
131133 'reference '
@@ -135,7 +137,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
135137 new CgiExecuteEvent (
136138 'cgi.verify.reference-execute.fail ' ,
137139 $ this ->exercise ,
138- $ input ,
140+ $ context -> getInput () ,
139141 $ request ,
140142 ['exception ' => $ e ]
141143 )
@@ -146,11 +148,12 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
146148 try {
147149 /** @var CgiExecuteEvent $event */
148150 $ 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 )
150152 );
151153 $ userResponse = $ this ->executePhpFile (
152- $ input ,
153- $ input ->getRequiredArgument ('program ' ),
154+ $ context ,
155+ $ context ->getStudentExecutionDirectory (),
156+ $ context ->getEntryPoint (),
154157 $ event ->getRequest (),
155158 'student '
156159 );
@@ -159,7 +162,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
159162 new CgiExecuteEvent (
160163 'cgi.verify.student-execute.fail ' ,
161164 $ this ->exercise ,
162- $ input ,
165+ $ context -> getInput () ,
163166 $ request ,
164167 ['exception ' => $ e ]
165168 )
@@ -199,16 +202,17 @@ private function getHeaders(ResponseInterface $response): array
199202 * @return ResponseInterface
200203 */
201204 private function executePhpFile (
202- Input $ input ,
205+ ExecutionContext $ context ,
206+ string $ workingDirectory ,
203207 string $ fileName ,
204208 RequestInterface $ request ,
205209 string $ type
206210 ): ResponseInterface {
207- $ process = $ this ->getPhpProcess (dirname ( $ fileName ), basename ( $ fileName) , $ request );
211+ $ process = $ this ->getPhpProcess ($ workingDirectory , $ fileName , $ request );
208212
209213 $ process ->start ();
210214 $ 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 )
212216 );
213217 $ process ->wait ();
214218
@@ -277,22 +281,22 @@ private function getPhpProcess(string $workingDirectory, string $fileName, Reque
277281 * * cgi.run.student-execute.pre
278282 * * cgi.run.student.executing
279283 *
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 .
281285 * @param OutputInterface $output A wrapper around STDOUT.
282286 * @return bool If the solution was successfully executed, eg. exit code was 0.
283287 */
284- public function run (Input $ input , OutputInterface $ output ): bool
288+ public function run (ExecutionContext $ context , OutputInterface $ output ): bool
285289 {
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 () ));
287291 $ success = true ;
288292 foreach ($ this ->exercise ->getRequests () as $ i => $ request ) {
289293 /** @var CgiExecuteEvent $event */
290294 $ 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 )
292296 );
293297 $ process = $ this ->getPhpProcess (
294- dirname ( $ input -> getRequiredArgument ( ' program ' ) ),
295- $ input -> getRequiredArgument ( ' program ' ),
298+ $ context -> getStudentExecutionDirectory ( ),
299+ $ context -> getEntryPoint ( ),
296300 $ event ->getRequest ()
297301 );
298302
@@ -301,7 +305,7 @@ public function run(Input $input, OutputInterface $output): bool
301305 new CgiExecuteEvent (
302306 'cgi.run.student.executing ' ,
303307 $ this ->exercise ,
304- $ input ,
308+ $ context -> getInput () ,
305309 $ request ,
306310 ['output ' => $ output ]
307311 )
@@ -318,10 +322,10 @@ public function run(Input $input, OutputInterface $output): bool
318322 $ output ->lineBreak ();
319323
320324 $ 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 )
322326 );
323327 }
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 () ));
325329 return $ success ;
326330 }
327331}
0 commit comments