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

[9.x] Add ability to push additional pipes onto a pipeline via chain($pipes) #41256

Merged
merged 5 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public function through($pipes)
return $this;
}

/**
* Push additional pipes onto the pipeline.
*
* @param array|mixed $pipes
* @return $this
*/
public function pipe($pipes)
{
array_push($this->pipes, ...(is_array($pipes) ? $pipes : func_get_args()));

return $this;
}

/**
* Set the method to call on the pipes.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Pipeline/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Pipeline\Pipeline;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;

class PipelineTest extends TestCase
{
Expand Down Expand Up @@ -96,6 +97,32 @@ function ($piped) {
unset($_SERVER['__test.pipe.one']);
}

public function testPipelineUsageWithPipe()
{
$object = new stdClass();

$object->value = 0;

$function = function ($object, $next) {
$object->value++;

return $next($object);
};

$result = (new Pipeline(new Container))
->send($object)
->through([$function])
->pipe([$function])
->then(
function ($piped) {
return $piped;
}
);

$this->assertSame($object, $result);
$this->assertEquals(2, $object->value);
}

public function testPipelineUsageWithInvokableClass()
{
$result = (new Pipeline(new Container))
Expand Down