DevelGenerateCommands.php

Same filename in this branch
  1. 5.x devel_generate/src/Commands/DevelGenerateCommands.php
Same filename in other branches
  1. 8.x-1.x devel_generate/src/Commands/DevelGenerateCommands.php
  2. 4.x devel_generate/src/Commands/DevelGenerateCommands.php

Namespace

Drupal\devel_generate\Drush\Commands

File

devel_generate/src/Drush/Commands/DevelGenerateCommands.php

View source
<?php

namespace Drupal\devel_generate\Drush\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\devel_generate\Attributes\Generator;
use Drupal\devel_generate\DevelGenerateBaseInterface;
use Drupal\devel_generate\DevelGeneratePluginManager;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;

/**
 * Provide Drush commands for all the core Devel Generate plugins.
 *
 * For commands that are parts of modules, Drush expects to find commandfiles in
 * __MODULE__/src/Drush/Commands, and the namespace is Drupal/__MODULE__/Drush/Commands.
 */
final class DevelGenerateCommands extends DrushCommands {
    use AutowireTrait;
    const USERS = 'devel-generate:users';
    const TERMS = 'devel-generate:terms';
    const VOCABS = 'devel-generate:vocabs';
    const MENUS = 'devel-generate:menus';
    const CONTENT = 'devel-generate:content';
    const BLOCK_CONTENT = 'devel-generate:block-content';
    const MEDIA = 'devel-generate:media';
    
    /**
     * The plugin instance.
     */
    private DevelGenerateBaseInterface $pluginInstance;
    
    /**
     * The Generate plugin parameters.
     */
    private array $parameters;
    
    /**
     * DevelGenerateCommands constructor.
     *
     * @param \Drupal\devel_generate\DevelGeneratePluginManager $manager
     *   The DevelGenerate plugin manager.
     */
    public function __construct(DevelGeneratePluginManager $manager) {
        parent::__construct();
        $this->setManager($manager);
    }
    
    /**
     * Get the DevelGenerate plugin manager.
     *
     * @return \Drupal\devel_generate\DevelGeneratePluginManager
     *   The DevelGenerate plugin manager.
     */
    public function getManager() : DevelGeneratePluginManager {
        return $this->manager;
    }
    
    /**
     * Set the DevelGenerate plugin manager.
     *
     * @param \Drupal\devel_generate\DevelGeneratePluginManager $manager
     *   The DevelGenerate plugin manager.
     */
    public function setManager(DevelGeneratePluginManager $manager) : void {
        $this->manager = $manager;
    }
    
    /**
     * Get the DevelGenerate plugin instance.
     *
     * @return \Drupal\devel_generate\DevelGenerateBaseInterface
     *   The DevelGenerate plugin instance.
     */
    public function getPluginInstance() : DevelGenerateBaseInterface {
        return $this->pluginInstance;
    }
    
    /**
     * Set the DevelGenerate plugin instance.
     *
     * @param mixed $pluginInstance
     *   The DevelGenerate plugin instance.
     */
    public function setPluginInstance(mixed $pluginInstance) : void {
        $this->pluginInstance = $pluginInstance;
    }
    
    /**
     * Get the DevelGenerate plugin parameters.
     *
     * @return array
     *   The plugin parameters.
     */
    public function getParameters() : array {
        return $this->parameters;
    }
    
    /**
     * Set the DevelGenerate plugin parameters.
     *
     * @param array $parameters
     *   The plugin parameters.
     */
    public function setParameters(array $parameters) : void {
        $this->parameters = $parameters;
    }
    
    /**
     * Create users.
     */
    public function users(int $num = 50, array $options = [
        'kill' => FALSE,
        'roles' => self::REQ,
    ]) : void {
        // @todo pass $options to the plugins.
        $this->generate();
    }
    
    /**
     * Create terms in specified vocabulary.
     */
    public function terms(int $num = 50, array $options = [
        'kill' => FALSE,
        'bundles' => self::REQ,
        'feedback' => '1000',
        'languages' => self::REQ,
        'translations' => self::REQ,
        'min-depth' => '1',
        'max-depth' => '4',
    ]) : void {
        $this->generate();
    }
    
    /**
     * Create vocabularies.
     */
    public function vocabs(int $num = 1, array $options = [
        'kill' => FALSE,
    ]) : void {
        $this->generate();
    }
    
    /**
     * Create menus.
     */
    public function menus(int $number_menus = 2, int $number_links = 50, int $max_depth = 3, int $max_width = 8, array $options = [
        'kill' => FALSE,
    ]) : void {
        $this->generate();
    }
    
    /**
     * Create content.
     */
    public function content(int $num = 50, int $max_comments = 0, array $options = [
        'kill' => FALSE,
        'bundles' => 'page,article',
        'authors' => self::REQ,
        'roles' => self::REQ,
        'feedback' => 1000,
        'skip-fields' => self::REQ,
        'base-fields' => self::REQ,
        'languages' => self::REQ,
        'translations' => self::REQ,
        'add-type-label' => FALSE,
    ]) : void {
        $this->generate();
    }
    
    /**
     * Create Block content blocks.
     */
    public function blockContent(int $num = 50, array $options = [
        'kill' => FALSE,
        'block_types' => 'basic',
        'feedback' => 1000,
        'skip-fields' => self::REQ,
        'base-fields' => self::REQ,
        'languages' => self::REQ,
        'translations' => self::REQ,
        'add-type-label' => FALSE,
        'reusable' => TRUE,
    ]) : void {
        $this->generate();
    }
    
    /**
     * Create media items.
     */
    public function media(int $num = 50, array $options = [
        'kill' => FALSE,
        'media-types' => self::REQ,
        'feedback' => 1000,
        'skip-fields' => self::REQ,
        'languages' => self::REQ,
        'base-fields' => self::REQ,
    ]) : void {
        $this->generate();
    }
    
    /**
     * The standard drush validate hook.
     *
     * @param \Consolidation\AnnotatedCommand\CommandData $commandData
     *   The data sent from the drush command.
     */
    public function validate(CommandData $commandData) : void {
        $manager = $this->manager;
        $args = $commandData->input()
            ->getArguments();
        // The command name is the first argument but we do not need this.
        array_shift($args);
        
        /** @var \Drupal\devel_generate\DevelGenerateBaseInterface $instance */
        $instance = $manager->createInstance($commandData->annotationData()
            ->get('pluginId'), []);
        $this->setPluginInstance($instance);
        $parameters = $instance->validateDrushParams($args, $commandData->input()
            ->getOptions());
        $this->setParameters($parameters);
    }
    
    /**
     * Wrapper for calling the plugin instance generate function.
     */
    public function generate() : void {
        $instance = $this->pluginInstance;
        $instance->generate($this->parameters);
    }

}

Classes

Title Deprecated Summary
DevelGenerateCommands Provide Drush commands for all the core Devel Generate plugins.