diff --git a/docs/index.md b/docs/index.md index de141792..aad5bbda 100644 --- a/docs/index.md +++ b/docs/index.md @@ -47,6 +47,7 @@ - [InputCsvReaderTask](reference/tasks/input_csv_reader_task.md) - File/JsonStream - [JsonStreamReaderTask] + - [JsonStreamWriterTask](reference/tasks/json_stream_writer_task.md) - File/XML - [XmlReaderTask](reference/tasks/xml_reader_task.md) - [XmlWriterTask](reference/tasks/xml_writer_task.md) diff --git a/docs/reference/tasks/json_stream_writer_task.md b/docs/reference/tasks/json_stream_writer_task.md new file mode 100644 index 00000000..79fd2e39 --- /dev/null +++ b/docs/reference/tasks/json_stream_writer_task.md @@ -0,0 +1,53 @@ +JsonStreamWriterTask +=============== + +Write given array to a json file, will wait until the end of the previous iteration (this is a blocking task) and outputs +the file path. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask` +* **Blocking task** + +Accepted inputs +--------------- + +`array` + +Possible outputs +---------------- + +`string`: absolute path of the produced file + +Options +------- + +| Code | Type | Required | Default | Description | +|-------------|----------|:--------:|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `file_path` | `string` | **X** | | Path of the file to write to (relative to symfony root or absolute).
It can also take placeholders (`{date}`, `{date_time}`, `{timestamp}` `{unique_token}`) to insert data into the filename | + +Example +---------------- + +```yaml +# Task configuration level +entry: + service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask' + outputs: [ write ] + options: + output: + - column1: value1-1 + column2: value2-1 + column3: value3-1 + - column1: value1-2 + column2: value2-2 + column3: value3-2 + - column1: '' + column2: null + column3: value3-3 +write: + service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask' + options: + file_path: '%kernel.project_dir%/var/data/json_stream_writer_{date_time}.csv' +``` diff --git a/src/Task/File/JsonStream/JsonStreamWriterTask.php b/src/Task/File/JsonStream/JsonStreamWriterTask.php new file mode 100644 index 00000000..494cc0ed --- /dev/null +++ b/src/Task/File/JsonStream/JsonStreamWriterTask.php @@ -0,0 +1,64 @@ +getOptions($state); + if (!$this->file instanceof JsonStreamFile) { + $this->file = new JsonStreamFile($options['file_path'], 'wb'); + } + + $input = $state->getInput(); + if (!\is_array($input)) { + throw new \UnexpectedValueException('Input value is not an array'); + } + $this->file->writeLine($input); + } + + public function proceed(ProcessState $state): void + { + $options = $this->getOptions($state); + $state->setOutput($options['file_path']); + } + + protected function configureOptions(OptionsResolver $resolver): void + { + $resolver->setRequired(['file_path']); + $resolver->setAllowedTypes('file_path', ['string']); + $resolver->setNormalizer( + 'file_path', + static fn (Options $options, $value): string => strtr( + $value, + [ + '{date}' => date('Ymd'), + '{date_time}' => date('Ymd_His'), + '{timestamp}' => time(), + '{unique_token}' => uniqid('', true), + ] + ) + ); + } +}