Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / src / Commands / core / TopicCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Consolidation\AnnotatedCommand\AnnotatedCommand;
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Drush\Commands\DrushCommands;
7 use Drush\Drush;
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;
13
14 class TopicCommands extends DrushCommands
15 {
16
17     /**
18      * Read detailed documentation on a given topic.
19      *
20      * @command core:topic
21      * @param $topic_name  The name of the topic you wish to view. If omitted, list all topic descriptions (and names in parenthesis).
22      * @usage drush topic
23      *   Pick from all available topics.
24      * @usage drush topic docs-repl
25      *   Show documentation for the Drush interactive shell
26      * @usage drush docs:r
27      *   Filter topics for those starting with 'docs-r'.
28      * @remote-tty
29      * @aliases topic,core-topic
30      * @bootstrap max
31      * @topics docs:readme
32      */
33     public function topic($topic_name)
34     {
35         $application = Drush::getApplication();
36         $input = new ArrayInput([$topic_name], null);
37         return $application->run($input);
38     }
39
40     /**
41      * @hook interact topic
42      */
43     public function interact(InputInterface $input, OutputInterface $output)
44     {
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) {
51                     unset($topics[$key]);
52                 }
53             }
54         }
55         if (count($topics) > 1) {
56             // Show choice list.
57             foreach ($topics as $key => $topic) {
58                 $choices[$key] = $topic->getDescription() . " ($key)";
59             }
60             natcasesort($choices);
61             $topic_name = $this->io()->choice(dt('Choose a topic'), $choices);
62             $input->setArgument('topic_name', $topic_name);
63         }
64     }
65
66     /**
67      * @hook validate topic
68      */
69     public function validate(CommandData $commandData)
70     {
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]));
74         }
75     }
76
77     /**
78      * Retrieve all defined topics
79      *
80      * @return Command[]
81      */
82     public static function getAllTopics()
83     {
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;
93                 }
94             }
95         }
96         return $topics;
97     }
98 }