Skip to content

Commit 7be4788

Browse files
committed
feat(runable): add setPipe method to support process piping
- Introduce a new protected property `$pipe` to hold a callable that transforms a Process instance - Add `setPipe` method to assign a callable to `$pipe` and enable method chaining - Modify `run` method to invoke the `$pipe` callable on the process if set, allowing process manipulation before returning - Maintain existing functionality of `$tap` callable for side effects without altering the process - Enhance flexibility in handling Process objects by enabling custom piping logic Signed-off-by: guanguans <[email protected]>
1 parent 31f316a commit 7be4788

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/Concerns/WithRunable.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@
2020
*/
2121
trait WithRunable
2222
{
23+
/** @var null|callable(Process):Process */
24+
protected $pipe;
25+
2326
/** @var null|callable(Process):void */
2427
protected $tap;
2528

29+
public function setPipe(?callable $pipe): self
30+
{
31+
$this->pipe = $pipe;
32+
33+
return $this;
34+
}
35+
2636
public function setTap(?callable $tap): self
2737
{
2838
$this->tap = $tap;
@@ -43,19 +53,16 @@ public function run(?callable $callback = null): string
4353
*/
4454
protected function toProcess(): Process
4555
{
46-
$normalizedOptions = $this->clone()->getNormalizedOptions();
56+
$command = [$this->soarBinary, ...$this->clone()->getNormalizedOptions()];
4757

4858
$process = $this->shouldApplySudoPassword()
49-
? new Process(
50-
command: ['sudo', '-S', $this->soarBinary, ...$normalizedOptions],
51-
input: $this->getSudoPassword()
52-
)
53-
: new Process([$this->soarBinary, ...$normalizedOptions]);
59+
? new Process(command: ['sudo', '-S', ...$command], input: $this->getSudoPassword())
60+
: new Process($command);
5461

5562
if (\is_callable($this->tap)) {
5663
($this->tap)($process);
5764
}
5865

59-
return $process;
66+
return \is_callable($this->pipe) ? ($this->pipe)($process) : $process;
6067
}
6168
}

0 commit comments

Comments
 (0)