Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SymfonyCon 2013 Lightning Talk about the Console in 2.5

SymfonyCon 2013 Lightning Talk about the Console in 2.5

Fabien Potencier

December 13, 2013
Tweet

More Decks by Fabien Potencier

Other Decks in Programming

Transcript

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

    View Slide

  2. ?hidden answer
    limited choices
    validator
    yes/no question
    autocompleter
    number of a﬙empts
    default value
    multiple values

    View Slide

  3. ask()
    select()
    askConfirmation()
    askAndValidate()
    askHiddenResponse()
    AskHiddenResponseAndValidate()

    View Slide

  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

    View Slide

  5. AskAQuestionWithALimitedSe
    tOfChoicesButLetTheUserPro
    videAFreeTextAnswerIfHeWan
    tsToButOfCourseHideTheResp
    onseAndPleaseValidateTheAn
    swerInAWayThatEmptyReturns
    APredefinedDefaultValue()

    View Slide

  6. From the
    Dialog Helper
    to the
    Question Helper
    2.5

    View Slide

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

    View Slide

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

    View Slide

  9. $question = new Question('Enter your full name: ', 'me');

    View Slide

  10. $question->setHidden(true);

    View Slide

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

    View Slide

  12. $question->setMaxAttemps(5);

    View Slide

  13. $question->setValidator(function ($value) {
    if (!empty($value)) {
    return $value;
    }
    throw new \InvalidArgumentException(
    'You need to provide a non-empty value.');
    });

    View Slide

  14. $question = new ChoiceQuestion('Choose a color',
    array('red', 'blue'), 'blue');
    $question->setMultiselect(true);
    $question->setPrompt(' > ');
    $question->setErrorMessage("That's not a valid color!");

    View Slide

  15. new ConfirmationQuestion('Is it better?', true);
    2.5

    View Slide

  16. Better feedback
    when running
    external
    processes
    from a
    Console application
    https://secure.flickr.com/photos/petergorges/3052698754

    View Slide

  17. $process = new Process('ls -lsa');
    $process->run();
    $output->writeln($process->getOutput());

    View Slide

  18. $callback = function ($type, $buffer) use ($output) {
    $output->write($buffer);
    };
    $process->run(OutputInterface::VERBOSITY_VERBOSE < $output-
    >getVerbosity() ? $callback : null);

    View Slide

  19. $callback = function ($type, $buffer) use ($output, &$startedOut, &$startedErr) {
    if ('err' === $type) {
    if (!$startedErr) {
    $output->write("\n ERR > ");
    $startedErr = true;
    $startedOut = false;
    }
    $output->write(str_replace("\n", "\n ERR > ", $buffer));
    } else {
    if (!$startedOut) {
    $output->write("\n OUT > ");
    $startedOut = true;
    $startedErr = false;
    }
    $output->write(str_replace("\n", "\n OUT > ",
    $buffer));
    }
    };

    View Slide

  20. View Slide

  21. $helper = $this->getHelper('process');
    $process = $helper->run($output, 'ls -lsa');
    $output->writeln($process->getOutput());
    2.5

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. What about
    debugging
    HTTP requests?

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  32. View Slide

  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

    View Slide