Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / examples / Commands / ArtCommands.php
1 <?php
2 namespace Drush\Commands;
3
4 use Consolidation\AnnotatedCommand\AnnotationData;
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface;
7 use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait;
8 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Output\OutputInterface;
11 use Symfony\Component\Console\Style\SymfonyStyle;
12
13 use Drush\Style\DrushStyle;
14 use Drush\Utils\StringUtils;
15
16 /**
17  * Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples art sandwich`
18  *
19  * For an example of a Drupal module implementing commands, see
20  * - http://cgit.drupalcode.org/devel/tree/devel_generate/src/Commands
21  * - http://cgit.drupalcode.org/devel/tree/devel_generate/drush.services.yml
22  *
23  * This file is a good example of the first of those bullets (a commandfile) but
24  * since it isn't part of a module, it does not implement drush.services.yml.
25  */
26
27 class ArtCommands extends DrushCommands implements CustomEventAwareInterface
28 {
29     use CustomEventAwareTrait;
30
31     /** @var string[] */
32     protected $arts;
33
34     /**
35      * Show a fabulous picture.
36      *
37      * @command artwork:show
38      * @aliases arts
39      * @param $art The name of the art to display
40      * @usage drush art sandwich
41      *   Show a marvelous picture of a sandwich with pickles.
42      */
43     public function art($art = '')
44     {
45         $data = $this->getArt();
46         $name = $data[$art]['name'];
47         $description = $data[$art]['description'];
48         $path = $data[$art]['path'];
49         $msg = dt(
50             'Okay. Here is {art}: {description}',
51             ['art' => $name, 'description' => $description]
52         );
53         $this->output()->writeln("\n" . $msg . "\n");
54         $this->printFile($path);
55     }
56
57     /**
58      * Show a table of information about available art.
59      *
60      * @command artwork:list
61      * @aliases artls
62      * @field-labels
63      *   name: Name
64      *   description: Description
65      *   path: Path
66      * @default-fields name,description
67      *
68      * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
69      */
70     public function listArt($options = ['format' => 'table'])
71     {
72         $data = $this->getArt();
73         return new RowsOfFields($data);
74     }
75
76     /**
77      * Commandfiles may also add topics.  These will appear in
78      * the list of topics when `drush topic` is executed.
79      * To view the topic below, run `drush --include=/full/path/to/examples topic`
80      */
81
82     /**
83      * Ruminations on the true meaning and philosophy of artwork.
84      *
85      * @command artwork:explain
86      * @hidden
87      * @topic
88      */
89     public function ruminate()
90     {
91         self::printFile(__DIR__ . '/art-topic.md');
92     }
93
94     /**
95      * Return the available built-in art. Any Drush commandfile may provide
96      * more art by implementing a 'drush-art' on-event hook. This on-event
97      * hook is defined in the 'findArt' method beolw.
98      *
99      * @hook on-event drush-art
100      */
101     public function builtInArt()
102     {
103         return [
104             'drush' => [
105                 'name' => 'Drush',
106                 'description' => 'The Drush logo.',
107                 'path' => __DIR__ . '/art/drush-nocolor.txt',
108             ],
109             'sandwich' => [
110                 'name' => 'Sandwich',
111                 'description' => 'A tasty meal with bread often consumed at lunchtime.',
112                 'path' => __DIR__ . '/art/sandwich-nocolor.txt',
113             ],
114         ];
115     }
116
117     /**
118      * @hook interact artwork:show
119      */
120     public function interact(InputInterface $input, OutputInterface $output, AnnotationData $annotationData)
121     {
122         $io = new DrushStyle($input, $output);
123
124         // If the user did not specify any artwork, then prompt for one.
125         $art = $input->getArgument('art');
126         if (empty($art)) {
127             $data = $this->getArt();
128             $selections = $this->convertArtListToKeyValue($data);
129             $selection = $io->choice('Select art to display', $selections);
130             $input->setArgument('art', $selection);
131         }
132     }
133
134     /**
135      * @hook validate artwork:show
136      */
137     public function artValidate(CommandData $commandData)
138     {
139         $art = $commandData->input()->getArgument('art');
140         $data = $this->getArt();
141         if (!isset($data[$art])) {
142             throw new \Exception(dt('I do not have any art called "{name}".', ['name' => $art]));
143         }
144     }
145
146     /**
147      * Get a list of available artwork. Cache result for future fast access.
148      */
149     protected function getArt()
150     {
151         if (!isset($this->arts)) {
152             $this->arts = $this->findArt();
153         }
154         return $this->arts;
155     }
156
157     /**
158      * Use custom defined on-event hook 'drush-art' to find available artwork.
159      */
160     protected function findArt()
161     {
162         $arts = [];
163         $handlers = $this->getCustomEventHandlers('drush-art');
164         foreach ($handlers as $handler) {
165             $handlerResult = $handler();
166             $arts = array_merge($arts, $handlerResult);
167         }
168         return $arts;
169     }
170
171     /**
172      * Given a list of artwork, converte to a 'key' => 'Name: Description' array.
173      * @param array $data
174      * @return array
175      */
176     protected function convertArtListToKeyValue($data)
177     {
178         $result = [];
179         foreach ($data as $key => $item) {
180             $result[$key] = $item['name'] . ': ' . $item['description'];
181         }
182         return $result;
183     }
184 }