Example #1
0
 /**
  * Demonstrate an alter hook with an option
  *
  * @hook alter example-table
  * @option french Add a row with French numbers.
  * @usage example:formatters --french
  */
 public function alterFormatters($result, CommandData $commandData)
 {
     if ($commandData->input()->getOption('french')) {
         $result['fr'] = ['first' => 'Un', 'second' => 'Deux', 'third' => 'Trois'];
     }
     return $result;
 }
 /**
  * Demonstrate an alter hook with an option
  *
  * @hook alter *
  * @option kanji Add a row with Kanji numbers.
  * @usage example:table --kanji
  */
 public function alterFormattersKanji($result, CommandData $commandData)
 {
     if ($commandData->input()->getOption('kanji')) {
         $result[] = ['first' => '一', 'second' => '二', 'third' => '三'];
     }
     return $result;
 }
Example #3
0
 /**
  * Print druplicon as post-command output.
  *
  * @hook post-command *
  * @option druplicon Shows the druplicon as glorious ASCII art.
  * @todo hidden is not yet part of annotated-command project. It is recognized by Drush's annotation_adapter.inc
  * @hidden-option druplicon
  */
 public function druplicon($result, CommandData $commandData)
 {
     // If one command does a drush_invoke to another command,
     // then this hook will be called multiple times. Only print
     // once.  (n.b. If drush_invoke_process passes along the
     // --druplicon option, then we will still get mulitple output)
     if ($this->printed) {
         return;
     }
     $this->printed = true;
     $annotationData = $commandData->annotationData();
     $commandName = $annotationData['command'];
     // For some reason, Drush help uses drush_invoke_process to call helpsingle
     if ($commandName == 'helpsingle') {
         return;
     }
     if ($commandData->input()->getOption('druplicon')) {
         $this->logger()->debug(dt('Displaying Druplicon for "!command" command.', array('!command' => $commandName)));
         $misc_dir = DRUSH_BASE_PATH . '/misc';
         if (drush_get_context('DRUSH_NOCOLOR')) {
             $content = file_get_contents($misc_dir . '/druplicon-no_color.txt');
         } else {
             $content = file_get_contents($misc_dir . '/druplicon-color.txt');
         }
         // @todo: `$commandData->output->writeln($content)` after $output hooked up to backend invoke
         drush_print($content);
     }
 }
 /**
  * Alter the results of the hook with its command name.
  *
  * @hook alter @addmycommandname
  */
 public function hookAddCommandName($result, CommandData $commandData)
 {
     $annotationData = $commandData->annotationData();
     return "{$result} from " . $annotationData['command'];
 }
 /**
  * Create a FormatterOptions object for use in writing the formatted output.
  * @param CommandData $commandData
  * @return FormatterOptions
  */
 protected function createFormatterOptions($commandData)
 {
     $options = $commandData->input()->getOptions();
     $formatterOptions = new FormatterOptions($commandData->annotationData()->getArrayCopy(), $options);
     foreach ($this->prepareOptionsList as $preparer) {
         $preparer->prepare($commandData, $formatterOptions);
     }
     return $formatterOptions;
 }
 /**
  * Process result and decide what to do with it.
  * Allow client to add transformation / interpretation
  * callbacks.
  */
 public function alterResult($names, $result, CommandData $commandData)
 {
     $processors = $this->getProcessResultHooks($names, $commandData->annotationData());
     foreach ($processors as $processor) {
         $result = $this->callProcessor($processor, $result, $commandData);
     }
     $alterers = $this->getAlterResultHooks($names, $commandData->annotationData());
     foreach ($alterers as $alterer) {
         $result = $this->callProcessor($alterer, $result, $commandData);
     }
     return $result;
 }
 protected function createCommandData(InputInterface $input, OutputInterface $output)
 {
     $commandData = new CommandData($this->annotationData, $input, $output);
     $commandData->setUseIOInterfaces($this->usesOutputInterface, $this->usesInputInterface);
     return $commandData;
 }
 public function validate(CommandData $commandData)
 {
     $args = $commandData->arguments();
     if (isset($args['one']) && $args['one'] == 'bet') {
         $commandData->input()->setArgument('one', 'replaced');
         return $args;
     }
 }
Example #9
0
 /**
  * Validate that passed View names are valid.
  *
  * @hook validate @validate-entity-load
  * @param \Consolidation\AnnotatedCommand\CommandData $commandData
  * @return \Consolidation\AnnotatedCommand\CommandError|null
  */
 public function validate_entity_load(CommandData $commandData)
 {
     list($entity_type, $arg_name) = explode(' ', $commandData->annotationData()->get('validate-entity-load', NULL));
     $names = _convert_csv_to_array($commandData->input()->getArgument($arg_name));
     $loaded = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple($names);
     if ($missing = array_diff($names, array_keys($loaded))) {
         $msg = dt('Unable to load Views: !str', ['!str' => implode(', ', $missing)]);
         return new CommandError($msg);
     }
 }