You are here

protected function WebformCliService::drush_webform_generate_commands_drush9 in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::drush_webform_generate_commands_drush9()

Generate WebformCommands class for Drush 9.x.

Return value

string WebformCommands class for Drush 9.x.

See also

\Drupal\webform\Commands\WebformCommands

1 call to WebformCliService::drush_webform_generate_commands_drush9()
WebformCliService::drush_webform_generate_commands in src/Commands/WebformCliService.php
Implements drush_hook_COMMAND().

File

src/Commands/WebformCliService.php, line 1364

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

protected function drush_webform_generate_commands_drush9() {
  $items = $this
    ->webform_drush_command();
  $methods = [];
  foreach ($items as $command_key => $command_item) {
    $command_name = str_replace('-', ':', $command_key);

    // Set defaults.
    $command_item += [
      'arguments' => [],
      'options' => [],
      'examples' => [],
      'aliases' => [],
    ];

    // Command name.
    $methods[] = "\n  /****************************************************************************/\n  // drush {$command_name}. DO NOT EDIT.\n  /****************************************************************************/";

    // Validate.
    $validate_method = 'drush_' . str_replace('-', '_', $command_key) . '_validate';
    if (method_exists($this, $validate_method)) {
      $methods[] = "\n  /**\n   * @hook validate {$command_name}\n   */\n  public function {$validate_method}(CommandData \$commandData) {\n    \$arguments = array_values(\$commandData->arguments());\n    array_shift(\$arguments);\n    call_user_func_array([\$this->cliService, '{$validate_method}'], \$arguments);\n  }";
    }

    // Command.
    $command_method = 'drush_' . str_replace('-', '_', $command_key);
    if (method_exists($this, $command_method)) {
      $command_params = [];
      $command_arguments = [];
      $command_annotations = [];

      // command.
      $command_annotations[] = "@command {$command_name}";

      // params.
      foreach ($command_item['arguments'] as $argument_name => $argument_description) {
        $command_annotations[] = "@param \${$argument_name} {$argument_description}";
        $command_params[] = "\${$argument_name} = NULL";
        $command_arguments[] = "\${$argument_name}";
      }

      // options.
      $command_options = [];
      foreach ($command_item['options'] as $option_name => $option_description) {
        $option_default = NULL;

        // Parse [datatype] from option description.
        if (preg_match('/\\[(boolean)\\]\\s+/', $option_description, $match)) {
          $option_description = preg_replace('/\\[(boolean)\\]\\s+/', '', $option_description);
          switch ($match[1]) {
            case 'boolean':
              $option_default = FALSE;
              break;
          }
        }
        $command_annotations[] = "@option {$option_name} {$option_description}";
        $command_options[$option_name] = $option_default;
      }
      if ($command_options) {
        $command_options = Variable::export($command_options);
        $command_options = preg_replace('/\\s+/', ' ', $command_options);
        $command_options = preg_replace('/array\\(\\s+/', '[', $command_options);
        $command_options = preg_replace('/, \\)/', ']', $command_options);
        $command_params[] = "array \$options = {$command_options}";
      }

      // usage.
      foreach ($command_item['examples'] as $example_name => $example_description) {
        $example_name = str_replace('-', ':', $example_name);
        $command_annotations[] = "@usage {$example_name}";
        $command_annotations[] = "  {$example_description}";
      }

      // aliases.
      $aliases = array_merge($command_item['aliases'] ?: [], [
        str_replace(':', '-', $command_name),
      ]);
      $aliases = array_unique($aliases);
      if ($aliases) {
        $command_annotations[] = "@aliases " . implode(',', $aliases);
      }
      $command_annotations = '   * ' . implode(PHP_EOL . '   * ', $command_annotations);
      $command_params = implode(', ', $command_params);
      $command_arguments = implode(', ', $command_arguments);
      $methods[] = "\n  /**\n   * {$command_item['description']}\n   *\n{$command_annotations}\n   */\n  public function {$command_method}({$command_params}) {\n    \$this->cliService->{$command_method}({$command_arguments});\n  }";
    }
  }

  // Class.
  $methods = implode(PHP_EOL, $methods) . PHP_EOL;
  return "<?php\n// @codingStandardsIgnoreFile\n\n/**\n * This is file was generated using Drush. DO NOT EDIT.\n *\n * @see drush webform-generate-commands\n * @see \\Drupal\\webform\\Commands\\DrushCliServiceBase::generate_commands_drush9\n */\nnamespace Drupal\\webform\\Commands;\n\nuse Consolidation\\AnnotatedCommand\\CommandData;\n\n/**\n * Webform commands for Drush 9.x.\n */\nclass WebformCommands extends WebformCommandsBase {\n{$methods}\n}";
}