123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Command\Concerns;
- use Closure;
- use Hyperf\Contract\Arrayable;
- use Hyperf\Stringable\Str;
- use Symfony\Component\Console\Formatter\OutputFormatterStyle;
- use Symfony\Component\Console\Helper\Table;
- use Symfony\Component\Console\Helper\TableStyle;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Question\ChoiceQuestion;
- use Symfony\Component\Console\Question\Question;
- use Symfony\Component\Console\Style\SymfonyStyle;
- trait InteractsWithIO
- {
- protected ?InputInterface $input = null;
- /**
- * @var null|SymfonyStyle
- */
- protected ?OutputInterface $output = null;
- /**
- * The default verbosity of output commands.
- */
- protected int $verbosity = OutputInterface::VERBOSITY_NORMAL;
- /**
- * The mapping between human readable verbosity levels and Symfony's OutputInterface.
- */
- protected array $verbosityMap = [
- 'v' => OutputInterface::VERBOSITY_VERBOSE,
- 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
- 'vvv' => OutputInterface::VERBOSITY_DEBUG,
- 'quiet' => OutputInterface::VERBOSITY_QUIET,
- 'normal' => OutputInterface::VERBOSITY_NORMAL,
- ];
- /**
- * Determine if the given argument is present.
- *
- * @param int|string $name
- * @return bool
- */
- public function hasArgument($name)
- {
- return $this->input->hasArgument($name);
- }
- /**
- * Get the value of a command argument.
- *
- * @param null|string $key
- * @return null|array|bool|string
- */
- public function argument($key = null)
- {
- if (is_null($key)) {
- return $this->input->getArguments();
- }
- return $this->input->getArgument($key);
- }
- /**
- * Get all of the arguments passed to the command.
- *
- * @return array
- */
- public function arguments()
- {
- return $this->argument();
- }
- /**
- * Determine if the given option is present.
- *
- * @param string $name
- * @return bool
- */
- public function hasOption($name)
- {
- return $this->input->hasOption($name);
- }
- /**
- * Get the value of a command option.
- *
- * @param null|string $key
- * @return null|array|bool|string
- */
- public function option($key = null)
- {
- if (is_null($key)) {
- return $this->input->getOptions();
- }
- return $this->input->getOption($key);
- }
- /**
- * Get all of the options passed to the command.
- *
- * @return array
- */
- public function options()
- {
- return $this->option();
- }
- /**
- * Confirm a question with the user.
- *
- * @param string $question
- * @param bool $default
- * @return bool
- */
- public function confirm($question, $default = false)
- {
- return $this->output?->confirm($question, $default);
- }
- /**
- * Prompt the user for input.
- *
- * @param string $question
- * @param null|string $default
- * @return mixed
- */
- public function ask($question, $default = null)
- {
- return $this->output?->ask($question, $default);
- }
- /**
- * Prompt the user for input with auto completion.
- *
- * @param string $question
- * @param array|callable $choices
- * @param null|string $default
- * @return mixed
- */
- public function anticipate($question, $choices, $default = null)
- {
- return $this->askWithCompletion($question, $choices, $default);
- }
- /**
- * Prompt the user for input with auto completion.
- *
- * @param string $question
- * @param array|callable $choices
- * @param null|string $default
- * @return mixed
- */
- public function askWithCompletion($question, $choices, $default = null)
- {
- $question = new Question($question, $default);
- is_callable($choices)
- ? $question->setAutocompleterCallback($choices)
- : $question->setAutocompleterValues($choices);
- return $this->output?->askQuestion($question);
- }
- /**
- * Prompt the user for input but hide the answer from the console.
- *
- * @param string $question
- * @param bool $fallback
- * @return mixed
- */
- public function secret($question, $fallback = true)
- {
- $question = new Question($question);
- $question->setHidden(true)->setHiddenFallback($fallback);
- return $this->output?->askQuestion($question);
- }
- /**
- * Give the user a single choice from an array of answers.
- *
- * @param string $question
- * @param null|int|string $default
- * @param null|mixed $attempts
- * @param bool $multiple
- * @return array|string
- */
- public function choice($question, array $choices, $default = null, $attempts = null, $multiple = false)
- {
- $question = new ChoiceQuestion($question, $choices, $default);
- $question->setMaxAttempts($attempts)->setMultiselect($multiple);
- return $this->output?->askQuestion($question);
- }
- /**
- * Format input to textual table.
- *
- * @param array $headers
- * @param array|Arrayable $rows
- * @param string|TableStyle $tableStyle
- */
- public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
- {
- $table = new Table($this->output);
- if ($rows instanceof Arrayable) {
- $rows = $rows->toArray();
- }
- $table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle);
- foreach ($columnStyles as $columnIndex => $columnStyle) {
- $table->setColumnStyle($columnIndex, $columnStyle);
- }
- $table->render();
- }
- /**
- * Execute a given callback while advancing a progress bar.
- *
- * @param int|iterable $totalSteps
- * @return mixed|void
- */
- public function withProgressBar($totalSteps, Closure $callback)
- {
- $bar = $this->output?->createProgressBar(
- is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
- );
- $bar->start();
- if (is_iterable($totalSteps)) {
- foreach ($totalSteps as $value) {
- $callback($value, $bar);
- $bar->advance();
- }
- } else {
- $callback($bar);
- }
- $bar->finish();
- if (is_iterable($totalSteps)) {
- return $totalSteps;
- }
- }
- /**
- * Write a string as information output.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function info($string, $verbosity = null)
- {
- $this->line($string, 'info', $verbosity);
- }
- /**
- * Write a string as standard output.
- *
- * @param string $string
- * @param null|string $style
- * @param null|int|string $verbosity
- */
- public function line($string, $style = null, $verbosity = null)
- {
- $styled = $style ? "<{$style}>{$string}</{$style}>" : $string;
- $this->output?->writeln($styled, $this->parseVerbosity($verbosity));
- }
- /**
- * Write a string as comment output.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function comment($string, $verbosity = null)
- {
- $this->line($string, 'comment', $verbosity);
- }
- /**
- * Write a string as question output.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function question($string, $verbosity = null)
- {
- $this->line($string, 'question', $verbosity);
- }
- /**
- * Write a string as error output.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function error($string, $verbosity = null)
- {
- $this->line($string, 'error', $verbosity);
- }
- /**
- * Write a string as warning output.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function warn($string, $verbosity = null)
- {
- if (! $this->output?->getFormatter()->hasStyle('warning')) {
- $style = new OutputFormatterStyle('yellow');
- $this->output?->getFormatter()->setStyle('warning', $style);
- }
- $this->line($string, 'warning', $verbosity);
- }
- /**
- * Write a string in an alert box.
- *
- * @param string $string
- * @param null|int|string $verbosity
- */
- public function alert($string, $verbosity = null)
- {
- $length = Str::length(strip_tags($string)) + 12;
- $this->comment(str_repeat('*', $length), $verbosity);
- $this->comment('* ' . $string . ' *', $verbosity);
- $this->comment(str_repeat('*', $length), $verbosity);
- $this->comment('', $verbosity);
- }
- /**
- * Write a blank line.
- *
- * @param int $count
- * @return $this
- */
- public function newLine($count = 1)
- {
- $this->output?->newLine($count);
- return $this;
- }
- /**
- * Set the input interface implementation.
- */
- public function setInput(InputInterface $input)
- {
- $this->input = $input;
- }
- /**
- * Set the output interface implementation.
- * @param SymfonyStyle $output
- */
- public function setOutput($output)
- {
- $this->output = $output;
- }
- /**
- * Get the output implementation.
- *
- * @return null|SymfonyStyle
- */
- public function getOutput()
- {
- return $this->output;
- }
- /**
- * Set the verbosity level.
- *
- * @param int|string $level
- */
- protected function setVerbosity($level)
- {
- $this->verbosity = $this->parseVerbosity($level);
- }
- /**
- * Get the verbosity level in terms of Symfony's OutputInterface level.
- *
- * @param null|int|string $level
- * @return int
- */
- protected function parseVerbosity($level = null)
- {
- if (isset($this->verbosityMap[$level])) {
- $level = $this->verbosityMap[$level];
- } elseif (! is_int($level)) {
- $level = $this->verbosity;
- }
- return $level;
- }
- }
|