Skip to content

Commit

Permalink
Merge 4c737c1 into 27dc181
Browse files Browse the repository at this point in the history
  • Loading branch information
ingluisjimenez committed Mar 17, 2017
2 parents 27dc181 + 4c737c1 commit 1270a41
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 44 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm
Expand Down Expand Up @@ -29,5 +28,4 @@ script:

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- if [ "$TRAVIS_PHP_VERSION" == "5.5" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.xml; fi
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.xml; fi
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ $pipeline = (new Pipeline)
$pipeline->process(10);
```

## Variadic arguments

Using variadic arguments, it is possible to pass an unlimited number of extra
arguments to each stage, along with the $payload.
```
$stage1 = function( $payload, $param1, $param2 ) {
// process the payload using the additional params
};
$stage2 = function( $payload, $param1, $param2 ) {
// process the payload using the additional params
};
$pipeline = (new Pipeline)
->pipe($stage1)
->pipe($stage2);
$pipeline->process( $payload, $var1, $var2 );
```

## Re-usable Pipelines

Because the PipelineInterface is an extension of the StageInterface
Expand All @@ -106,12 +127,12 @@ something along these lines:
$processApiRequest = (new Pipeline)
->pipe(new ExecuteHttpRequest) // 2
->pipe(new ParseJsonResponse); // 3

$pipeline = (new Pipeline)
->pipe(new ConvertToPsr7Request) // 1
->pipe($processApiRequest) // (2,3)
->pipe(new ConvertToResponseDto); // 4
->pipe(new ConvertToResponseDto); // 4

$pipeline->process(new DeleteBlogPost($postId));
```

Expand Down Expand Up @@ -147,7 +168,7 @@ pipeline processes a payload.
$pipeline = (new Pipeline)->pipe(function () {
throw new LogicException();
});

try {
$pipeline->process($payload);
} catch(LogicException $e) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"require": {
"php": ">=5.5"
"php": ">=5.6"
},
"scripts": {
"test": "phpspec run"
Expand Down
29 changes: 29 additions & 0 deletions spec/PipelineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ function it_should_process_a_payload()
$this->pipe($operation)->process(1)->shouldBe(2);
}

function it_should_process_a_payload_with_params()
{
$prefix_operation = function ( $strings, $prefix, $suffix ) {
return array_map( function( $string ) use ( $prefix ) {
return "{$prefix}{$string}";
}, $strings );
};

$suffix_operation = function ( $strings, $prefix, $suffix ) {
return array_map( function( $string ) use ( $suffix ) {
return "{$string}{$suffix}";
}, $strings );
};

$payload = [
'test value 1',
'test value 2',
'test value 3',
];

$expected = [
'<test value 1>',
'<test value 2>',
'<test value 3>',
];

$this->pipe($prefix_operation)->pipe($suffix_operation)->process($payload,'<','>')->shouldBe($expected);
}

function it_should_execute_operations_in_sequence()
{
$this->beConstructedWith([
Expand Down
9 changes: 3 additions & 6 deletions src/FingersCrossedProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
class FingersCrossedProcessor implements ProcessorInterface
{
/**
* @param array $stages
* @param mixed $payload
*
* @return mixed
* {@inheritdoc}
*/
public function process(array $stages, $payload)
public function process(array $stages, $payload, ...$params)
{
foreach ($stages as $stage) {
$payload = call_user_func($stage, $payload);
$payload = call_user_func($stage, $payload, ...$params);
}

return $payload;
Expand Down
11 changes: 4 additions & 7 deletions src/InterruptibleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ public function __construct(callable $check)
}

/**
* @param array $stages
* @param mixed $payload
*
* @return mixed
* {@inheritdoc}
*/
public function process(array $stages, $payload)
public function process(array $stages, $payload, ...$params)
{
foreach ($stages as $stage) {
$payload = call_user_func($stage, $payload);
$payload = call_user_func($stage, $payload, ...$params);

if (true !== call_user_func($this->check, $payload)) {
if (true !== call_user_func($this->check, $payload, ...$params)) {
return $payload;
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(array $stages = [], ProcessorInterface $processor =
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function pipe(callable $stage)
{
Expand All @@ -48,22 +48,18 @@ public function pipe(callable $stage)
}

/**
* Process the payload.
*
* @param $payload
*
* @return mixed
* {@inheritdoc}
*/
public function process($payload)
public function process($payload, ...$params)
{
return $this->processor->process($this->stages, $payload);
return $this->processor->process($this->stages, $payload, ...$params);
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function __invoke($payload)
public function __invoke($payload, ...$params)
{
return $this->process($payload);
return $this->process($payload, ...$params);
}
}
14 changes: 12 additions & 2 deletions src/PipelineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ interface PipelineInterface extends StageInterface
/**
* Create a new pipeline with an appended stage.
*
* @param callable $operation
* @param callable $stage
*
* @return static
*/
public function pipe(callable $operation);
public function pipe(callable $stage);

/**
* Process the payload
*
* @param mixed $payload
* @param mixed ...$params
*
* @return mixed
*/
public function process($payload, ...$params);
}
7 changes: 4 additions & 3 deletions src/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
interface ProcessorInterface
{
/**
* @param array $stages
* @param mixed $payload
* @param callable[] $stages
* @param mixed $payload
* @param mixed ...$params
*
* @return mixed
*/
public function process(array $stages, $payload);
public function process(array $stages, $payload, ...$params);
}
5 changes: 3 additions & 2 deletions src/StageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
interface StageInterface
{
/**
* Process the payload.
* Process the payload
*
* @param mixed $payload
* @param mixed ...$params
*
* @return mixed
*/
public function __invoke($payload);
public function __invoke($payload, ...$params);
}
8 changes: 2 additions & 6 deletions stub/StubStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ class StubStage implements StageInterface
const STUBBED_RESPONSE = 'stubbed response';

/**
* Process the payload.
*
* @param mixed $payload
*
* @return mixed
* @inheritdoc
*/
public function __invoke($payload)
public function __invoke($payload, ...$params)
{
return self::STUBBED_RESPONSE;
}
Expand Down

0 comments on commit 1270a41

Please sign in to comment.