Skip to content
Snippets Groups Projects

Modernising the drush commands

Merged nterbogt requested to merge issue/data_pipelines-3513916:3513916-drush-improvements into 2.x
Files
2
<?php
namespace Drupal\data_pipelines\Commands;
declare(strict_types=1);
use Drupal\Core\Database\Connection;
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;
@@ -16,45 +21,31 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class DataPipelinesCommands extends DrushCommands {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
use AutowireTrait;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
const REINDEX = 'data-pipelines:reindex';
/**
* Creates a DataPipelinesCommand object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Database\Connection $database
* The database connection.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database) {
public function __construct(
protected readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct();
$this->entityTypeManager = $entity_type_manager;
$this->database = $database;
}
/**
* Provide a list of datasets if a machine name has not been provided.
*
* @hook interact data-pipelines:reindex
*/
#[Attributes\Hook(type: HookManager::INTERACT, target: self::REINDEX)]
public function datasetReindexInteract(InputInterface $input, OutputInterface $output): void {
if (!empty($input->getArgument('machine_name'))) {
if (!empty($input->getArgument('machine_names'))) {
return;
}
$datasets = $this->entityTypeManager->getStorage('data_pipelines')->loadMultiple();
$choices = ['all' => 'All'];
foreach ($datasets as $dataset) {
assert($dataset instanceof DatasetInterface);
$choices[$dataset->getMachineName()] = $dataset->label();
@@ -66,27 +57,16 @@ class DataPipelinesCommands extends DrushCommands {
/**
* Delete and reindex dataset(s).
*
* @param string $machine_name
* @param string $machine_names
* The dataset machine name.
*
* @command data-pipelines:reindex
*
* @validate-module-enabled data_pipelines
*
* @throws \Exception
*/
public function datasetReindex(string $machine_name): void {
$datasets = [];
if ('all' === $machine_name) {
$results = $this->database->select('data_pipelines')
->fields('data_pipelines', ['machine_name'])
->execute()
->fetchAll();
$datasets = array_map(fn($result) => $result->machine_name, $results);
}
else {
$datasets[] = $machine_name;
}
#[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);
@@ -98,4 +78,68 @@ class DataPipelinesCommands extends DrushCommands {
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);
}
}
}
Loading