/** * @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; }
/** * {@inheritdoc} */ protected function handle(JobInterface $job) { try { parent::handle($job); $this->container->get('cqrs.command_bus')->dispatchNow($job); } catch (\Exception $e) { $this->output->writeln(sprintf('<style name="error">%s</style>', $e->getMessage())); } }
/** * Execute console. * * @param AbstractInput $input * @param Output $output */ protected function execute(AbstractInput $input, Output $output) { $address = $input->option->get('address'); $port = $input->option->get('port'); $processControl = new ProcessControl(sprintf('%s:%s', $address, $port)); if (false === $processControl->isRunning()) { throw new RuntimeException(sprintf('There are no active daemon run in "%s:%s"', $address, $port)); } $output->writeln('<style name="info">Shutting down Borobudur development server</style>'); $processControl->kill(); }
/** * Execute console. * * @param AbstractInput $input * @param Output $output */ protected function execute(AbstractInput $input, Output $output) { $class = $input->argument->get($this->getMessageType()); $this->reflection = new ReflectionClass($this->parseClass($class)); /** @var MessageInterface $message */ $message = $this->createMessage((array) json_decode($input->argument->get('data'), true)); $result = $this->getBus()->dispatch($message); if (true === $input->option->get('serialize')) { if (method_exists($result, 'serialize')) { $result = $result->serialize(); } elseif (method_exists($result, 'toArray')) { $result = $result->toArray(); } } $output->writeln(sprintf('<style name="info">Dump result: </style>')); var_dump($result); }
/** * Run console. * * @param AbstractInput $input */ public function run(AbstractInput $input) { $this->output = $this->output ?: new Output(); $input->bind($this->definition); if (true === $input->option->get('no-ansi')) { $this->output->setEnableAnsi(false); } if (true === $input->option->get('help')) { $this->application->writeHelp($this, $input->option->all()); } else { $input->validate(); $args = func_get_args(); array_shift($args); call_user_func_array(array($this, 'execute'), array_merge(array($input, $this->output), $args)); } }
/** * Compute max width. * * @return int[] */ private function computeMaxWidth() { $widths = array(); foreach ($this->headers as $header) { $widths[] = strlen($header->getClean()) + 2; } foreach ($this->rows as $row) { foreach ($row as $index => $cell) { $cells = $this->collectCell($index); $width = $this->output->getMaxWidth($this->normalizeContent($cells)) + 2; if (!isset($widths[$index]) || $widths[$index] < $width) { $widths[$index] = $width; } } } return $widths; }
/** * 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 list of options. * * @param array $options * @param int $width * @param AbstractConsole $console * @param Output $output */ private function writeOptions(array $options, $width, AbstractConsole $console, Output $output) { if (!empty($options)) { $output->writeln(); $output->writeln(sprintf('<style name="comment">%s</style>', 'Options:')); foreach ($options as $option) { $clean = trim(strip_tags($option)); $pos = strpos($clean, '--'); $parts = explode('=', substr($clean, $pos + 2)); $opt = $console->getDefinition()->getOption($parts[0]); $description = $opt->getDescription(); if (null !== ($default = $opt->getDefault())) { if ($description) { $description .= ' '; } $description .= sprintf('<style name="comment">[default: "%s"]</style>', $default); } $output->writeln($output->width($width, $option) . ' ' . $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))); }
/** * Event listener when queue halted. */ public function halted() { $this->output->writeln('Queue worker stopped'); }
/** * Execute console. * * @param AbstractInput $input * @param Output $output */ protected function execute(AbstractInput $input, Output $output) { $output->writeln($this->application->getApplicationVersion()); }
private function sayGreeting() { $this->output->writeln(sprintf('<style name="info">%s</style>', sprintf('Borobudur development server started on http://%s:%s', $this->address, $this->port))); }