DevelGenerateCommands.php 5.46 KB
Newer Older
moshe weitzman's avatar
moshe weitzman committed
1 2 3 4 5 6 7 8 9 10
<?php
namespace Drupal\devel_generate\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Drupal\devel_generate\DevelGenerateBaseInterface;
use Drush\Commands\DrushCommands;

/**
 * For commands that are parts of modules, Drush expects to find commandfiles in
 * __MODULE__/src/Commands, and the namespace is Drupal/__MODULE__/Commands.
11 12 13
 *
 * In addition to a commandfile like this one, you need to add a drush.services.yml
 * in root of your module like this module does.
moshe weitzman's avatar
moshe weitzman committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 */
class DevelGenerateCommands extends DrushCommands {

  /**
   * @var DevelGenerateBaseInterface $manager
   */
  protected $manager;

  /**
   * The plugin instance.
   *
   * @var DevelGenerateBaseInterface $instance
   */
  protected $pluginInstance;

  /**
   * The Generate plugin parameters.
   *
   * @var array $parameters
   */
  protected $parameters;

  /**
   * DevelGenerateCommands constructor.
   * @param $manager
   */
  public function __construct($manager) {
    parent::__construct();
    $this->setManager($manager);
  }

  /**
   * @return \Drupal\devel_generate\DevelGenerateBaseInterface
   */
  public function getManager() {
    return $this->manager;
  }

  /**
   * @param \Drupal\devel_generate\DevelGenerateBaseInterface $manager
   */
  public function setManager($manager) {
    $this->manager = $manager;
  }

  /**
   * @return mixed
   */
  public function getPluginInstance() {
    return $this->pluginInstance;
  }

  /**
   * @param mixed $pluginInstance
   */
  public function setPluginInstance($pluginInstance) {
    $this->pluginInstance = $pluginInstance;
  }

  /**
   * @return array
   */
  public function getParameters() {
    return $this->parameters;
  }

  /**
   * @param array $parameters
   */
  public function setParameters($parameters) {
    $this->parameters = $parameters;
  }

  /**
   * Create users.
   *
90
   * @command devel-generate-users
moshe weitzman's avatar
moshe weitzman committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
   * @pluginId user
   * @param $num Number of users to generate.
   * @option kill Delete all users before generating new ones.
   * @option roles A comma delimited list of role IDs for new users. Don't specify 'authenticated'.
   * @option pass Specify a password to be set for all generated users.
   * @aliases genu
   */
  public function users($num = 50, $options = ['kill' => FALSE, 'roles' => '']) {
    // @todo pass $options to the plugins.
    $this->generate();
  }

  /**
   * Create terms in specified vocabulary.
   *
106
   * @command devel-generate-terms
moshe weitzman's avatar
moshe weitzman committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
   * @pluginId term
   * @param $machine_name Vocabulary machine name into which new terms will be inserted.
   * @param $num Number of terms to generate.
   * @option kill Delete all terms before generating new ones.
   * @option feedback An integer representing interval for insertion rate logging.
   * @validate-entity-load taxonomy_vocabulary machine_name
   * @aliases gent
   */
  public function terms($machine_name, $num = 50, $options = ['feedback' => 1000]) {
    $this->generate();
  }

  /**
   * Create vocabularies.
   *
122
   * @command devel-generate-vocabs
moshe weitzman's avatar
moshe weitzman committed
123 124 125 126 127 128 129 130 131 132 133 134 135
   * @pluginId vocabulary
   * @param $num Number of vocabularies to generate.
   * @option kill Delete all vocabs before generating new ones.
   * @aliases genv
   * @validate-module-enabled taxonomy
   */
  public function vocabs($num = 1, $options = ['kill' => FALSE]) {
    $this->generate();
  }

  /**
   * Create menus.
   *
136
   * @command devel-generate-menus
moshe weitzman's avatar
moshe weitzman committed
137 138 139 140 141 142 143
   * @pluginId menu
   * @param $number_menus Number of menus to generate.
   * @param $number_links Number of links to generate.
   * @param $max_depth Max link depth.
   * @param $max_width Max width of first level of links.
   * @option kill Delete all content before generating new content.
   * @aliases genm
144
   * @validate-module-enabled menu_link_content
moshe weitzman's avatar
moshe weitzman committed
145 146 147 148 149 150 151 152
   */
  public function menus($number_menus = 2, $number_links = 50, $max_depth = 3, $max_width = 8, $options = ['kill' => FALSE]) {
    $this->generate();
  }

  /**
   * Create content.
   *
153
   * @command devel-generate-content
moshe weitzman's avatar
moshe weitzman committed
154 155 156 157 158 159 160 161 162 163 164 165 166
   * @pluginId content
   * @param $num Number of nodes to generate.
   * @param $max_comments Maximum number of comments to generate.
   * @option kill Delete all content before generating new content.
   * @option types A comma delimited list of content types to create. Defaults to page,article.
   * @option feedback An integer representing interval for insertion rate logging.
   * @option skip-fields A comma delimited list of fields to omit when generating random values
   * @option languages A comma-separated list of language codes
   * @aliases genc
   * @validate-module-enabled node
   */
  public function content($num = 50, $max_comments = 0, $options = ['kill' => FALSE, 'types' => 'page,article', 'feedback' => 1000]) {
    $this->generate();
167
    drush_backend_batch_process();
moshe weitzman's avatar
moshe weitzman committed
168 169 170 171 172 173 174 175 176 177 178 179
  }


  /**
   * @hook validate
   * @param \Consolidation\AnnotatedCommand\CommandData $commandData
   * @return \Consolidation\AnnotatedCommand\CommandError|null
   */
  public function validate(CommandData $commandData) {
    $manager = $this->getManager();
    $args = $commandData->input()->getArguments();
    $commandName = array_shift($args);
willzyx's avatar
willzyx committed
180
    /* @var DevelGenerateBaseInterface $instance */
moshe weitzman's avatar
moshe weitzman committed
181 182
    $instance = $manager->createInstance($commandData->annotationData()->get('pluginId'), array());
    $this->setPluginInstance($instance);
183
    $parameters = $instance->validateDrushParams($args, $commandData->input()->getOptions());
moshe weitzman's avatar
moshe weitzman committed
184 185 186 187 188 189 190
    $this->setParameters($parameters);
  }

  public function generate() {
    $instance = $this->getPluginInstance();
    $instance->generate($this->getParameters());
  }
191
}