|
4 | 4 |
|
5 | 5 | namespace PetrKnap\ExternalFilter; |
6 | 6 |
|
7 | | -use InvalidArgumentException; |
8 | 7 | use Symfony\Component\Process\Exception\ProcessFailedException; |
9 | 8 | use Symfony\Component\Process\Process; |
10 | 9 |
|
11 | | -final class Filter |
| 10 | +/** |
| 11 | + * @todo make it abstract and remove extends |
| 12 | + */ |
| 13 | +final class Filter extends PipelinableFilter |
12 | 14 | { |
13 | | - private self|null $previous = null; |
14 | | - |
15 | 15 | /** |
| 16 | + * @deprecated use {@see self::new()} |
| 17 | + * |
16 | 18 | * @param non-empty-string $command |
17 | | - * @param array<non-empty-string> $options |
| 19 | + * @param array<non-empty-string>|null $options |
18 | 20 | */ |
19 | 21 | public function __construct( |
20 | 22 | private readonly string $command, |
21 | | - private readonly array $options = [], |
| 23 | + private readonly array|null $options = null, |
22 | 24 | ) { |
23 | 25 | } |
24 | 26 |
|
25 | 27 | /** |
26 | | - * @param string|resource $input |
27 | | - * @param resource|null $output |
28 | | - * @param resource|null $error |
29 | | - * |
30 | | - * @return ($output is null ? string : null) |
31 | | - * |
32 | | - * @throws Exception\FilterException |
| 28 | + * @param non-empty-string $command |
| 29 | + * @param array<non-empty-string>|null $options |
| 30 | + */ |
| 31 | + public static function new(string $command, array|null $options = null): PipelinableFilter |
| 32 | + { |
| 33 | + return new Filter($command, $options); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @todo move it to {@see ExternalFilter} |
33 | 38 | */ |
34 | 39 | public function filter(mixed $input, mixed $output = null, mixed $error = null): string|null |
35 | 40 | { |
36 | | - if (!is_string($input) && !is_resource($input)) { |
37 | | - throw new class ('$input must be string|resource') extends InvalidArgumentException implements Exception\FilterException { |
38 | | - }; |
39 | | - } |
40 | | - if ($output !== null && !is_resource($output)) { |
41 | | - throw new class ('$output must be resource|null') extends InvalidArgumentException implements Exception\FilterException { |
42 | | - }; |
43 | | - } |
44 | | - if ($error !== null && !is_resource($error)) { |
45 | | - throw new class ('$error must be resource|null') extends InvalidArgumentException implements Exception\FilterException { |
46 | | - }; |
| 41 | + self::checkFilterArguments( |
| 42 | + input: $input, |
| 43 | + output: $output, |
| 44 | + error: $error, |
| 45 | + ); |
| 46 | + |
| 47 | + $headlessPipeline = self::transformPipeline($this->buildPipeline()); |
| 48 | + $pipelineHead = array_pop($headlessPipeline); |
| 49 | + foreach ($headlessPipeline as $filter) { |
| 50 | + $filter->setInput($input); |
| 51 | + $filter->start(self::buildProcessCallback( |
| 52 | + output: null, |
| 53 | + error: $error, |
| 54 | + )); |
| 55 | + $input = $filter; |
47 | 56 | } |
| 57 | + $process = $pipelineHead; |
| 58 | + |
| 59 | + $process->setInput($input); |
| 60 | + $process->run(self::buildProcessCallback( |
| 61 | + output: $output, |
| 62 | + error: $error, |
| 63 | + )); |
| 64 | + self::checkFinishedProcess($process); |
| 65 | + |
| 66 | + return $output === null ? $process->getOutput() : null; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @todo move it to {@see ExternalFilter} |
| 71 | + */ |
| 72 | + protected function clone(): static |
| 73 | + { |
| 74 | + return new self($this->command, $this->options); |
| 75 | + } |
48 | 76 |
|
49 | | - $process = $this->startFilter($input, static function (string $type, string $data) use ($output, $error): void { |
| 77 | + /** |
| 78 | + * @todo move it to {@see ExternalFilter} |
| 79 | + * |
| 80 | + * @param resource|null $output |
| 81 | + * @param resource|null $error |
| 82 | + */ |
| 83 | + private static function buildProcessCallback(mixed $output, mixed $error): callable |
| 84 | + { |
| 85 | + return static function (string $type, string $data) use ($output, $error): void { |
50 | 86 | /** @var Process::OUT|Process::ERR $type */ |
51 | 87 | match ($type) { |
52 | | - Process::OUT => $output === null or fwrite($output, $data), |
53 | | - Process::ERR => $error === null or fwrite($error, $data), |
54 | | - }; |
55 | | - }); |
56 | | - $process->wait(); |
57 | | - if (!$process->isSuccessful()) { |
58 | | - throw new class ($process) extends ProcessFailedException implements Exception\FilterException { |
| 88 | + Process::OUT => $output === null or fputs($output, $data), |
| 89 | + Process::ERR => $error === null or fputs($error, $data), |
59 | 90 | }; |
60 | | - } |
61 | | - return $output === null ? $process->getOutput() : null; |
| 91 | + }; |
62 | 92 | } |
63 | 93 |
|
64 | | - public function pipe(self $to): self |
| 94 | + /** |
| 95 | + * @todo move it to {@see ExternalFilter} |
| 96 | + */ |
| 97 | + private static function checkFinishedProcess(Process $finishedProcess): void |
65 | 98 | { |
66 | | - $reversedPipeline = []; |
67 | | - $head = $to; |
68 | | - while ($head !== null) { |
69 | | - $reversedPipeline[] = $head; |
70 | | - $head = $head->previous; |
71 | | - } |
72 | | - |
73 | | - $base = $this; |
74 | | - foreach (array_reverse($reversedPipeline) as $next) { |
75 | | - $next = new self($next->command, $next->options); |
76 | | - $next->previous = $base; |
77 | | - $base = $next; |
| 99 | + if (!$finishedProcess->isSuccessful()) { |
| 100 | + throw new class ($finishedProcess) extends ProcessFailedException implements Exception\FilterException { |
| 101 | + }; |
78 | 102 | } |
79 | | - |
80 | | - return $base; |
81 | 103 | } |
82 | 104 |
|
83 | 105 | /** |
84 | | - * @param string|resource $input |
| 106 | + * @todo move it to {@see ExternalFilter} |
| 107 | + * |
| 108 | + * @param non-empty-array<PipelinableFilter> $pipeline |
| 109 | + * |
| 110 | + * @return non-empty-array<Process> |
85 | 111 | */ |
86 | | - private function startFilter(mixed $input, callable|null $callback): Process |
| 112 | + private static function transformPipeline(array $pipeline): array |
87 | 113 | { |
88 | | - if ($this->previous !== null) { |
89 | | - $input = $this->previous->startFilter($input, null); |
| 114 | + $transformedPipeline = []; |
| 115 | + foreach ($pipeline as $filter) { |
| 116 | + if ($filter instanceof self) { |
| 117 | + $transformedPipeline[] = new Process([ |
| 118 | + $filter->command, |
| 119 | + ...($filter->options ?? []), |
| 120 | + ]); |
| 121 | + } else { |
| 122 | + throw new \BadMethodCallException('$pipeline contains unsupported filter'); |
| 123 | + } |
90 | 124 | } |
91 | | - |
92 | | - $process = new Process([ |
93 | | - $this->command, |
94 | | - ...$this->options, |
95 | | - ]); |
96 | | - $process->setInput($input); |
97 | | - $process->start($callback); |
98 | | - |
99 | | - return $process; |
| 125 | + return $transformedPipeline; |
100 | 126 | } |
101 | 127 | } |
0 commit comments