Repository navigation
Expand file tree
/
Copy pathFileSplitterTask.php
More file actions
137 lines (113 loc) · 4.17 KB
/
Copy pathFileSplitterTask.php
File metadata and controls
137 lines (113 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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;
use CleverAge\ProcessBundle\Filesystem\SplFile;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\IterableTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Split long file into smaller ones.
*/
class FileSplitterTask extends AbstractConfigurableTask implements IterableTaskInterface
{
protected ?SplFile $file = null;
/**
* Next line of the source file to write (read ahead to detect the end of the file), null when there is none.
*/
private ?string $nextLine = null;
public function execute(ProcessState $state): void
{
$options = $this->getMergedOptions($state);
if (!$this->file instanceof SplFile) {
// No flag: lines are read with fgets(), which ignores DROP_NEW_LINE/SKIP_EMPTY and must not be preceded
// by a rewind() in READ_AHEAD mode (the first line would be skipped)
$this->file = new SplFile($options['file_path'], 'rb', []);
$this->nextLine = $this->file->readLine();
}
if (null === $this->nextLine) {
// Empty source file: nothing to split
$this->file = null;
$state->setSkipped(true);
return;
}
// Return a temporary file containing a limited number of lines
$splittedFilename = $this->splitFile($this->file, $options['max_lines']);
$state->setOutput($splittedFilename);
}
/**
* Moves the internal pointer to the next element,
* return true if the task has a next element
* return false if the task has terminated it's iteration.
*/
public function next(ProcessState $state): bool
{
if (!$this->file instanceof SplFile) {
return false;
}
if (null === $this->nextLine) {
$this->file = null;
return false;
}
return true;
}
protected function splitFile(SplFile $file, int $maxLines): string
{
$tmpFilePath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'php_'.uniqid('process', false).'.tmp';
$splitFile = new SplFile($tmpFilePath, 'wb', []);
$writtenLines = 0;
while (null !== $this->nextLine && $writtenLines < $maxLines) {
// fgets() keeps the line break while writeLine() appends one
$splitFile->writeLine($this->stripLineBreak($this->nextLine));
++$writtenLines;
$this->nextLine = $file->readLine();
}
return $tmpFilePath;
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['file_path']);
$resolver->setAllowedTypes('file_path', ['string']);
$resolver->setDefaults([
'max_lines' => 1000,
]);
$resolver->setAllowedTypes('max_lines', ['int']);
$resolver->setAllowedValues('max_lines', static fn (int $value): bool => $value >= 1);
}
/**
* @return array<mixed>
*/
protected function getMergedOptions(ProcessState $state): array
{
/** @var array<mixed> $options */
$options = $this->getOptions($state);
/** @var array<mixed>|mixed $input */
$input = $state->getInput() ?: [];
if (!\is_array($input)) {
$input = [];
}
// @var array<mixed> $input
// Options given as input are validated like the task options
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
return $resolver->resolve(array_merge($options, array_intersect_key($input, $options)));
}
private function stripLineBreak(string $line): string
{
if (str_ends_with($line, "\r\n")) {
return substr($line, 0, -2);
}
if (str_ends_with($line, "\n")) {
return substr($line, 0, -1);
}
return $line;
}
}