/**
  * Running a custom verifying exercise does nothing. There is no program required, therefore there is nothing
  * to run.
  *
  * @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)
 {
     $message = 'Nothing to run here. This exercise does not require a code solution, ';
     $message .= 'so there is nothing to execute.';
     $output->writeLine($message);
     return true;
 }
Ejemplo n.º 2
0
 /**
  * @return int|void
  */
 public function __invoke()
 {
     $currentExercise = $this->userState->getCurrentExercise();
     $exercise = $this->exerciseRepository->findByName($currentExercise);
     $markDown = file_get_contents($exercise->getProblem());
     $doc = $this->markdownRenderer->render($markDown);
     $doc = str_replace('{appname}', $this->appName, $doc);
     $this->output->write($doc);
 }
Ejemplo n.º 3
0
 /**
  * @return int|void
  */
 public function __invoke()
 {
     if (!$this->userState->isAssignedExercise()) {
         $this->output->printError("No active exercises. Select one from the menu");
         return 1;
     }
     $currentExercise = $this->userState->getCurrentExercise();
     $exercise = $this->exerciseRepository->findByName($currentExercise);
     $markDown = file_get_contents($exercise->getProblem());
     $doc = $this->markdownRenderer->render($markDown);
     $doc = str_replace('{appname}', $this->appName, $doc);
     $this->output->write($doc);
 }
Ejemplo n.º 4
0
 /**
  * @param string $appName
  * @param string $program
  *
  * @return int|void
  */
 public function __invoke($appName, $program)
 {
     if (!file_exists($program)) {
         $this->output->printError(sprintf('Could not run. File: "%s" does not exist', $program));
         return 1;
     }
     $program = realpath($program);
     if (!$this->userState->isAssignedExercise()) {
         $this->output->printError("No active exercises. Select one from the menu");
         return 1;
     }
     $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
     $this->exerciseDispatcher->run($exercise, $program, $this->output);
 }
Ejemplo n.º 5
0
 /**
  * @param CliMenu $menu
  */
 public function __invoke(CliMenu $menu)
 {
     $menu->close();
     $item = $menu->getSelectedItem();
     $exercise = $this->exerciseRepository->findByName($item->getText());
     $exercises = $this->exerciseRepository->findAll();
     $this->userState->setCurrentExercise($item->getText());
     $this->userStateSerializer->serialize($this->userState);
     $numExercises = count($exercises);
     $exerciseIndex = array_search($exercise, $exercises) + 1;
     $output = "\n";
     $output .= $this->color->__invoke(' LEARN YOU THE PHP FOR MUCH WIN! ')->magenta()->bold() . "\n";
     $output .= $this->color->__invoke('*********************************')->magenta()->bold() . "\n";
     $output .= "\n";
     $output .= $this->color->__invoke(" " . $exercise->getName())->yellow()->bold() . "\n";
     $output .= $this->color->__invoke(sprintf(" Exercise %d of %d\n\n", $exerciseIndex, $numExercises))->yellow();
     $content = file_get_contents($exercise->getProblem());
     $doc = $this->markdownRenderer->render($content);
     $doc = str_replace('{appname}', $this->appName, $doc);
     $output .= $doc;
     $output .= "\n";
     $output .= $this->helpLine('To print these instructions again, run', 'print');
     $output .= $this->helpLine('To execute your program in a test environment, run', 'run program.php');
     $output .= $this->helpLine('To verify your program, run', 'verify program.php');
     $output .= $this->helpLine('For help run', 'help');
     $output .= "\n\n";
     $this->output->write($output);
 }
Ejemplo n.º 6
0
 /**
  * @param string $appName
  * @param string $program
  *
  * @return int|void
  */
 public function __invoke($appName, $program)
 {
     if (!file_exists($program)) {
         $this->output->printError(sprintf('Could not verify. File: "%s" does not exist', $program));
         return 1;
     }
     $program = realpath($program);
     if (!$this->userState->isAssignedExercise()) {
         $this->output->printError("No active exercises. Select one from the menu");
         return 1;
     }
     $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
     $results = $this->exerciseDispatcher->verify($exercise, $program);
     if ($results->isSuccessful()) {
         $this->userState->addCompletedExercise($exercise->getName());
         $this->userStateSerializer->serialize($this->userState);
     }
     $this->resultsRenderer->render($results, $exercise, $this->userState, $this->output);
     return $results->isSuccessful() ? 0 : 1;
 }
 /**
  * @return int|void
  */
 public function __invoke()
 {
     if (empty($this->coreContributors)) {
         return;
     }
     $this->output->writeLine($this->color->__invoke("PHP School is bought to you by...")->yellow()->__toString());
     $this->output->emptyLine();
     $this->writeContributors($this->coreContributors);
     if (empty($this->appContributors)) {
         return;
     }
     $this->output->emptyLine();
     $this->output->emptyLine();
     $this->output->writeLine($this->color->__invoke("This workshop is brought to you by...")->yellow()->__toString());
     $this->output->writeLine("");
     $this->writeContributors($this->appContributors);
 }
Ejemplo n.º 8
0
 /**
  * @return void
  */
 public function __invoke()
 {
     $this->output->writeLine($this->color->__invoke('Usage')->yellow()->bold());
     $this->output->writeLine("");
     $this->output->writeLine(sprintf("  %s", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show a menu to interactively select a workshop.");
     $this->output->writeLine(sprintf("  %s print", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Print the instructions for the currently selected workshop.");
     $this->output->writeLine(sprintf("  %s verify program.php", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Verify your program against the expected output.");
     $this->output->writeLine(sprintf("  %s help", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show this help.");
     $this->output->writeLine(sprintf("  %s credits", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show the people who made this happen.");
     $this->output->writeLine("");
     $this->output->writeLine($this->color->__invoke('Having trouble with a PHPSchool exercise?')->yellow()->bold());
     $this->output->writeLine("");
     $this->output->writeLine("  A team of expert helper elves is eagerly waiting to assist you in");
     $this->output->writeLine("  mastering the basics of PHP, simply go to:");
     $this->output->writeLine("    https://github.com/php-school/discussions");
     $this->output->writeLine("  and add a New Issue and let us know what you're having trouble");
     $this->output->writeLine("  with. There are no dumb questions!");
     $this->output->writeLine("");
     $this->output->writeLine("  If you're looking for general help with PHP, the #php");
     $this->output->writeLine("  channel on Freenode IRC is usually a great place to find someone");
     $this->output->writeLine("  willing to help. There is also the PHP StackOverflow Chat:");
     $this->output->writeLine("    https://chat.stackoverflow.com/rooms/11/php");
     $this->output->writeLine("");
     $this->output->writeLine($this->color->__invoke('Found a bug with PHPSchool or just want to contribute?')->yellow()->bold());
     $this->output->writeLine("  The official repository for PHPSchool is:");
     $this->output->writeLine("    https://github.com/php-school/php-workshop");
     $this->output->writeLine("  Feel free to file a bug report or (preferably) a pull request.");
     $this->output->writeLine("");
 }
Ejemplo n.º 9
0
 /**
  * @param CliMenu $menu
  */
 public function __invoke(CliMenu $menu)
 {
     $this->userStateSerializer->serialize(new UserState());
     $this->output->writeLine("Status Reset!");
 }
Ejemplo n.º 10
0
 /**
  * @param ExerciseInterface $exercise
  * @param UserState $userState
  * @param OutputInterface $output
  */
 private function renderSuccessInformation(ExerciseInterface $exercise, UserState $userState, OutputInterface $output)
 {
     $output->emptyLine();
     $output->writeLine($this->style(" PASS!", ['green', 'bg_default', 'bold']));
     $output->emptyLine();
     $output->writeLine("Here's the official solution in case you want to compare notes:");
     $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->writeLine(sprintf('You have %d challenges left.', $numExercises - $completedCount));
     $output->writeLine(sprintf('Type "%s" to show the menu.', $this->appName));
     $output->emptyLine();
 }
Ejemplo n.º 11
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;
 }
Ejemplo n.º 12
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;
 }
Ejemplo n.º 13
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;
 }
Ejemplo n.º 14
0
 /**
  * @param string $fileName
  * @param OutputInterface $output
  * @return bool
  */
 public function run($fileName, OutputInterface $output)
 {
     /** @var CliExecuteEvent $event */
     $event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.user-execute.pre', new ArrayObject($this->exercise->getArgs())));
     $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($fileName, $args);
     $process->start();
     $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.executing', $args, ['output' => $output]));
     $process->wait(function ($outputType, $outputBuffer) use($output) {
         $output->writeLine($outputBuffer);
     });
     return $process->isSuccessful();
 }
Ejemplo n.º 15
0
 /**
  * @param OutputInterface $output
  * @param $string
  * @param array $style
  */
 private function fullWidthBlock(OutputInterface $output, $string, array $style)
 {
     $stringLength = mb_strlen(StringUtil::stripAnsiEscapeSequence($string));
     $stringHalfLength = $stringLength / 2;
     $widthHalfLength = ceil($this->terminal->getWidth() / 2);
     $start = ceil($widthHalfLength - $stringHalfLength);
     $output->writeLine($this->style(str_repeat(' ', $this->terminal->getWidth()), $style));
     $output->writeLine($this->style(sprintf('%s%s%s', str_repeat(' ', $start), $string, str_repeat(' ', $this->terminal->getWidth() - $stringLength - $start)), $style));
     $output->writeLine($this->style(str_repeat(' ', $this->terminal->getWidth()), $style));
 }