run($input); } /** * @hook interact topic */ public function interact(InputInterface $input, OutputInterface $output) { $topics = self::getAllTopics(); $topic_name = $input->getArgument('topic_name'); if (!empty($topic_name)) { // Filter the topics to those matching the query. foreach ($topics as $key => $topic) { if (strstr($key, $topic_name) === false) { unset($topics[$key]); } } } if (count($topics) > 1) { // Show choice list. foreach ($topics as $key => $topic) { $choices[$key] = $topic->getDescription() . " ($key)"; } natcasesort($choices); $topic_name = $this->io()->choice(dt('Choose a topic'), $choices); $input->setArgument('topic_name', $topic_name); } } /** * @hook validate topic */ public function validate(CommandData $commandData) { $topic_name = $commandData->input()->getArgument('topic_name'); if (!in_array($topic_name, array_keys(self::getAllTopics()))) { throw new \Exception(dt("!topic topic not found.", ['!topic' => $topic_name])); } } /** * Retrieve all defined topics * * @return Command[] */ public static function getAllTopics() { /** @var Application $application */ $application = Drush::getApplication(); $all = $application->all(); foreach ($all as $key => $command) { if ($command instanceof AnnotatedCommand) { /** @var \Consolidation\AnnotatedCommand\AnnotationData $annotationData */ $annotationData = $command->getAnnotationData(); if ($annotationData->has('topic')) { $topics[$command->getName()] = $command; } } } return $topics; } }