X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrush%2Fdrush%2Fexamples%2FCommands%2FArtCommands.php;fp=vendor%2Fdrush%2Fdrush%2Fexamples%2FCommands%2FArtCommands.php;h=1db55571aa231ea0524bb9bf3729021999f47567;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/drush/drush/examples/Commands/ArtCommands.php b/vendor/drush/drush/examples/Commands/ArtCommands.php new file mode 100644 index 000000000..1db55571a --- /dev/null +++ b/vendor/drush/drush/examples/Commands/ArtCommands.php @@ -0,0 +1,184 @@ +getArt(); + $name = $data[$art]['name']; + $description = $data[$art]['description']; + $path = $data[$art]['path']; + $msg = dt( + 'Okay. Here is {art}: {description}', + ['art' => $name, 'description' => $description] + ); + $this->output()->writeln("\n" . $msg . "\n"); + $this->printFile($path); + } + + /** + * Show a table of information about available art. + * + * @command artwork:list + * @aliases artls + * @field-labels + * name: Name + * description: Description + * path: Path + * @default-fields name,description + * + * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields + */ + public function listArt($options = ['format' => 'table']) + { + $data = $this->getArt(); + return new RowsOfFields($data); + } + + /** + * Commandfiles may also add topics. These will appear in + * the list of topics when `drush topic` is executed. + * To view the topic below, run `drush --include=/full/path/to/examples topic` + */ + + /** + * Ruminations on the true meaning and philosophy of artwork. + * + * @command artwork:explain + * @hidden + * @topic + */ + public function ruminate() + { + self::printFile(__DIR__ . '/art-topic.md'); + } + + /** + * Return the available built-in art. Any Drush commandfile may provide + * more art by implementing a 'drush-art' on-event hook. This on-event + * hook is defined in the 'findArt' method beolw. + * + * @hook on-event drush-art + */ + public function builtInArt() + { + return [ + 'drush' => [ + 'name' => 'Drush', + 'description' => 'The Drush logo.', + 'path' => __DIR__ . '/art/drush-nocolor.txt', + ], + 'sandwich' => [ + 'name' => 'Sandwich', + 'description' => 'A tasty meal with bread often consumed at lunchtime.', + 'path' => __DIR__ . '/art/sandwich-nocolor.txt', + ], + ]; + } + + /** + * @hook interact artwork:show + */ + public function interact(InputInterface $input, OutputInterface $output, AnnotationData $annotationData) + { + $io = new DrushStyle($input, $output); + + // If the user did not specify any artwork, then prompt for one. + $art = $input->getArgument('art'); + if (empty($art)) { + $data = $this->getArt(); + $selections = $this->convertArtListToKeyValue($data); + $selection = $io->choice('Select art to display', $selections); + $input->setArgument('art', $selection); + } + } + + /** + * @hook validate artwork:show + */ + public function artValidate(CommandData $commandData) + { + $art = $commandData->input()->getArgument('art'); + $data = $this->getArt(); + if (!isset($data[$art])) { + throw new \Exception(dt('I do not have any art called "{name}".', ['name' => $art])); + } + } + + /** + * Get a list of available artwork. Cache result for future fast access. + */ + protected function getArt() + { + if (!isset($this->arts)) { + $this->arts = $this->findArt(); + } + return $this->arts; + } + + /** + * Use custom defined on-event hook 'drush-art' to find available artwork. + */ + protected function findArt() + { + $arts = []; + $handlers = $this->getCustomEventHandlers('drush-art'); + foreach ($handlers as $handler) { + $handlerResult = $handler(); + $arts = array_merge($arts, $handlerResult); + } + return $arts; + } + + /** + * Given a list of artwork, converte to a 'key' => 'Name: Description' array. + * @param array $data + * @return array + */ + protected function convertArtListToKeyValue($data) + { + $result = []; + foreach ($data as $key => $item) { + $result[$key] = $item['name'] . ': ' . $item['description']; + } + return $result; + } +}