data_pipelines-1.x-dev/src/Drush/Commands/DataPipelinesCommands.php

src/Drush/Commands/DataPipelinesCommands.php
<?php

declare(strict_types=1);

namespace Drupal\data_pipelines\Drush\Commands;

use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\data_pipelines\Entity\Dataset;
use Drupal\data_pipelines\Entity\DatasetInterface;
use Drupal\data_pipelines\Form\DatasetBatchOperations;
use Drush\Attributes;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Data pipelines drush commands.
 */
class DataPipelinesCommands extends DrushCommands {

  use AutowireTrait;

  const REINDEX = 'data-pipelines:reindex';

  /**
   * Creates a DataPipelinesCommand object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(
    protected readonly EntityTypeManagerInterface $entityTypeManager,
  ) {
    parent::__construct();
  }

  /**
   * Provide a list of datasets if a machine name has not been provided.
   */
  #[Attributes\Hook(type: HookManager::INTERACT, target: self::REINDEX)]
  public function datasetReindexInteract(InputInterface $input, OutputInterface $output): void {
    if (!empty($input->getArgument('machine_names'))) {
      return;
    }
    $datasets = $this->entityTypeManager->getStorage('data_pipelines')->loadMultiple();
    foreach ($datasets as $dataset) {
      assert($dataset instanceof DatasetInterface);
      $choices[$dataset->getMachineName()] = $dataset->label();
    }
    $choice = $this->io()->choice(dt('Which dataset would you like to reindex'), $choices);
    $input->setArgument('machine_name', $choice);
  }

  /**
   * Delete and reindex dataset(s).
   *
   * @param string $machine_names
   *   The dataset machine name.
   *
   * @throws \Exception
   */
  #[Attributes\Command(name: self::REINDEX)]
  #[Attributes\Argument(name: 'machine_names', description: 'A comma delimited list of datasets.')]
  public function datasetReindex(string $machine_names): void {
    $datasets = explode(',', $machine_names);
    $datasets = array_map('trim', $datasets);
    foreach ($datasets as $dataset) {
      $dataset = Dataset::loadByMachineName($dataset);
      assert($dataset instanceof DatasetInterface);
      foreach ($dataset->getDestinations() as $destination) {
        $destination->getDestinationPlugin()->deleteDataSet($dataset, $destination);
      }
      batch_set(DatasetBatchOperations::batchForDataset($dataset));
    }
    drush_backend_batch_process();
  }

  /**
   * List datasets.
   *
   * @param array $options
   *   Available options for the user.
   *
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields|null
   *   The output of the command.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  #[Attributes\Command(name: 'data-pipelines:list')]
  #[Attributes\Option(name: 'pipeline', description: 'Only show results with the provided pipeline.')]
  #[Attributes\Option(name: 'destination', description: 'Only show results with the provided destination.')]
  #[Attributes\Option(name: 'pipe', description: 'Enforce output compatible with ' . self::REINDEX . '.')]
  #[Attributes\FieldLabels(labels: [
    'id' => 'ID',
    'machine_name' => 'Machine name',
    'pipeline' => 'Pipeline',
    'destination' => 'Destination',
  ])]
  #[Attributes\DefaultTableFields(fields: ['id', 'machine_name', 'pipeline', 'destination'])]
  public function datasetList(
    array $options = [
      'format' => 'table',
      'pipeline' => NULL,
      'destination' => NULL,
      'pipe' => FALSE,
    ],
  ): ?RowsOfFields {
    $query = $this->entityTypeManager->getStorage('data_pipelines')->getQuery()
      ->accessCheck(FALSE)
      ->sort('id');
    if ($options['pipeline']) {
      $query->condition('pipeline', $options['pipeline']);
    }
    if ($options['destination']) {
      $query->condition('destinations', $options['destination']);
    }
    /** @var \Drupal\data_pipelines\Entity\Dataset[] $datasets */
    $datasets = $this->entityTypeManager->getStorage('data_pipelines')->loadMultiple($query->execute());

    if ($options['pipe']) {
      $names = array_map(fn($dataset) => $dataset->getMachineName(), $datasets);
      $this->io()->write(implode(',', $names));

      return NULL;
    }
    else {
      $rows = [];
      foreach ($datasets as $dataset) {
        $row = [
          'id' => $dataset->id(),
          'machine_name' => $dataset->getMachineName(),
          'pipeline' => $dataset->getPipeline()->getPluginId(),
          'destination' => implode(', ', array_map(fn($destination) => $destination->id(), $dataset->getDestinations())),
        ];
        $rows[] = $row;
      }
      return new RowsOfFields($rows);
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc