From 44aed0f47bd16f7ff644900701f0975b6ebda1f8 Mon Sep 17 00:00:00 2001 From: hamzahasbi Date: Mon, 18 Apr 2022 12:12:00 +0000 Subject: [PATCH] feat: add global header to batched csv --- src/Commands/MigrateBatchCommands.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Commands/MigrateBatchCommands.php b/src/Commands/MigrateBatchCommands.php index 7e3822e..1405c68 100644 --- a/src/Commands/MigrateBatchCommands.php +++ b/src/Commands/MigrateBatchCommands.php @@ -37,6 +37,14 @@ class MigrateBatchCommands extends MigrateToolsCommands implements SiteAliasMana */ protected $fileSystem; + /** + * The original csv header. + * + * @var array + */ + */ + protected $header; + /** * Constructor that sets up dependency injection. * @@ -45,6 +53,7 @@ class MigrateBatchCommands extends MigrateToolsCommands implements SiteAliasMana public function __construct(MigrationPluginManager $migrationPluginManager, DateFormatter $dateFormatter, EntityTypeManagerInterface $entityTypeManager, KeyValueFactoryInterface $keyValue, FileSystemInterface $fileSystem) { parent::__construct($migrationPluginManager, $dateFormatter, $entityTypeManager, $keyValue); $this->fileSystem = $fileSystem; + $this->header = []; } /** @@ -304,6 +313,8 @@ private function batchSourceFile($source_file, $batch_size) { // Close the existing file and create a new one. fclose($file_handle); $file_handle = $this->createBatchedFile($files, $temp_file_name, $batch_id); + // Add the header to the batched file so we can have a valid CSV to parse. + fwrite($file_handle, $this->header); } // Write a line to the file. @@ -365,7 +376,13 @@ private function getFileIterator($file_path) { // Return a line at a time. while (!feof($file)) { - yield fgets($file); + $line = fgets($file); + + // We need to fill our global header with the first line of the file. + if (empty($this->header)) { + $this->header = $line; + } + yield $line; } // Close the file.