Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

AskAQuestionWithALimitedSe tOfChoicesButLetTheUserPro videAFreeTextAnswerIfHeWan tsToButOfCourseHideTheResp onseAndPleaseValidateTheAn swerInAWayThatEmptyReturns APredefinedDefaultValue()

Slide 6

Slide 6 text

From the Dialog Helper to the Question Helper 2.5

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

$question->setHidden(true);

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

$question->setMaxAttemps(5);

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

$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)); } };

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

What about debugging HTTP requests?

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

use Guzzle\Http\Client; $client = new Client(); $client->addSubscriber(new GuzzleConsolePlugin( $output, $this->getHelper('debug_formatter')) ); $client->get('http://example.com/')->send(); 2.5

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

$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