1717use PhpSchool \PhpWorkshop \Result \Success ;
1818use Psr \Http \Message \RequestInterface ;
1919use Psr \Http \Message \ResponseInterface ;
20+ use RuntimeException ;
2021use Symfony \Component \Process \Process ;
2122use Zend \Diactoros \Response \Serializer as ResponseSerializer ;
2223
2324/**
24- * Class CgiRunner
25+ * The `CGI` runner. This runner executes solutions as if they were behind a web-server. They populate the `$_SERVER`,
26+ * `$_GET` & `$_POST` super globals with information based of the request objects returned from the exercise.
27+ *
2528 * @author Aydin Hassan <aydin@hotmail.co.uk>
2629 */
2730class CgiRunner implements ExerciseRunnerInterface
@@ -38,8 +41,13 @@ class CgiRunner implements ExerciseRunnerInterface
3841 private $ eventDispatcher ;
3942
4043 /**
41- * @param CgiExercise $exercise
42- * @param EventDispatcher $eventDispatcher
44+ * Requires the exercise instance and an event dispatcher. This runner requires the `php-cgi` binary to
45+ * be available. It will check for it's existence in the system's $PATH variable or the same
46+ * folder that the CLI php binary lives in.
47+ *
48+ * @param CgiExercise $exercise The exercise to be invoked.
49+ * @param EventDispatcher $eventDispatcher The event dispatcher.
50+ * @throws RuntimeException If the `php-cgi` binary cannot be found.
4351 */
4452 public function __construct (CgiExercise $ exercise , EventDispatcher $ eventDispatcher )
4553 {
@@ -52,15 +60,15 @@ public function __construct(CgiExercise $exercise, EventDispatcher $eventDispatc
5260 // Try one more time, relying on being in the php binary's directory (where it should be on Windows)
5361 system (sprintf ('%s --version %s ' , $ newPath , $ silence ), $ stillFailedToRun );
5462 if ($ stillFailedToRun ) {
55- throw new \ RuntimeException (
63+ throw new RuntimeException (
5664 'Could not load php-cgi binary. Please install php-cgi using your package manager. '
5765 );
5866 }
5967 }
6068 } else {
6169 @system ('php-cgi --version > /dev/null 2>&1 ' , $ failedToRun );
6270 if ($ failedToRun ) {
63- throw new \ RuntimeException (
71+ throw new RuntimeException (
6472 'Could not load php-cgi binary. Please install php-cgi using your package manager. '
6573 );
6674 }
@@ -192,8 +200,21 @@ private function getProcess($fileName, RequestInterface $request)
192200 }
193201
194202 /**
195- * @param string $fileName
196- * @return ResultInterface
203+ * Verifies a solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
204+ * the information from the request objects returned from the exercise. The exercise can return multiple
205+ * requests so the solution will be invoked for however many requests there are.
206+ *
207+ * Events dispatched (for each request):
208+ *
209+ * * cgi.verify.reference-execute.pre
210+ * * cgi.verify.reference.executing
211+ * * cgi.verify.reference-execute.fail (if the reference solution fails to execute)
212+ * * cgi.verify.student-execute.pre
213+ * * cgi.verify.student.executing
214+ * * cgi.verify.student-execute.fail (if the student's solution fails to execute)
215+ *
216+ * @param string $fileName The absolute path to the student's solution.
217+ * @return ResultInterface The result of the check.
197218 */
198219 public function verify ($ fileName )
199220 {
@@ -209,9 +230,21 @@ function (RequestInterface $request) use ($fileName) {
209230 }
210231
211232 /**
212- * @param string $fileName
213- * @param OutputInterface $output
214- * @return bool
233+ * Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
234+ * the information from the request objects returned from the exercise. The exercise can return multiple
235+ * requests so the solution will be invoked for however many requests there are.
236+ *
237+ * Running only runs the student's solution, the reference solution is not run and no verification is performed,
238+ * the output of the student's solution is written directly to the output.
239+ *
240+ * Events dispatched (for each request):
241+ *
242+ * * cgi.run.student-execute.pre
243+ * * cgi.run.student.executing
244+ *
245+ * @param string $fileName The absolute path to the student's solution.
246+ * @param OutputInterface $output A wrapper around STDOUT.
247+ * @return bool If the solution was successfully executed, eg. exit code was 0.
215248 */
216249 public function run ($ fileName , OutputInterface $ output )
217250 {
0 commit comments