/**
  * Executes the current command.
  *
  * This method is not abstract because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws LogicException When this abstract method is not implemented
  *
  * @see setCode()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $num = $input->getArgument('num');
     try {
         $pb = ProblemFactory::get(intval($num));
     } catch (ProblemFactoryException $e) {
         $output->writeln('');
         $output->writeln('<error>Error : problem #' . $num . ' not found !</error>');
         $output->writeln('');
         return;
     }
     $output->writeln('Problem #' . $pb->getId() . ': ' . $pb->getTitle());
     $output->writeln('<comment>' . $pb->getDescription() . '</comment>');
     $output->writeln('');
     $output->writeln('<info>Result: ' . $pb->getSolution() . '</info>');
     $time = $pb->getRoundTime();
     $output->writeln('Time: ' . $time . ' s');
 }
Example #2
0
use ProjectEuler\Problem\ProblemFactory;
use ProjectEuler\Problem\ProblemFactoryException;
use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
/**
 * Project Euler command class.
 *
 * @category  Project Euler in PHP
 *
 * @author    Francois-Xavier Soubirou <*****@*****.**>
 * @copyright 2016 Francois-Xavier Soubirou
 * @license   http://www.gnu.org/licenses/   GPLv3
 *
 * @link      https://projecteuler.net/
 * @since     1
 */
$app = new Application();
// define route for /resolve/{id}
$app->get('/resolve/{id}', function ($id) {
    try {
        $pb = ProblemFactory::get(intval($id));
    } catch (ProblemFactoryException $e) {
        return new Response("Problem #{$id} not found.", 404);
    }
    return $pb->toJson();
})->assert('id', '\\d+');
// default route
$app->get('/', function () {
    return "List of available methods:\n  - /resolve/{id} - Resolution for Project Euler problem id";
});
$app->run();