Skip to content

Commit e65d315

Browse files
Merge pull request #170 from cleverage/168
#168 Add JsonStreamWriterTask with related doc
2 parents 1c97575 + b2fc5c1 commit e65d315

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [InputCsvReaderTask](reference/tasks/input_csv_reader_task.md)
4848
- File/JsonStream
4949
- [JsonStreamReaderTask]
50+
- [JsonStreamWriterTask](reference/tasks/json_stream_writer_task.md)
5051
- File/XML
5152
- [XmlReaderTask](reference/tasks/xml_reader_task.md)
5253
- [XmlWriterTask](reference/tasks/xml_writer_task.md)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
JsonStreamWriterTask
2+
===============
3+
4+
Write given array to a json file, will wait until the end of the previous iteration (this is a blocking task) and outputs
5+
the file path.
6+
7+
Task reference
8+
--------------
9+
10+
* **Service**: `CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask`
11+
* **Blocking task**
12+
13+
Accepted inputs
14+
---------------
15+
16+
`array`
17+
18+
Possible outputs
19+
----------------
20+
21+
`string`: absolute path of the produced file
22+
23+
Options
24+
-------
25+
26+
| Code | Type | Required | Default | Description |
27+
|-------------|----------|:--------:|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
28+
| `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 |
29+
30+
Example
31+
----------------
32+
33+
```yaml
34+
# Task configuration level
35+
entry:
36+
service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask'
37+
outputs: [ write ]
38+
options:
39+
output:
40+
- column1: value1-1
41+
column2: value2-1
42+
column3: value3-1
43+
- column1: value1-2
44+
column2: value2-2
45+
column3: value3-2
46+
- column1: ''
47+
column2: null
48+
column3: value3-3
49+
write:
50+
service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask'
51+
options:
52+
file_path: '%kernel.project_dir%/var/data/json_stream_writer_{date_time}.csv'
53+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CleverAge/ProcessBundle package.
7+
*
8+
* Copyright (c) Clever-Age
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CleverAge\ProcessBundle\Task\File\JsonStream;
15+
16+
use CleverAge\ProcessBundle\Filesystem\JsonStreamFile;
17+
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
18+
use CleverAge\ProcessBundle\Model\BlockingTaskInterface;
19+
use CleverAge\ProcessBundle\Model\ProcessState;
20+
use Symfony\Component\OptionsResolver\Options;
21+
use Symfony\Component\OptionsResolver\OptionsResolver;
22+
23+
class JsonStreamWriterTask extends AbstractConfigurableTask implements BlockingTaskInterface
24+
{
25+
protected ?JsonStreamFile $file = null;
26+
27+
public function execute(ProcessState $state): void
28+
{
29+
$options = $this->getOptions($state);
30+
if (!$this->file instanceof JsonStreamFile) {
31+
$this->file = new JsonStreamFile($options['file_path'], 'wb');
32+
}
33+
34+
$input = $state->getInput();
35+
if (!\is_array($input)) {
36+
throw new \UnexpectedValueException('Input value is not an array');
37+
}
38+
$this->file->writeLine($input);
39+
}
40+
41+
public function proceed(ProcessState $state): void
42+
{
43+
$options = $this->getOptions($state);
44+
$state->setOutput($options['file_path']);
45+
}
46+
47+
protected function configureOptions(OptionsResolver $resolver): void
48+
{
49+
$resolver->setRequired(['file_path']);
50+
$resolver->setAllowedTypes('file_path', ['string']);
51+
$resolver->setNormalizer(
52+
'file_path',
53+
static fn (Options $options, $value): string => strtr(
54+
$value,
55+
[
56+
'{date}' => date('Ymd'),
57+
'{date_time}' => date('Ymd_His'),
58+
'{timestamp}' => time(),
59+
'{unique_token}' => uniqid('', true),
60+
]
61+
)
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)