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 ;
@@ -63,7 +64,8 @@ class CgiRunner implements ExerciseRunnerInterface
6364 public  function  __construct (
6465 private  CgiExercise $ exercise
6566 private  EventDispatcher $ eventDispatcher
66-  private  ProcessFactory $ processFactory
67+  private  ProcessFactory $ processFactory
68+  private  EnvironmentManager $ environmentManager
6769 ) {
6870 }
6971
@@ -99,36 +101,43 @@ public function getRequiredChecks(): array
99101 * * cgi.verify.student.executing 
100102 * * cgi.verify.student-execute.fail (if the student's solution fails to execute) 
101103 * 
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 . 
103105 * @return CgiResult The result of the check. 
104106 */ 
105-  public  function  verify (Input $ input ResultInterface 
107+  public  function  verify (ExecutionContext $ context ResultInterface 
106108 {
107109 $ scenario$ this exercise ->defineTestScenario ();
108110
109-  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this exercise , $ input
111+  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.verify.start ' , $ this exercise , $ contextgetInput ()));
112+ 113+  $ this environmentManager ->prepareStudent ($ context$ scenario
114+  $ this environmentManager ->prepareReference ($ context$ scenario
110115
111116 $ resultnew  CgiResult (
112117 array_map (
113-  function  (RequestInterface $ requestuse  ($ input 
114-  return  $ this doVerify ($ request$ input 
118+  function  (RequestInterface $ requestuse  ($ context 
119+  return  $ this doVerify ($ request$ context 
115120 },
116121 $ scenariogetExecutions ()
117122 )
118123 );
119-  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this exercise , $ input
124+ 125+  $ this environmentManager ->cleanup ($ context$ scenario
126+ 127+  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.verify.finish ' , $ this exercise , $ contextgetInput ()));
120128 return  $ result
121129 }
122130
123-  private  function  doVerify (RequestInterface $ requestInput $ input CgiResultInterface 
131+  private  function  doVerify (RequestInterface $ requestExecutionContext $ context CgiResultInterface 
124132 {
125133 try  {
126134 /** @var CgiExecuteEvent $event */ 
127135 $ event$ this eventDispatcher ->dispatch (
128-  new  CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this exercise , $ input $ request
136+  new  CgiExecuteEvent ('cgi.verify.reference-execute.pre ' , $ this exercise , $ context -> getInput () , $ request
129137 );
130138 $ solutionResponse$ this executePhpFile (
131-  $ input
139+  $ context
140+  $ contextgetReferenceExecutionDirectory (),
132141 $ this exercise ->getSolution ()->getEntryPoint ()->getAbsolutePath (),
133142 $ eventgetRequest (),
134143 'reference ' 
@@ -138,7 +147,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
138147 new  CgiExecuteEvent (
139148 'cgi.verify.reference-execute.fail ' ,
140149 $ this exercise ,
141-  $ input 
150+  $ context -> getInput () ,
142151 $ request
143152 ['exception '  => $ e
144153 )
@@ -149,11 +158,12 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
149158 try  {
150159 /** @var CgiExecuteEvent $event */ 
151160 $ event$ this eventDispatcher ->dispatch (
152-  new  CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this exercise , $ input $ request
161+  new  CgiExecuteEvent ('cgi.verify.student-execute.pre ' , $ this exercise , $ context -> getInput () , $ request
153162 );
154163 $ userResponse$ this executePhpFile (
155-  $ input
156-  $ inputgetRequiredArgument ('program ' ),
164+  $ context
165+  $ contextgetStudentExecutionDirectory (),
166+  $ contextgetEntryPoint (),
157167 $ eventgetRequest (),
158168 'student ' 
159169 );
@@ -162,7 +172,7 @@ private function doVerify(RequestInterface $request, Input $input): CgiResultInt
162172 new  CgiExecuteEvent (
163173 'cgi.verify.student-execute.fail ' ,
164174 $ this exercise ,
165-  $ input 
175+  $ context -> getInput () ,
166176 $ request
167177 ['exception '  => $ e
168178 )
@@ -202,16 +212,17 @@ private function getHeaders(ResponseInterface $response): array
202212 * @return ResponseInterface 
203213 */ 
204214 private  function  executePhpFile (
205-  Input $ input
215+  ExecutionContext $ context
216+  string  $ workingDirectory
206217 string  $ fileName
207218 RequestInterface $ request
208219 string  $ type
209220 ): ResponseInterface 
210-  $ process$ this getPhpProcess (dirname ( $ fileName ),  basename ( $ fileName) , $ request
221+  $ process$ this getPhpProcess ($ workingDirectory ,  $ fileName$ request
211222
212223 $ processstart ();
213224 $ this eventDispatcher ->dispatch (
214-  new  CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type$ this exercise , $ input $ request
225+  new  CgiExecuteEvent (sprintf ('cgi.verify.%s.executing ' , $ type$ this exercise , $ context -> getInput () , $ request
215226 );
216227 $ processwait ();
217228
@@ -280,25 +291,27 @@ private function getPhpProcess(string $workingDirectory, string $fileName, Reque
280291 * * cgi.run.student-execute.pre 
281292 * * cgi.run.student.executing 
282293 * 
283-  * @param Input $input  The command line arguments passed to  the command . 
294+  * @param ExecutionContext $context  The current execution context, containing  the exercise, input and working directories . 
284295 * @param OutputInterface $output A wrapper around STDOUT. 
285296 * @return bool If the solution was successfully executed, eg. exit code was 0. 
286297 */ 
287-  public  function  run (Input $ input OutputInterface $ outputbool 
298+  public  function  run (ExecutionContext $ context OutputInterface $ outputbool 
288299 {
289300 $ scenario$ this exercise ->defineTestScenario ();
290301
291-  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.run.start ' , $ this exercise , $ input
302+  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.run.start ' , $ this exercise , $ contextgetInput ()));
303+ 304+  $ this environmentManager ->prepareStudent ($ context$ scenario
292305
293306 $ successtrue ;
294307 foreach  ($ scenariogetExecutions () as  $ i$ request
295308 /** @var CgiExecuteEvent $event */ 
296309 $ event$ this eventDispatcher ->dispatch (
297-  new  CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this exercise , $ input $ request
310+  new  CgiExecuteEvent ('cgi.run.student-execute.pre ' , $ this exercise , $ context -> getInput () , $ request
298311 );
299312 $ process$ this getPhpProcess (
300-  dirname ( $ input -> getRequiredArgument ( ' program ' ) ),
301-  $ input -> getRequiredArgument ( ' program ' 
313+  $ context -> getStudentExecutionDirectory ( ),
314+  $ context -> getEntryPoint ( ),
302315 $ eventgetRequest ()
303316 );
304317
@@ -307,7 +320,7 @@ public function run(Input $input, OutputInterface $output): bool
307320 new  CgiExecuteEvent (
308321 'cgi.run.student.executing ' ,
309322 $ this exercise ,
310-  $ input 
323+  $ context -> getInput () ,
311324 $ request
312325 ['output '  => $ output
313326 )
@@ -324,10 +337,13 @@ public function run(Input $input, OutputInterface $output): bool
324337 $ outputlineBreak ();
325338
326339 $ this eventDispatcher ->dispatch (
327-  new  CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this exercise , $ input $ request
340+  new  CgiExecuteEvent ('cgi.run.student-execute.post ' , $ this exercise , $ context -> getInput () , $ request
328341 );
329342 }
330-  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this exercise , $ input
343+ 344+  $ this environmentManager ->cleanup ($ context$ scenario
345+ 346+  $ this eventDispatcher ->dispatch (new  CgiExerciseRunnerEvent ('cgi.run.finish ' , $ this exercise , $ contextgetInput ()));
331347 return  $ success
332348 }
333349}
0 commit comments