From 3ec6ad544b14caf08cbe46c567370d8992182164 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Wed, 8 Nov 2023 17:44:52 +0100 Subject: [PATCH] Doc: Improve recipes documentation --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 7faae10..f833962 100644 --- a/README.md +++ b/README.md @@ -209,8 +209,50 @@ $etl = (new EtlExecutor()) This will basically listen to all events and fire log entries. +### Creating your own recipes You can create your own recipes by implementing `BenTools\ETL\Recipe\Recipe` or using a callable with the same signature. +Example for displaying a progress bar when using the Symfony framework: + +```php +use BenTools\ETL\EtlExecutor; +use BenTools\ETL\EventDispatcher\Event\Event; +use BenTools\ETL\Recipe\Recipe; +use Symfony\Component\Console\Helper\ProgressBar; + +final class ProgressBarRecipe extends Recipe +{ + public function __construct( + public readonly ProgressBar $progressBar, + ) { + } + + public function decorate(EtlExecutor $executor): EtlExecutor + { + return $executor + ->onStart(function (Event $event) { + if (!$event->state->nbTotalItems) { + return; + } + $this->progressBar->setMaxSteps($event->state->nbTotalItems); + }) + ->onExtract(fn () => $this->progressBar->advance()) + ->onEnd(fn () => $this->progressBar->finish()); + } +} +``` + +Usage: + +```php +use BenTools\ETL\EtlExecutor; +use Symfony\Component\Console\Style\SymfonyStyle; + +$output = new SymfonyStyle($input, $output); +$progressBar = $output->createProgressBar(); +$executor = (new EtlExecutor())->withRecipe(new ProgressBarRecipe()); +``` + Contribute ----------