2 namespace Drush\Commands\core;
4 use Consolidation\AnnotatedCommand\AnnotatedCommand;
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Drush\Commands\DrushCommands;
8 use Symfony\Component\Console\Application;
9 use Symfony\Component\Console\Command\Command;
10 use Symfony\Component\Console\Input\ArrayInput;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
14 class TopicCommands extends DrushCommands
18 * Read detailed documentation on a given topic.
21 * @param $topic_name The name of the topic you wish to view. If omitted, list all topic descriptions (and names in parenthesis).
23 * Pick from all available topics.
24 * @usage drush topic docs-repl
25 * Show documentation for the Drush interactive shell
27 * Filter topics for those starting with 'docs-r'.
29 * @aliases topic,core-topic
33 public function topic($topic_name)
35 $application = Drush::getApplication();
36 $input = new ArrayInput([$topic_name], null);
37 return $application->run($input);
41 * @hook interact topic
43 public function interact(InputInterface $input, OutputInterface $output)
45 $topics = self::getAllTopics();
46 $topic_name = $input->getArgument('topic_name');
47 if (!empty($topic_name)) {
48 // Filter the topics to those matching the query.
49 foreach ($topics as $key => $topic) {
50 if (strstr($key, $topic_name) === false) {
55 if (count($topics) > 1) {
57 foreach ($topics as $key => $topic) {
58 $choices[$key] = $topic->getDescription() . " ($key)";
60 natcasesort($choices);
61 $topic_name = $this->io()->choice(dt('Choose a topic'), $choices);
62 $input->setArgument('topic_name', $topic_name);
67 * @hook validate topic
69 public function validate(CommandData $commandData)
71 $topic_name = $commandData->input()->getArgument('topic_name');
72 if (!in_array($topic_name, array_keys(self::getAllTopics()))) {
73 throw new \Exception(dt("!topic topic not found.", ['!topic' => $topic_name]));
78 * Retrieve all defined topics
82 public static function getAllTopics()
84 /** @var Application $application */
85 $application = Drush::getApplication();
86 $all = $application->all();
87 foreach ($all as $key => $command) {
88 if ($command instanceof AnnotatedCommand) {
89 /** @var \Consolidation\AnnotatedCommand\AnnotationData $annotationData */
90 $annotationData = $command->getAnnotationData();
91 if ($annotationData->has('topic')) {
92 $topics[$command->getName()] = $command;