Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions docs/reference/tasks/json_stream_writer_task.md
Original file line number Diff line number Diff line change
@@ -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).<br/>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'
```
64 changes: 64 additions & 0 deletions src/Task/File/JsonStream/JsonStreamWriterTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Task\File\JsonStream;

use CleverAge\ProcessBundle\Filesystem\JsonStreamFile;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\BlockingTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class JsonStreamWriterTask extends AbstractConfigurableTask implements BlockingTaskInterface
{
protected ?JsonStreamFile $file = null;

public function execute(ProcessState $state): void
{
$options = $this->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),
]
)
);
}
}