Skip to content
Open
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $result = $stage3($stage2($stage1($payload)));

Pipelines are implemented as immutable stage chains. When you pipe a new
stage, a new pipeline will be created with the added stage. This makes
pipelines easy to reuse, and minimizes side-effects.
pipelines easy to reuse, and minimizes side effects.

## Usage

Expand All @@ -58,6 +58,12 @@ type-hint. So closures and anything that's invokable is good.
$pipeline = (new Pipeline)->pipe(function ($payload) {
return $payload * 10;
});

// or

$pipeline = Pipeline::init()->pipe(function ($payload) {
return $payload * 10;
});
```

## Class based stages.
Expand Down Expand Up @@ -127,6 +133,8 @@ any given time.
use League\Pipeline\PipelineBuilder;

// Prepare the builder
// $pipelineBuilder = PipelineBuilder::init()
// or
$pipelineBuilder = (new PipelineBuilder)
->add(new LogicalStage)
->add(new AnotherStage)
Expand Down
5 changes: 5 additions & 0 deletions spec/PipelineBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function it_is_initializable()
$this->shouldHaveType(PipelineBuilder::class);
}

function it_is_static_initializable()
{
$this->shouldBeAnInstanceOf(get_class(PipelineBuilder::init()));
}

function it_should_collect_operations_for_a_pipeline()
{
$this->add(function ($p) {
Expand Down
8 changes: 6 additions & 2 deletions spec/PipelineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace spec\League\Pipeline;

use InvalidArgumentException;
use League\Pipeline\CallableStage;
use League\Pipeline\Pipeline;
use League\Pipeline\PipelineInterface;
use League\Pipeline\Stub\StubStage;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class PipelineSpec extends ObjectBehavior
{
Expand All @@ -30,6 +28,12 @@ function it_should_compose_pipelines()
$this->pipe($pipeline)->process('something')->shouldBe(10);
}

function it_should_initiate_and_compose_pipelines()
{
$pipeline = Pipeline::init()->pipe(function () { return 10; });
$this->pipe($pipeline)->process('something')->shouldBe(10);
}

function it_should_process_a_payload()
{
$operation = function ($payload) { return $payload + 1; };
Expand Down
7 changes: 6 additions & 1 deletion src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Pipeline implements PipelineInterface
/**
* @var callable[]
*/
private $stages = [];
private $stages;

/**
* @var ProcessorInterface
Expand All @@ -21,6 +21,11 @@ public function __construct(ProcessorInterface $processor = null, callable ...$s
$this->stages = $stages;
}

public static function init(ProcessorInterface $processor = null, callable ...$stages): PipelineInterface
{
return new self($processor, ...$stages);
}

public function pipe(callable $stage): PipelineInterface
{
$pipeline = clone $this;
Expand Down
8 changes: 8 additions & 0 deletions src/PipelineBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class PipelineBuilder implements PipelineBuilderInterface
*/
private $stages = [];

/**
* @return self
*/
public static function init(): PipelineBuilderInterface
{
return new self();
}

/**
* @return self
*/
Expand Down
1 change: 1 addition & 0 deletions src/PipelineBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface PipelineBuilderInterface
/**
* Add an stage.
*
* @param callable $stage
* @return self
*/
public function add(callable $stage): PipelineBuilderInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/PipelineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface PipelineInterface extends StageInterface
*
* @return static
*/
public function pipe(callable $operation): PipelineInterface;
public function pipe(callable $stage): PipelineInterface;

/**
* Process the payload.
Expand Down