diff --git a/src/Command/Batch.php b/src/Command/Batch.php new file mode 100644 index 00000000..7214c944 --- /dev/null +++ b/src/Command/Batch.php @@ -0,0 +1,32 @@ +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'); + } + +} diff --git a/templates/_batch/batch.twig b/templates/_batch/batch.twig new file mode 100644 index 00000000..3bd0ab4d --- /dev/null +++ b/templates/_batch/batch.twig @@ -0,0 +1,69 @@ +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. + } + } + +} diff --git a/tests/functional/Generator/BatchTest.php b/tests/functional/Generator/BatchTest.php new file mode 100644 index 00000000..9a13865d --- /dev/null +++ b/tests/functional/Generator/BatchTest.php @@ -0,0 +1,58 @@ +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'); + } + +} diff --git a/tests/functional/Generator/_batch/src/ExampleBatch.php b/tests/functional/Generator/_batch/src/ExampleBatch.php new file mode 100644 index 00000000..b10dba27 --- /dev/null +++ b/tests/functional/Generator/_batch/src/ExampleBatch.php @@ -0,0 +1,69 @@ +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. + } + } + +}