Skip to content

Commit 6f61a97

Browse files
Merge pull request #219 from cleverage/208
#208 - Fix FolderBrowserTask, InputFolderBrowserTask, CsvReaderTask and LineReaderTask (and their `Input*` variants)
2 parents 9ac6829 + fdb6a5c commit 6f61a97

14 files changed

Lines changed: 422 additions & 6 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Latest
1818
* [#205](https://github.com/cleverage/process-bundle/issues/205) Fix FileSplitterTask: produced files lost the first line, doubled line breaks and ended with an empty file; each file now contains exactly `max_lines` lines of the source. Update documentation, add tests.
1919
* [#206](https://github.com/cleverage/process-bundle/issues/206) Fix AdvancedStatCounterTask, IterableBatchTask, ConditionTrait and ColumnAggregatorTask edge cases: log the first counted execution with correct counts, accept a `null` `batch_count` (only flush at the end), allow scalar inputs in conditions (`''` path on the whole value), aggregate `null` column values. Update documentation, add tests.
2020
* [#207](https://github.com/cleverage/process-bundle/issues/207) Fix JsonStreamReaderTask / JsonStreamWriterTask: throw an explicit `\UnexpectedValueException` when a line decodes to a scalar, create the missing parent directory when writing. Update documentation, add tests.
21+
* [#208](https://github.com/cleverage/process-bundle/issues/208) Fix FolderBrowserTask, InputFolderBrowserTask, CsvReaderTask and LineReaderTask (and their `Input*` variants): reset the state at the end of the iteration, so that a following input is read from its beginning. Update documentation, add tests.
2122

2223
## Deprecated
2324
* [#189](https://github.com/cleverage/process-bundle/issues/189) EventDispatcherTask: when `event_name` is set, listening to `CleverAge\ProcessBundle\Event\EventDispatcherTaskEvent` is deprecated (the event is still dispatched under its class name, with an `E_USER_DEPRECATED` error, if it has listeners). Listen to the configured `event_name` instead: the BC layer will be removed in v6.0.

‎docs/reference/tasks/csv_reader_task.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ Notes
7272
* Each line must contain exactly as many columns as there are headers, otherwise an `\UnexpectedValueException` is thrown.
7373
* A UTF-8 BOM is removed from the first header when headers are read from the file.
7474
* `csv_file` and `csv_line` are added to the error context of the process.
75+
* The file is closed once fully read: if the task is executed again (e.g. for a new input), the file is read again
76+
from its beginning.
7577
* See also [InputCsvReaderTask](input_csv_reader_task.md) to read a file path given as input.

‎docs/reference/tasks/folder_browser_task.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ Notes
5353
-----
5454
5555
* `current_file_path` is added to the error context of the process.
56+
* The iteration is reset once all files have been output: if the task is executed again (e.g. for a new input), the
57+
folder is browsed again from the start.
5658
* See also [InputFolderBrowserTask](input_folder_browser_task.md) to browse a folder path given as input.

‎docs/reference/tasks/input_csv_reader_task.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Accepted inputs
1414
---------------
1515

1616
`string`: path of the file to read, prefixed by the `base_path` option if set.
17-
When a different path is received, the previous file is dropped and the new one is opened.
17+
The file is closed once fully read, so each input (the same path again or a different one) is read from its
18+
beginning.
1819

1920
Possible outputs
2021
----------------

‎docs/reference/tasks/input_folder_browser_task.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Accepted inputs
1717
`string`: path of the folder to browse, prefixed by the `base_folder_path` option. It must be an existing readable
1818
directory.
1919

20-
Receiving a different folder path before the task has been flushed throws a `\LogicException`.
20+
The folder path is released once its files have all been iterated, so each input (the same path again or a different
21+
one) is browsed from the start. Receiving a different folder path while an iteration is in progress throws a
22+
`\LogicException`.
2123

2224
Possible outputs
2325
----------------

‎docs/reference/tasks/input_line_reader_task.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Accepted inputs
1414
---------------
1515

1616
`string`: path of the file to read. An `\UnexpectedValueException` is thrown if the file does not exist or is not
17-
readable. When a different path is received, the previous file is dropped and the new one is opened.
17+
readable. The file is released once fully read, so each input (the same path again or a different one) is read from
18+
its beginning.
1819

1920
Possible outputs
2021
----------------

‎docs/reference/tasks/line_reader_task.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ trim:
5151
Notes
5252
-----
5353
54+
* The file is released once fully read: if the task is executed again (e.g. for a new input), the file is read again
55+
from its beginning.
5456
* See also [InputLineReaderTask](input_line_reader_task.md) to read a file path given as input.

‎src/Task/File/Csv/CsvReaderTask.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ public function next(ProcessState $state): bool
7474
$state->removeErrorContext('csv_file');
7575
$state->removeErrorContext('csv_line');
7676

77-
return !$this->csv->isEndOfFile();
77+
$endOfFile = $this->csv->isEndOfFile();
78+
if ($endOfFile) {
79+
// Release the file to allow the following iteration to read it again
80+
$this->csv->close();
81+
$this->csv = null;
82+
}
83+
84+
return !$endOfFile;
7885
}
7986

8087
protected function getHeaders(ProcessState $state, array $options): ?array

‎src/Task/File/FolderBrowserTask.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ public function next(ProcessState $state): bool
8080
$this->files->next();
8181
$state->removeErrorContext('current_file_path');
8282

83-
return $this->files->valid();
83+
if (!$this->files->valid()) {
84+
// Reset the iterator to allow the following iteration
85+
$this->files = null;
86+
87+
return false;
88+
}
89+
90+
return true;
8491
}
8592

8693
protected function configureOptions(OptionsResolver $resolver): void

‎src/Task/File/InputFolderBrowserTask.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public function flush(ProcessState $state): void
3131
$state->setSkipped(true);
3232
}
3333

34+
#[\Override]
35+
public function next(ProcessState $state): bool
36+
{
37+
$hasNext = parent::next($state);
38+
if (!$hasNext) {
39+
// Release the folder path to allow the following input to browse another folder
40+
$this->folderPath = null;
41+
}
42+
43+
return $hasNext;
44+
}
45+
3446
#[\Override]
3547
public function initialize(ProcessState $state): void
3648
{

0 commit comments

Comments
 (0)