Skip to content

Commit 3d3af7d

Browse files
Merge pull request #173 from cleverage/172
#172 Add spl_file_object_flags and json_flags options on JsonStream*T…
2 parents 6b3a6f6 + c0ec819 commit 3d3af7d

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

docs/reference/tasks/json_stream_reader_task.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ Underlying method are [SplFileObject::fgets](https://www.php.net/manual/fr/splfi
2323
Options
2424
-------
2525

26-
none
26+
| Code | Type | Required | Default | Description |
27+
|-------------------------|-----------------|:--------:|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
28+
| `spl_file_object_flags` | `array`, `null` | | `\SplFileObject::DROP_NEW_LINE \SplFileObject::READ_AHEAD \SplFileObject::SKIP_EMPTY` | Flags to pass to `SplFileObject` constructor, can be empty.<br/>See [PHP documentation](https://www.php.net/manual/en/splfileobject.construct.php) for more information on available flags. |
29+
| `json_flags` | `array`, `null` | | `\JSON_THROW_ON_ERROR` | Flags to pass to `json_encode` function, can be empty.<br/>See [PHP documentation](https://www.php.net/manual/en/function.json-encode.php) for more information on available flags. |
30+
2731

2832
Example
2933
-------
@@ -38,6 +42,10 @@ entry:
3842
file_path: '%kernel.project_dir%/var/data/json_stream_reader.json'
3943
read:
4044
service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamReaderTask'
45+
options:
46+
spl_file_object_flags: []
47+
json_flags:
48+
- !php/const JSON_ERROR_NONE
4149
```
4250
4351

docs/reference/tasks/json_stream_writer_task.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ Possible outputs
2323
Options
2424
-------
2525

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 |
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+
| `spl_file_object_flags` | `array`, `null` | | `\SplFileObject::DROP_NEW_LINE \SplFileObject::READ_AHEAD \SplFileObject::SKIP_EMPTY` | Flags to pass to `SplFileObject` constructor, can be empty.<br/>See [PHP documentation](https://www.php.net/manual/en/splfileobject.construct.php) for more information on available flags. |
30+
| `json_flags` | `array`, `null` | | `\JSON_THROW_ON_ERROR` | Flags to pass to `json_encode` function, can be empty.<br/>See [PHP documentation](https://www.php.net/manual/en/function.json-encode.php) for more information on available flags. |
2931

3032
Example
3133
----------------
@@ -50,4 +52,8 @@ write:
5052
service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask'
5153
options:
5254
file_path: '%kernel.project_dir%/var/data/json_stream_writer_{date_time}.csv'
55+
spl_file_object_flags: []
56+
json_flags:
57+
- !php/const JSON_PRETTY_PRINT
58+
- !php/const JSON_UNESCAPED_SLASHES
5359
```

src/Filesystem/JsonStreamFile.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,30 @@ class JsonStreamFile implements FileStreamInterface, WritableFileInterface
2020
{
2121
protected \SplFileObject $file;
2222

23+
private readonly int $jsonFlags;
24+
2325
protected ?int $lineCount = null;
2426

2527
protected int $lineNumber = 1;
2628

27-
public function __construct(string $filename, string $mode = 'rb')
28-
{
29+
public function __construct(
30+
string $filename,
31+
string $mode = 'rb',
32+
?array $splFileObjectFlags = null,
33+
?array $jsonFlags = null,
34+
) {
2935
$this->file = new \SplFileObject($filename, $mode);
3036

3137
// Useful to skip empty trailing lines (doesn't work well on PHP 8, see readLine() code)
32-
$this->file->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
38+
$this->file->setFlags(null !== $splFileObjectFlags
39+
? array_sum($splFileObjectFlags)
40+
: \SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY
41+
);
42+
43+
$this->jsonFlags = null !== $jsonFlags
44+
? array_sum($jsonFlags)
45+
: \JSON_THROW_ON_ERROR
46+
;
3347
}
3448

3549
/**
@@ -78,12 +92,12 @@ public function readLine(?int $length = null): ?array
7892
}
7993
++$this->lineNumber;
8094

81-
return json_decode($rawLine, true, 512, \JSON_THROW_ON_ERROR);
95+
return json_decode($rawLine, true, 512, $this->jsonFlags);
8296
}
8397

8498
public function writeLine(array $fields): int
8599
{
86-
$this->file->fwrite(json_encode($fields, \JSON_THROW_ON_ERROR).\PHP_EOL);
100+
$this->file->fwrite(json_encode($fields, $this->jsonFlags).\PHP_EOL);
87101
++$this->lineNumber;
88102

89103
return $this->lineNumber;

src/Task/File/JsonStream/JsonStreamReaderTask.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
namespace CleverAge\ProcessBundle\Task\File\JsonStream;
1515

1616
use CleverAge\ProcessBundle\Filesystem\JsonStreamFile;
17+
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
1718
use CleverAge\ProcessBundle\Model\IterableTaskInterface;
1819
use CleverAge\ProcessBundle\Model\ProcessState;
20+
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

20-
class JsonStreamReaderTask implements IterableTaskInterface
22+
class JsonStreamReaderTask extends AbstractConfigurableTask implements IterableTaskInterface
2123
{
2224
protected ?JsonStreamFile $file = null;
2325

2426
public function execute(ProcessState $state): void
2527
{
2628
if (!$this->file instanceof JsonStreamFile) {
27-
$this->file = new JsonStreamFile($this->getFilePath($state), 'rb');
29+
$options = $this->getOptions($state);
30+
$this->file = new JsonStreamFile(
31+
$this->getFilePath($state),
32+
'rb',
33+
$options['spl_file_object_flags'],
34+
$options['json_flags'],
35+
);
2836
}
2937

3038
$line = $this->file->readLine();
@@ -49,4 +57,14 @@ protected function getFilePath(ProcessState $state): string
4957
{
5058
return $state->getInput();
5159
}
60+
61+
protected function configureOptions(OptionsResolver $resolver): void
62+
{
63+
$resolver->setDefaults([
64+
'spl_file_object_flags' => null,
65+
'json_flags' => null,
66+
]);
67+
$resolver->setAllowedTypes('spl_file_object_flags', ['array', 'null']);
68+
$resolver->setAllowedTypes('json_flags', ['array', 'null']);
69+
}
5270
}

src/Task/File/JsonStream/JsonStreamWriterTask.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ class JsonStreamWriterTask extends AbstractConfigurableTask implements BlockingT
2626

2727
public function execute(ProcessState $state): void
2828
{
29-
$options = $this->getOptions($state);
3029
if (!$this->file instanceof JsonStreamFile) {
31-
$this->file = new JsonStreamFile($options['file_path'], 'wb');
30+
$options = $this->getOptions($state);
31+
$this->file = new JsonStreamFile(
32+
$options['file_path'],
33+
'wb',
34+
$options['spl_file_object_flags'],
35+
$options['json_flags'],
36+
);
3237
}
3338

3439
$input = $state->getInput();
@@ -60,5 +65,11 @@ protected function configureOptions(OptionsResolver $resolver): void
6065
]
6166
)
6267
);
68+
$resolver->setDefaults([
69+
'spl_file_object_flags' => null,
70+
'json_flags' => null,
71+
]);
72+
$resolver->setAllowedTypes('spl_file_object_flags', ['array', 'null']);
73+
$resolver->setAllowedTypes('json_flags', ['array', 'null']);
6374
}
6475
}

0 commit comments

Comments
 (0)