/**
  * @param Output $output
  * @param string $text
  * @param string $type
  * @param array  $options
  *
  * @return mixed
  */
 public function dialog(Output $output, $text, $type = 'string', array $options = array())
 {
     $output->write($this->getDialogText($text, $type, $options));
     $input = trim(fgets(STDIN));
     if (isset($options['default']) && '' === $input) {
         $input = $options['default'];
     }
     $success = false;
     switch ($type) {
         case 'string':
             $success = true;
             break;
         case 'enum':
             $success = in_array($input, (array) $options['values'], true);
             break;
         case 'bool':
             if (is_bool($input)) {
                 $success = true;
             } elseif (in_array($input, array('y', 'N'), true)) {
                 $input = 'y' === $input ? true : false;
                 $success = true;
             }
             break;
         default:
             throw new InvalidArgumentException(sprintf('Unsupported dialog with type "%s"', $type));
     }
     if (isset($options['required']) && '' === $input || false === $success) {
         return $this->dialog($output, $text, $type, $options);
     }
     return $input;
 }
Ejemplo n.º 2
0
 /**
  * Write border.
  *
  * @param int[] $widths
  */
 private function writeBorders(array $widths)
 {
     foreach ($widths as $width) {
         $this->output->write('+');
         $this->output->write(str_repeat('-', $width));
     }
     $this->output->writeln('+');
 }
 /**
  * Execute console.
  *
  * @param AbstractInput $input
  * @param Output        $output
  */
 protected function execute(AbstractInput $input, Output $output)
 {
     $output->writeln($this->getApplication()->getApplicationVersion());
     $output->write("\n");
     $names = $this->getConsoles();
     $width = $output->getMaxWidth($names) + 4;
     $output->writeln(sprintf('<style name="comment">%s</style>', 'Available commands:'));
     foreach ($names as $name) {
         $description = null;
         $cleanName = strip_tags(trim($name));
         if ($this->application->hasConsole($cleanName)) {
             $description = $this->application->getConsole($cleanName)->getDescription();
         }
         $output->writeln($output->width($width, $name) . '  ' . (string) $description);
     }
 }
 /**
  * Write exception.
  *
  * @param Exception $e
  * @param Output    $output
  */
 protected function writeException(Exception $e, Output $output)
 {
     $class = get_class($e);
     $pos = strrpos($class, '\\');
     $title = sprintf('  [%s]  ', false === $pos ? $class : substr($class, $pos + 1));
     $msg = '  ' . $e->getMessage();
     $max = (strlen($title) > strlen($msg) ? strlen($title) : strlen($msg)) + 2;
     $messages = array();
     $messages[] = $title;
     $messages[] = $msg;
     $output->writeln(sprintf('<style name="error">%s</style>', str_repeat(' ', $max)));
     foreach ($messages as $msg) {
         $output->write(sprintf('<style name="error">%s</style>', $msg));
         $output->write(sprintf('<style name="error">%s</style>', str_repeat(' ', $max - strlen($msg))));
         $output->writeln('');
     }
     $output->writeln(sprintf('<style name="error">%s</style>', str_repeat(' ', $max)));
 }