-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pipeline.php
219 lines (196 loc) · 6.71 KB
/
Pipeline.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Kiboko\Component\Pipeline;
use Kiboko\Contract\Pipeline\ExtractingInterface;
use Kiboko\Contract\Pipeline\ExtractorInterface;
use Kiboko\Contract\Pipeline\FlushableInterface;
use Kiboko\Contract\Pipeline\LoaderInterface;
use Kiboko\Contract\Pipeline\LoadingInterface;
use Kiboko\Contract\Pipeline\PipelineInterface;
use Kiboko\Contract\Pipeline\PipelineRunnerInterface;
use Kiboko\Contract\Pipeline\StateInterface;
use Kiboko\Contract\Pipeline\StepCodeInterface;
use Kiboko\Contract\Pipeline\StepRejectionInterface;
use Kiboko\Contract\Pipeline\StepStateInterface;
use Kiboko\Contract\Pipeline\TransformerInterface;
use Kiboko\Contract\Pipeline\TransformingInterface;
use Kiboko\Contract\Pipeline\WalkableInterface;
use Kiboko\Contract\Satellite\RunnableInterface;
class Pipeline implements PipelineInterface, WalkableInterface, RunnableInterface
{
/** @var \AppendIterator<int<0, max>, non-empty-array<array-key, mixed>|object, \Iterator<int<0, max>, non-empty-array<array-key, mixed>|object>> */
private readonly \AppendIterator $source;
/** @var \Iterator<int<0, max>, non-empty-array<array-key, mixed>|object>|\NoRewindIterator */
private iterable $subject;
public function __construct(
private readonly PipelineRunnerInterface $runner,
private readonly StateInterface $state,
\Iterator $source = null
) {
$this->source = new \AppendIterator();
$this->source->append($source ?? new \EmptyIterator());
$this->subject = new \NoRewindIterator($this->source);
}
/**
* @template InputType of non-empty-array<array-key, mixed>|object
*
* @param InputType ...$data
*/
public function feed(...$data): void
{
$this->source->append(new \ArrayIterator($data));
}
private function passThroughCoroutine(): \Generator
{
$line = yield;
/* @phpstan-ignore-next-line */
while (true) {
$line = yield $line;
}
}
/**
* @template Type of non-empty-array<array-key, mixed>|object
*
* @param ExtractorInterface<Type> $extractor
* @param StepRejectionInterface<Type> $rejection
*/
public function extract(
StepCodeInterface $stepCode,
ExtractorInterface $extractor,
StepRejectionInterface $rejection,
StepStateInterface $state,
): ExtractingInterface {
$extract = $extractor->extract();
if (\is_array($extract)) {
$this->source->append(
$this->runner->run(
new \ArrayIterator($extract),
$this->passThroughCoroutine(),
$rejection,
$state,
)
);
} elseif ($extract instanceof \Iterator) {
$this->source->append(
$this->runner->run(
$extract,
$this->passThroughCoroutine(),
$rejection,
$state,
)
);
} elseif ($extract instanceof \Traversable) {
$this->source->append(
$this->runner->run(
new \IteratorIterator($extract),
$this->passThroughCoroutine(),
$rejection,
$state,
)
);
} else {
throw new \RuntimeException('Invalid data source, expecting array or Traversable.');
}
return $this;
}
/**
* @template InputType of non-empty-array<array-key, mixed>|object
* @template OutputType of non-empty-array<array-key, mixed>|object
*
* @param TransformerInterface<InputType, OutputType> $transformer
* @param StepRejectionInterface<InputType> $rejection
*/
public function transform(
StepCodeInterface $stepCode,
TransformerInterface $transformer,
StepRejectionInterface $rejection,
StepStateInterface $state,
): TransformingInterface {
if ($transformer instanceof FlushableInterface) {
$iterator = new \AppendIterator();
$iterator->append(
$this->runner->run(
$this->subject,
$transformer->transform(),
$rejection,
$state,
)
);
$iterator->append(
$this->runner->run(
new \ArrayIterator([null]),
(function () use ($transformer): \Generator {
yield;
yield $transformer->flush();
})(),
$rejection,
$state,
)
);
} else {
$iterator = $this->runner->run(
$this->subject,
$transformer->transform(),
$rejection,
$state,
);
}
$this->subject = new \NoRewindIterator($iterator);
return $this;
}
/**
* @template InputType of non-empty-array<array-key, mixed>|object
* @template OutputType of non-empty-array<array-key, mixed>|object
*
* @param LoaderInterface<InputType, OutputType> $loader
* @param StepRejectionInterface<InputType> $rejection
*/
public function load(
StepCodeInterface $stepCode,
LoaderInterface $loader,
StepRejectionInterface $rejection,
StepStateInterface $state,
): LoadingInterface {
if ($loader instanceof FlushableInterface) {
$iterator = new \AppendIterator();
$iterator->append(
$this->runner->run(
$this->subject,
$loader->load(),
$rejection,
$state,
)
);
$iterator->append(
$this->runner->run(
new \ArrayIterator([null]),
(function () use ($loader): \Generator {
yield;
yield $loader->flush();
})(),
$rejection,
$state,
)
);
} else {
$iterator = $this->runner->run(
$this->subject,
$loader->load(),
$rejection,
$state,
);
}
$this->subject = new \NoRewindIterator($iterator);
return $this;
}
public function walk(): \Iterator
{
$this->state->initialize();
yield from $this->subject;
$this->state->teardown();
}
public function run(int $interval = 1000): int
{
return iterator_count($this->walk());
}
}