SymfonyCon 2013 Lightning Talk about the Console in 2.5

SymfonyCon 2013 Lightning Talk about the Console in 2.5

9a22d09f92d50fa3d2a16766d0ba52f8?s=128

Fabien Potencier

December 13, 2013
Tweet

Transcript

  1. Easier interaction with the user from a Console application https://secure.flickr.com/photos/sylvain_masson/4195880838

  2. ?hidden answer limited choices validator yes/no question autocompleter number of

    a﬙empts default value multiple values
  3. ask() select() askConfirmation() askAndValidate() askHiddenResponse() AskHiddenResponseAndValidate()

  4. select ask askConfirmation askHiddenResponse AskAndValidate askHiddenResponseAn dValidate a﬙empts Y Y

    Y Y hidden answer Y Y autocompleter Y Y validator Y Y Y default value Y Y Y Y yes/no Y choices Y
  5. AskAQuestionWithALimitedSe tOfChoicesButLetTheUserPro videAFreeTextAnswerIfHeWan tsToButOfCourseHideTheResp onseAndPleaseValidateTheAn swerInAWayThatEmptyReturns APredefinedDefaultValue()

  6. From the Dialog Helper to the Question Helper 2.5

  7. QuestionHelper::ask( OutputInterface $output, Question $question )

  8. use Symfony\Component\Console\Dialog\Question; $helper = $this->getHelper('question'); $question = new Question('Enter your

    full name: '); $name = $helper->ask($output, $question);
  9. $question = new Question('Enter your full name: ', 'me');

  10. $question->setHidden(true);

  11. $question->setAutocompleter(array('one', 'two', '...'));

  12. $question->setMaxAttemps(5);

  13. $question->setValidator(function ($value) { if (!empty($value)) { return $value; } throw

    new \InvalidArgumentException( 'You need to provide a non-empty value.'); });
  14. $question = new ChoiceQuestion('Choose a color', array('red', 'blue'), 'blue'); $question->setMultiselect(true);

    $question->setPrompt(' > '); $question->setErrorMessage("That's not a valid color!");
  15. new ConfirmationQuestion('Is it better?', true); 2.5

  16. Better feedback when running external processes from a Console application

    https://secure.flickr.com/photos/petergorges/3052698754
  17. $process = new Process('ls -lsa'); $process->run(); $output->writeln($process->getOutput());

  18. $callback = function ($type, $buffer) use ($output) { $output->write($buffer); };

    $process->run(OutputInterface::VERBOSITY_VERBOSE < $output- >getVerbosity() ? $callback : null);
  19. $callback = function ($type, $buffer) use ($output, &$startedOut, &$startedErr) {

    if ('err' === $type) { if (!$startedErr) { $output->write("\n<bg=red;fg=white> ERR </> "); $startedErr = true; $startedOut = false; } $output->write(str_replace("\n", "\n<bg=red;fg=white> ERR </> ", $buffer)); } else { if (!$startedOut) { $output->write("\n<bg=green;fg=white> OUT </> "); $startedOut = true; $startedErr = false; } $output->write(str_replace("\n", "\n<bg=green;fg=white> OUT </> ", $buffer)); } };
  20. None
  21. $helper = $this->getHelper('process'); $process = $helper->run($output, 'ls -lsa'); $output->writeln($process->getOutput()); 2.5

  22. $helper->run($output, 'ls -lsa'); $helper->run($output, 'run foobar'); ./app/console ... -vv

  23. $helper->run($output, 'ls -lsa'); ./app/console ... -vvv

  24. ./app/console ... -vvv $helper->run($output, 'run foobar');

  25. $helper = $this->getHelper('process'); $process = new Process('ls -lsa'); $helper->run($output, $process);

    $output->writeln($process->getOutput()); 2.5
  26. $callback = function ($type, $buffer) { error_log($buffer); }; $helper->run($output, $process,

    null, $callback); 2.5
  27. What about debugging HTTP requests?

  28. use Guzzle\Http\Client; $client->get('http://example.com/')->send();

  29. use Guzzle\Http\Client; $client = new Client(); $client->addSubscriber(new GuzzleConsolePlugin( $output, $this->getHelper('debug_formatter'))

    ); $client->get('http://example.com/')->send(); 2.5
  30. ./app/console ... -vv $client->get('http://example.com/')->send(); $client->get('http://symfony.com/foobar')->send();

  31. ./app/console ... -vv $client->get('http://insight.sensiolabs.com/')->send();

  32. None
  33. $formatter = $this->getHelperSet()->get('debug_formatter'); // start a session $start = $formatter->start($uuid,

    $cmd); $output->write($start); // give some feedback $progress = $formatter->progress($uuid, $buffer, $isError); $output->write($progress); // at the end $stop = $formatter->stop($uuid, $message, $isSuccessful); $output->write($stop); 2.5