2 namespace Drush\Commands;
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;
13 use Drush\Style\DrushStyle;
14 use Drush\Utils\StringUtils;
17 * Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples art sandwich`
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
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.
27 class ArtCommands extends DrushCommands implements CustomEventAwareInterface
29 use CustomEventAwareTrait;
35 * Show a fabulous picture.
37 * @command artwork:show
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.
43 public function art($art = '')
45 $data = $this->getArt();
46 $name = $data[$art]['name'];
47 $description = $data[$art]['description'];
48 $path = $data[$art]['path'];
50 'Okay. Here is {art}: {description}',
51 ['art' => $name, 'description' => $description]
53 $this->output()->writeln("\n" . $msg . "\n");
54 $this->printFile($path);
58 * Show a table of information about available art.
60 * @command artwork:list
64 * description: Description
66 * @default-fields name,description
68 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
70 public function listArt($options = ['format' => 'table'])
72 $data = $this->getArt();
73 return new RowsOfFields($data);
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`
83 * Ruminations on the true meaning and philosophy of artwork.
85 * @command artwork:explain
89 public function ruminate()
91 self::printFile(__DIR__ . '/art-topic.md');
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.
99 * @hook on-event drush-art
101 public function builtInArt()
106 'description' => 'The Drush logo.',
107 'path' => __DIR__ . '/art/drush-nocolor.txt',
110 'name' => 'Sandwich',
111 'description' => 'A tasty meal with bread often consumed at lunchtime.',
112 'path' => __DIR__ . '/art/sandwich-nocolor.txt',
118 * @hook interact artwork:show
120 public function interact(InputInterface $input, OutputInterface $output, AnnotationData $annotationData)
122 $io = new DrushStyle($input, $output);
124 // If the user did not specify any artwork, then prompt for one.
125 $art = $input->getArgument('art');
127 $data = $this->getArt();
128 $selections = $this->convertArtListToKeyValue($data);
129 $selection = $io->choice('Select art to display', $selections);
130 $input->setArgument('art', $selection);
135 * @hook validate artwork:show
137 public function artValidate(CommandData $commandData)
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]));
147 * Get a list of available artwork. Cache result for future fast access.
149 protected function getArt()
151 if (!isset($this->arts)) {
152 $this->arts = $this->findArt();
158 * Use custom defined on-event hook 'drush-art' to find available artwork.
160 protected function findArt()
163 $handlers = $this->getCustomEventHandlers('drush-art');
164 foreach ($handlers as $handler) {
165 $handlerResult = $handler();
166 $arts = array_merge($arts, $handlerResult);
172 * Given a list of artwork, converte to a 'key' => 'Name: Description' array.
176 protected function convertArtListToKeyValue($data)
179 foreach ($data as $key => $item) {
180 $result[$key] = $item['name'] . ': ' . $item['description'];