From 48559e310b728e956f2e9db883b28a1d7475822f Mon Sep 17 00:00:00 2001 From: Jordan Hall Date: Wed, 28 Mar 2018 15:25:16 +0100 Subject: [PATCH] Add progress bar if $migrator->withProgressBar() is called --- composer.json | 3 +- .../Example.php | 1 + src/Examples/CSVtoCSVMigration/Example.php | 1 + src/Examples/PDOtoCSVMigration/Example.php | 1 + src/Examples/PDOtoPDOMigration/Example.php | 1 + src/Examples/XMLtoCSVMigration/Example.php | 1 + src/Objects/Migrator.php | 31 +++++++++++++++++++ 7 files changed, 38 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 12f7608..4bffc98 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "psr/cache": "^1.0", "cache/array-adapter": "^1.0", "illuminate/support": "^5.1", - "rapidwebltd/array_undot": "^1.0" + "rapidwebltd/array_undot": "^1.0", + "rapidwebltd/php-cli-progress-bar": "^1.0" } } diff --git a/src/Examples/AssocArraytoAssocArrayMigration/Example.php b/src/Examples/AssocArraytoAssocArrayMigration/Example.php index a4acf1f..8aa6022 100644 --- a/src/Examples/AssocArraytoAssocArrayMigration/Example.php +++ b/src/Examples/AssocArraytoAssocArrayMigration/Example.php @@ -23,6 +23,7 @@ $dataItem->value = round($dataItem->value, 1); } }) + ->withProgressBar() ->migrate(); var_dump($destinationArray); diff --git a/src/Examples/CSVtoCSVMigration/Example.php b/src/Examples/CSVtoCSVMigration/Example.php index e8654c0..9b74f83 100644 --- a/src/Examples/CSVtoCSVMigration/Example.php +++ b/src/Examples/CSVtoCSVMigration/Example.php @@ -14,4 +14,5 @@ ->setDestination($csvDestination) ->setFieldsToMigrate(['Author']) ->setFieldMap(['Author' => 'Writer']) + ->withProgressBar() ->migrate(); diff --git a/src/Examples/PDOtoCSVMigration/Example.php b/src/Examples/PDOtoCSVMigration/Example.php index b89f6eb..7c43d8d 100644 --- a/src/Examples/PDOtoCSVMigration/Example.php +++ b/src/Examples/PDOtoCSVMigration/Example.php @@ -16,4 +16,5 @@ ->addDestination($csvDestination) ->addDestination($emailsCSVDestination, ['email']) ->setFieldsToMigrate(['email', 'name']) + ->withProgressBar() ->migrate(); diff --git a/src/Examples/PDOtoPDOMigration/Example.php b/src/Examples/PDOtoPDOMigration/Example.php index 4fa2d00..4bac4df 100644 --- a/src/Examples/PDOtoPDOMigration/Example.php +++ b/src/Examples/PDOtoPDOMigration/Example.php @@ -44,4 +44,5 @@ } } }) + ->withProgressBar() ->migrate(); diff --git a/src/Examples/XMLtoCSVMigration/Example.php b/src/Examples/XMLtoCSVMigration/Example.php index c166c0f..7ee1972 100644 --- a/src/Examples/XMLtoCSVMigration/Example.php +++ b/src/Examples/XMLtoCSVMigration/Example.php @@ -15,4 +15,5 @@ $migrator->setSource($xmlSource) ->setDestination($csvDestination) ->setFieldsToMigrate(['loc']) + ->withProgressBar() ->migrate(); diff --git a/src/Objects/Migrator.php b/src/Objects/Migrator.php index 128baae..c144c19 100644 --- a/src/Objects/Migrator.php +++ b/src/Objects/Migrator.php @@ -8,6 +8,7 @@ use RapidWeb\uxdm\Objects\Exceptions\MissingFieldToMigrateException; use RapidWeb\uxdm\Objects\Exceptions\NoDestinationException; use RapidWeb\uxdm\Objects\Exceptions\NoSourceException; +use RapidWeb\CliProgressBar\ProgressBar; class Migrator { @@ -22,6 +23,8 @@ class Migrator private $sourceCachePool; private $sourceCacheKey; private $sourceCacheExpiresAfter; + private $showProgressBar = false; + private $progressBar; public function setSource(SourceInterface $source) { @@ -96,6 +99,12 @@ public function setSourceCache(CacheItemPoolInterface $sourceCachePool, $sourceC return $this; } + public function withProgressBar() + { + $this->showProgressBar = true; + return $this; + } + private function getSourceDataRows($page) { if (!$this->sourceCachePool || !$this->sourceCacheKey) { @@ -147,7 +156,14 @@ public function migrate() $results = []; + if ($this->showProgressBar) { + $this->progressBar = new ProgressBar(); + $this->progressBar->setMaxProgress($this->source->countDataRows() * count($this->destinationContainers)); + $this->progressBar->display(); + } + for ($page = 1; $page < PHP_INT_MAX; $page++) { + $dataRows = $this->getSourceDataRows($page); if (!$dataRows) { @@ -177,6 +193,7 @@ public function migrate() foreach ($this->destinationContainers as $destinationContainer) { if (!$destinationContainer->fields) { $results[] = $destinationContainer->destination->putDataRows($dataRows); + $this->advanceProgressBar(count($dataRows)); continue; } @@ -193,9 +210,23 @@ public function migrate() } $results[] = $destinationContainer->destination->putDataRows($destinationDataRows); + $this->advanceProgressBar(count($dataRows)); } } + if ($this->showProgressBar) { + $this->progressBar->complete(); + } + return $results; } + + private function advanceProgressBar($amount) + { + if ($this->showProgressBar) { + for ($i=0; $i < $amount; $i++) { + $this->progressBar->advance()->display(); + } + } + } }