Example #1
0
 /**
  * Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
  * the information from the request objects returned from the exercise. The exercise can return multiple
  * requests so the solution will be invoked for however many requests there are.
  *
  * Running only runs the student's solution, the reference solution is not run and no verification is performed,
  * the output of the student's solution is written directly to the output.
  *
  * Events dispatched (for each request):
  *
  * * cgi.run.student-execute.pre
  * * cgi.run.student.executing
  *
  * @param string $fileName The absolute path to the student's solution.
  * @param OutputInterface $output A wrapper around STDOUT.
  * @return bool If the solution was successfully executed, eg. exit code was 0.
  */
 public function run($fileName, OutputInterface $output)
 {
     $success = true;
     foreach ($this->exercise->getRequests() as $i => $request) {
         $event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student-execute.pre', $request));
         $process = $this->getProcess($fileName, $event->getRequest());
         $output->writeTitle("Request");
         $output->emptyLine();
         $output->writeRequest($request);
         $output->writeTitle("Output");
         $output->emptyLine();
         $process->start();
         $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student.executing', $request, ['output' => $output]));
         $process->wait(function ($outputType, $outputBuffer) use($output) {
             $output->write($outputBuffer);
         });
         $output->emptyLine();
         if (!$process->isSuccessful()) {
             $success = false;
         }
         $output->lineBreak();
     }
     return $success;
 }
Example #2
0
 /**
  * Runs a student's solution by invoking PHP from the CLI passing the arguments gathered from the exercise
  * as command line arguments to PHP.
  *
  * Running only runs the student's solution, the reference solution is not run and no verification is performed,
  * the output of the student's solution is written directly to the output.
  *
  * Events dispatched:
  *
  *  * cli.run.student-execute.pre
  *  * cli.run.student.executing
  *
  * @param Input $input The command line arguments passed to the command.
  * @param OutputInterface $output A wrapper around STDOUT.
  * @return bool If the solution was successfully executed, eg. exit code was 0.
  */
 public function run(Input $input, OutputInterface $output)
 {
     $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.start', $this->exercise, $input));
     $success = true;
     foreach ($this->preserveOldArgFormat($this->exercise->getArgs()) as $i => $args) {
         /** @var CliExecuteEvent $event */
         $event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.student-execute.pre', new ArrayObject($args)));
         $args = $event->getArgs();
         if (count($args)) {
             $glue = max(array_map('strlen', $args->getArrayCopy())) > 30 ? "\n" : ', ';
             $output->writeTitle('Arguments');
             $output->write(implode($glue, $args->getArrayCopy()));
             $output->emptyLine();
         }
         $output->writeTitle("Output");
         $process = $this->getPhpProcess($input->getArgument('program'), $args);
         $process->start();
         $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.student.executing', $args, ['output' => $output]));
         $process->wait(function ($outputType, $outputBuffer) use($output) {
             $output->write($outputBuffer);
         });
         $output->emptyLine();
         if (!$process->isSuccessful()) {
             $success = false;
         }
         $output->lineBreak();
     }
     $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.finish', $this->exercise, $input));
     return $success;
 }
Example #3
0
 /**
  * Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
  * the information from the request objects returned from the exercise. The exercise can return multiple
  * requests so the solution will be invoked for however many requests there are.
  *
  * Running only runs the student's solution, the reference solution is not run and no verification is performed,
  * the output of the student's solution is written directly to the output.
  *
  * Events dispatched (for each request):
  *
  * * cgi.run.student-execute.pre
  * * cgi.run.student.executing
  *
  * @param Input $input The command line arguments passed to the command.
  * @param OutputInterface $output A wrapper around STDOUT.
  * @return bool If the solution was successfully executed, eg. exit code was 0.
  */
 public function run(Input $input, OutputInterface $output)
 {
     $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.start', $this->exercise, $input));
     $success = true;
     foreach ($this->exercise->getRequests() as $i => $request) {
         $event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student-execute.pre', $request));
         $process = $this->getProcess($input->getArgument('program'), $event->getRequest());
         $output->writeTitle("Request");
         $output->emptyLine();
         $output->write($this->requestRenderer->renderRequest($request));
         $output->writeTitle("Output");
         $output->emptyLine();
         $process->start();
         $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student.executing', $request, ['output' => $output]));
         $process->wait(function ($outputType, $outputBuffer) use($output) {
             $output->write($outputBuffer);
         });
         $output->emptyLine();
         if (!$process->isSuccessful()) {
             $success = false;
         }
         $output->lineBreak();
     }
     $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input));
     return $success;
 }
 /**
  * @param ExerciseInterface $exercise
  * @param UserState $userState
  * @param OutputInterface $output
  */
 private function renderSuccessInformation(ExerciseInterface $exercise, UserState $userState, OutputInterface $output)
 {
     $output->lineBreak();
     $output->emptyLine();
     $output->emptyLine();
     $this->fullWidthBlock($output, 'PASS!', ['white', 'bg_green', 'bold']);
     $output->emptyLine();
     if ($exercise instanceof ProvidesSolution) {
         $output->writeLine($this->center("Here's the official solution in case you want to compare notes:"));
         $output->emptyLine();
         $output->writeLine($this->lineBreak());
         foreach ($exercise->getSolution()->getFiles() as $file) {
             $output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline']));
             $output->emptyLine();
             $code = $file->getContents();
             if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') {
                 $code = $this->syntaxHighlighter->highlight($code);
             }
             //make sure there is a new line at the end
             $code = preg_replace('/\\n$/', '', $code) . "\n";
             $output->write($code);
             $output->writeLine($this->lineBreak());
         }
     }
     $completedCount = count($userState->getCompletedExercises());
     $numExercises = $this->exerciseRepository->count();
     $output->emptyLine();
     $output->writeLine($this->center(sprintf('You have %d challenges left.', $numExercises - $completedCount)));
     $output->writeLine($this->center(sprintf('Type "%s" and hit enter to show the menu.', $this->appName)));
     $output->emptyLine();
 }