Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add syntax sugar to the Process::pipe method #46745

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Illuminate/Process/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ public function pool(callable $callback)
/**
* Start defining a series of piped processes.
*
* @param callable $callback
* @param callable|array $callback
* @return \Illuminate\Process\Pipe
*/
public function pipe(callable $callback, ?callable $output = null)
public function pipe(callable|array $callback, ?callable $output = null)
{
return (new Pipe($this, $callback))->run(output: $output);
return is_array($callback)
taylorotwell marked this conversation as resolved.
Show resolved Hide resolved
? (new Pipe($this, $callback))->runSimple()
: (new Pipe($this, $callback))->run(output: $output);
}

/**
Expand Down
35 changes: 31 additions & 4 deletions src/Illuminate/Process/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class Pipe
protected $factory;

/**
* The callback that resolves the pending processes.
* The callback that resolves the pending processes,
* or array of string processes to run.
*
* @var callable
* @var callable|array
*/
protected $callback;

Expand All @@ -35,10 +36,10 @@ class Pipe
* Create a new series of piped processes.
*
* @param \Illuminate\Process\Factory $factory
* @param callable $callback
* @param callable|array $callback
* @return void
*/
public function __construct(Factory $factory, callable $callback)
public function __construct(Factory $factory, callable|array $callback)
{
$this->factory = $factory;
$this->callback = $callback;
Expand Down Expand Up @@ -86,6 +87,32 @@ public function run(?callable $output = null)
});
}

/**
* Runs the processes in the pipe as simple commands.
*
* @return \Illuminate\Contracts\Process\ProcessResult
*/
public function runSimple()
{
return collect($this->callback)
->reduce(function ($previousProcessResult, $command) {
if (! is_string($command)) {
throw new InvalidArgumentException('Process pipe must only contain strings.');
}

if ($previousProcessResult && $previousProcessResult->failed()) {
return $previousProcessResult;
}

$pendingProcess = $this->command($command);

return $pendingProcess->when(
$previousProcessResult,
fn () => $pendingProcess->input($previousProcessResult->output())
)->run();
});
}

/**
* Dynamically proxy methods calls to a new pending process.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @method static \Illuminate\Process\Factory assertDidntRun(\Closure|string $callback)
* @method static \Illuminate\Process\Factory assertNothingRan()
* @method static \Illuminate\Process\Pool pool(callable $callback)
* @method static \Illuminate\Process\Pipe pipe(callable $callback, callable|null $output = null)
* @method static \Illuminate\Process\Pipe pipe(callable|array $callback, callable|null $output = null)
* @method static \Illuminate\Process\ProcessPoolResults concurrently(callable $callback, callable|null $output = null)
* @method static \Illuminate\Process\PendingProcess newPendingProcess()
* @method static void macro(string $name, object|callable $macro)
Expand Down
38 changes: 38 additions & 0 deletions tests/Process/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,44 @@ public function testProcessPipeFailed()
$this->assertTrue($pipe->failed());
}

public function testProcessSimplePipe()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$factory = new Factory;
$factory->fake([
'cat *' => "Hello, world\nfoo\nbar",
]);

$pipe = $factory->pipe([
'cat test',
'grep -i "foo"',
]);

$this->assertSame("foo\n", $pipe->output());
}

public function testProcessSimplePipeFailed()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$factory = new Factory;
$factory->fake([
'cat *' => $factory->result(exitCode: 1),
]);

$pipe = $factory->pipe([
'cat test',
'grep -i "foo"',
]);

$this->assertTrue($pipe->failed());
}

public function testFakeInvokedProcessOutputWithLatestOutput()
{
$factory = new Factory;
Expand Down