Skip to content

Commit 9f08dd7

Browse files
[10.x] Add syntax sugar to the Process::pipe method (#46745)
* 10.x - Add syntax sugar to the Process::pipe method * fix code style issues * simplify code --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent ddbbb2b commit 9f08dd7

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/Illuminate/Process/Factory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,16 @@ public function pool(callable $callback)
274274
/**
275275
* Start defining a series of piped processes.
276276
*
277-
* @param callable $callback
277+
* @param callable|array $callback
278278
* @return \Illuminate\Process\Pipe
279279
*/
280-
public function pipe(callable $callback, ?callable $output = null)
280+
public function pipe(callable|array $callback, ?callable $output = null)
281281
{
282-
return (new Pipe($this, $callback))->run(output: $output);
282+
return is_array($callback)
283+
? (new Pipe($this, fn ($pipe) => collect($callback)->each(
284+
fn ($command) => $pipe->command($command)
285+
)))->run(output: $output)
286+
: (new Pipe($this, $callback))->run(output: $output);
283287
}
284288

285289
/**

src/Illuminate/Support/Facades/Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @method static \Illuminate\Process\Factory assertDidntRun(\Closure|string $callback)
3636
* @method static \Illuminate\Process\Factory assertNothingRan()
3737
* @method static \Illuminate\Process\Pool pool(callable $callback)
38-
* @method static \Illuminate\Process\Pipe pipe(callable $callback, callable|null $output = null)
38+
* @method static \Illuminate\Process\Pipe pipe(callable|array $callback, callable|null $output = null)
3939
* @method static \Illuminate\Process\ProcessPoolResults concurrently(callable $callback, callable|null $output = null)
4040
* @method static \Illuminate\Process\PendingProcess newPendingProcess()
4141
* @method static void macro(string $name, object|callable $macro)

tests/Process/ProcessTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,44 @@ public function testProcessPipeFailed()
534534
$this->assertTrue($pipe->failed());
535535
}
536536

537+
public function testProcessSimplePipe()
538+
{
539+
if (windows_os()) {
540+
$this->markTestSkipped('Requires Linux.');
541+
}
542+
543+
$factory = new Factory;
544+
$factory->fake([
545+
'cat *' => "Hello, world\nfoo\nbar",
546+
]);
547+
548+
$pipe = $factory->pipe([
549+
'cat test',
550+
'grep -i "foo"',
551+
]);
552+
553+
$this->assertSame("foo\n", $pipe->output());
554+
}
555+
556+
public function testProcessSimplePipeFailed()
557+
{
558+
if (windows_os()) {
559+
$this->markTestSkipped('Requires Linux.');
560+
}
561+
562+
$factory = new Factory;
563+
$factory->fake([
564+
'cat *' => $factory->result(exitCode: 1),
565+
]);
566+
567+
$pipe = $factory->pipe([
568+
'cat test',
569+
'grep -i "foo"',
570+
]);
571+
572+
$this->assertTrue($pipe->failed());
573+
}
574+
537575
public function testFakeInvokedProcessOutputWithLatestOutput()
538576
{
539577
$factory = new Factory;

0 commit comments

Comments
 (0)