Skip to content

Added the command to generate the batch class. #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Command/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DrupalCodeGenerator\Command;

use DrupalCodeGenerator\Application;
use DrupalCodeGenerator\Asset\AssetCollection;
use DrupalCodeGenerator\Attribute\Generator;
use DrupalCodeGenerator\GeneratorType;

#[Generator(
name: 'batch',
description: 'Generates a batch',
templatePath: Application::TEMPLATE_PATH . '/_batch',
type: GeneratorType::MODULE_COMPONENT
)]
final class Batch extends BaseGenerator {

/**
* {@inheritDoc}
*/
protected function generate(array &$vars, AssetCollection $assets): void {
$ir = $this->createInterviewer($vars);
$vars['machine_name'] = $ir->askMachineName();
$vars['class'] = $ir->askClass(default: '{machine_name|camelize}Batch');
$vars['operation_callback'] = $ir->ask('Operation callback method name', 'processItem');
$vars['finished_callback'] = $ir->ask('Finished callback method name', 'finished');
$assets->addFile('src/{class}.php', 'batch.twig');
}

}
69 changes: 69 additions & 0 deletions templates/_batch/batch.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Drupal\{{ machine_name }};

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
* Service for batch processing.
*/
final class {{ class }} {
use StringTranslationTrait;

/**
* Builds a batch.
*
* @param array $items
* The items to process.
*
* @return array
* The batch definition.
*/
public function buildBatch(array $items) {
$batch_builder = new BatchBuilder();
$batch_builder
->setTitle($this->t('Processing items...'))
->setFinishCallback([$this, '{{ finished_callback }}']);

foreach ($items as $item) {
$batch_builder->addOperation([$this, '{{ operation_callback }}'], [$item]);
}

return $batch_builder->toArray();
}

/**
* Processes a single batch item.
*
* @param mixed $item
* The item to process.
* @param array $context
* The batch context.
*/
public function {{ operation_callback }}($item, array &$context) {
// @todo Implement batch processing logic.
}

/**
* Batch finish callback.
*
* @param bool $success
* Indicates whether the batch process was successful.
* @param array $results
* The results from the batch process.
* @param array $operations
* The operations that were processed.
*/
public function {{ finished_callback }}(bool $success, array $results, array $operations) {
if ($success) {
// @todo Implement batch processing logic.
}
else {
// @todo Implement batch processing logic.
}
}

}
58 changes: 58 additions & 0 deletions tests/functional/Generator/BatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace DrupalCodeGenerator\Tests\Functional\Generator;

use DrupalCodeGenerator\Command\Batch;
use DrupalCodeGenerator\Test\Functional\GeneratorTestBase;

/**
* Tests batch generator.
*/
final class BatchTest extends GeneratorTestBase {

protected string $fixtureDir = __DIR__ . '/_batch';

/**
* Test callback.
*/
public function testGenerator(): void {
$input = [
'example',
'ExampleBatch',
'processItem',
'finished',
];
$this->execute(Batch::class, $input);

$expected_display = <<< 'TXT'

Welcome to batch generator!
–––––––––––––––––––––––––––––

Module machine name:

Class [ExampleBatch]:

Operation callback method name [processItem]:

Finished callback method name [finished]:

The following directories and files have been created or updated:
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
• example.info.yml
• src/ExampleBatch.php

TXT;

$this->assertDisplay($expected_display);

$this->assertGeneratedFile('src/ExampleBatch.php');
}

}
69 changes: 69 additions & 0 deletions tests/functional/Generator/_batch/src/ExampleBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Drupal\example;

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
* Service for batch processing.
*/
final class ExampleBatch {
use StringTranslationTrait;

/**
* Builds a batch.
*
* @param array $items
* The items to process.
*
* @return array
* The batch definition.
*/
public function buildBatch(array $items) {
$batch_builder = new BatchBuilder();
$batch_builder
->setTitle($this->t('Processing items...'))
->setFinishCallback([$this, 'finished']);

foreach ($items as $item) {
$batch_builder->addOperation([$this, 'processItem'], [$item]);
}

return $batch_builder->toArray();
}

/**
* Processes a single batch item.
*
* @param mixed $item
* The item to process.
* @param array $context
* The batch context.
*/
public function processItem($item, array &$context) {
// @todo Implement batch processing logic.
}

/**
* Batch finish callback.
*
* @param bool $success
* Indicates whether the batch process was successful.
* @param array $results
* The results from the batch process.
* @param array $operations
* The operations that were processed.
*/
public function finished(bool $success, array $results, array $operations) {
if ($success) {
// @todo Implement batch processing logic.
}
else {
// @todo Implement batch processing logic.
}
}

}
Loading