From b4a80332b1a886475ff91fdfd19548b8550c6a02 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 18 Jun 2025 15:11:23 +0200 Subject: [PATCH] #162 Add timestamp placeholder on file_path parameter of CsvWriterTask. Improve documentation. --- docs/reference/tasks/csv_writer_task.md | 45 +++++++++++++++++++------ src/Task/File/Csv/CsvWriterTask.php | 1 + 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/docs/reference/tasks/csv_writer_task.md b/docs/reference/tasks/csv_writer_task.md index 96ac0f2b..0ff7e2ed 100644 --- a/docs/reference/tasks/csv_writer_task.md +++ b/docs/reference/tasks/csv_writer_task.md @@ -24,13 +24,38 @@ Possible outputs 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 two placeholders (`{date}` and `{date_time}`) to insert timestamps into the filename | -| `delimiter` | `string` | | `;` | CSV delimiter | -| `enclosure` | `string` | | `"` | CSV enclosure character | -| `escape` | `string` | | `\\` | CSV escape character | -| `headers` | `array` or `null` | | `null` | Static list of CSV headers, without the option, it will be dynamically read from first line | -| `mode` | `string` | | `wb` | File open mode (see [fopen mode parameter](https://secure.php.net/manual/en/function.fopen.php)) | -| `split_character` | `string` | | `\|` | Used to implode array values | -| `write_headers` | `bool` | | `true` | Write the headers as a first line | +| 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 | +| `delimiter` | `string` | | `;` | CSV delimiter | +| `enclosure` | `string` | | `"` | CSV enclosure character | +| `escape` | `string` | | `\\` | CSV escape character | +| `headers` | `array` or `null` | | `null` | Static list of CSV headers, without the option, it will be dynamically read from first line | +| `mode` | `string` | | `wb` | File open mode (see [fopen mode parameter](https://secure.php.net/manual/en/function.fopen.php)) | +| `split_character` | `string` | | `\|` | Used to implode array values | +| `write_headers` | `bool` | | `true` | Write the headers as a first line | + +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\Csv\CsvWriterTask' + options: + file_path: '%kernel.project_dir%/var/data/csv_writer_{date_time}.csv' +``` diff --git a/src/Task/File/Csv/CsvWriterTask.php b/src/Task/File/Csv/CsvWriterTask.php index d1145dec..e20532a6 100644 --- a/src/Task/File/Csv/CsvWriterTask.php +++ b/src/Task/File/Csv/CsvWriterTask.php @@ -59,6 +59,7 @@ protected function configureOptions(OptionsResolver $resolver): void [ '{date}' => date('Ymd'), '{date_time}' => date('Ymd_His'), + '{timestamp}' => time(), '{unique_token}' => uniqid('', true), ] )