setName(self::ROUTE) ->setDescription('Add new backend.') ->addArgument('backend', InputArgument::OPTIONAL, 'Backend name', null) ->addOption('select-backend', 's', InputOption::VALUE_REQUIRED, 'Select backend.') ->setHelp( r( <<interaction to work. This command is shortcut for running the following command: {cmd} {manage_route} --add -s backend_name HELP, [ 'cmd' => trim(commandContext()), 'route' => self::ROUTE, 'manage_route' => ManageCommand::ROUTE, ] ) ); } /** * Executes the command. * * @param InputInterface $input The input object. * @param OutputInterface $output The output object. * * @return int The exit code. * @throws \Symfony\Component\Console\Exception\ExceptionInterface If an error occurs during command execution. */ protected function runCommand(InputInterface $input, OutputInterface $output): int { if (function_exists('stream_isatty') && defined('STDERR')) { $tty = stream_isatty(STDERR); } else { $tty = true; } if (false === $tty || $input->getOption('no-interaction')) { $output->writeln( r( <<ERROR: This command require interaction. For example: {cmd} {route} ERROR, [ 'cmd' => trim(commandContext()), 'route' => self::ROUTE, ] ) ); return self::FAILURE; } $opts = [ '--add' => true, ]; foreach ($input->getOptions() as $option => $val) { if (null === $val) { continue; } $opts['--' . $option] = $val; } if (null !== ($name = $input->getOption('select-backend'))) { $name = explode(',', $name, 2)[0]; } if (empty($name) && null !== ($name = $input->getArgument('backend'))) { $name = $input->getArgument('backend'); $output->writeln( 'WARNING: The use of backend name as argument is deprecated and will be removed from future versions. Please use [-s, --select-backend] option instead.' ); } if (null !== $name) { $opts['backend'] = strtolower($name); } else { // -- $backend.token $opts['backend'] = (function () use (&$opts, $input, $output) { $chosen = ag($opts, 'backend'); $question = new Question( <<What should we be calling this backend? ------------------ Backend name is used to identify the backend. The backend name must only contains lower case a-z, 0-9 and _. ------------------ Choose good name to identify your backend. For example, home_plex. HELP. PHP_EOL . '> ', $chosen ); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Backend Name cannot be empty.'); } if (!isValidName($answer) || strtolower($answer) !== $answer) { throw new \RuntimeException( r( 'ERROR: Invalid [{name}] name was given. Only [a-z, 0-9, _] are allowed.', [ 'name' => $answer ], ) ); } return $answer; }); return (new QuestionHelper())->ask($input, $output, $question); })(); $output->writeln(''); $opts['--select-backend'] = strtolower($opts['backend']); } return $this->getApplication()?->find(ManageCommand::ROUTE)->run(new ArrayInput($opts), $output) ?? 1; } }