Skip to content

static init() for usability #59

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -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

@@ -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.
@@ -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)
5 changes: 5 additions & 0 deletions spec/PipelineBuilderSpec.php
Original file line number Diff line number Diff line change
@@ -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) {
8 changes: 6 additions & 2 deletions spec/PipelineSpec.php
Original file line number Diff line number Diff line change
@@ -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
{
@@ -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; };
7 changes: 6 additions & 1 deletion src/Pipeline.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class Pipeline implements PipelineInterface
/**
* @var callable[]
*/
private $stages = [];
private $stages;

/**
* @var ProcessorInterface
@@ -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;
8 changes: 8 additions & 0 deletions src/PipelineBuilder.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,14 @@ class PipelineBuilder implements PipelineBuilderInterface
*/
private $stages = [];

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

/**
* @return self
*/
1 change: 1 addition & 0 deletions src/PipelineBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ interface PipelineBuilderInterface
/**
* Add an stage.
*
* @param callable $stage
* @return self
*/
public function add(callable $stage): PipelineBuilderInterface;
2 changes: 1 addition & 1 deletion src/PipelineInterface.php
Original file line number Diff line number Diff line change
@@ -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.