Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
324 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/composer.lock | ||
/coverage/ | ||
/coverage.xml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace spec\League\Pipeline; | ||
|
||
use League\Pipeline\Pipeline; | ||
use PhpSpec\Exception\Exception; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ForkJoinSpec extends ObjectBehavior | ||
{ | ||
|
||
public function it_should_pass() | ||
{ | ||
$pipeline = (new Pipeline()) | ||
->pipe(function($payload) {return $payload * 2;}) | ||
->fork(function($payload) { | ||
if($payload == 0) return '0'; | ||
if($payload > 0) return '+'; | ||
if($payload < 0) return '-'; | ||
return false; | ||
}) | ||
->disjoin('0') | ||
->pipe(function() {return INF;}) | ||
->disjoin('+') | ||
->pipe(function($payload) {return sqrt($payload);}) | ||
->pipe(function($payload) {return $payload / 2;}) | ||
->disjoin('-', function() {return NAN;}) | ||
->join() | ||
->pipe(function ($payload) {return "&" . $payload;}); | ||
|
||
if(($result = $pipeline->process(0)) != '&INF') | ||
{ | ||
throw new Exception('Should be INF but was ' . $result); | ||
} | ||
|
||
if(($result = $pipeline->process(32)) != '&4') | ||
{ | ||
throw new Exception('Should be 4 but was ' . $result); | ||
} | ||
|
||
if($pipeline->process(-1) != '&NAN') | ||
{ | ||
throw new Exception('Should be NAN'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 1:58 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
/** | ||
* Internal class used as a wrapper around pipelines within a fork. | ||
* There should be no need to use this class directly. | ||
* | ||
* Class DisjointAwarePipeline | ||
* @package League\Pipeline | ||
*/ | ||
class DisjointAwarePipeline extends Pipeline implements DisjointAwarePipelineInterface | ||
{ | ||
/** | ||
* @var Fork | ||
*/ | ||
public $fork; | ||
|
||
/** | ||
* DisjointAwarePipeline constructor. | ||
* @param Fork $fork | ||
*/ | ||
public function __construct(Fork $fork) | ||
{ | ||
parent::__construct(); | ||
$this->fork = $fork; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function disjoin(string $tag, callable $stage = null) | ||
{ | ||
return $this->fork->disjoin($tag, $stage); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function join(callable $resolver = null) | ||
{ | ||
return $this->fork->join($resolver); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function pipe(callable $stage) | ||
{ | ||
$this->stages[] = $stage; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 4:04 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface DisjointAwarePipelineInterface extends PipelineInterface, ForkInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
class Fork implements ForkInterface | ||
{ | ||
/** | ||
* @var Pipeline | ||
*/ | ||
private $parent; | ||
|
||
/** | ||
* @var array callable | ||
*/ | ||
protected $forks = []; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
protected $resolver; | ||
|
||
/** | ||
* Fork constructor. | ||
* | ||
* @param callable|null $resolver | ||
*/ | ||
public function __construct(callable $resolver = null) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
public function pipeline(Pipeline $pipeline) | ||
{ | ||
$this->parent = $pipeline; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function join(callable $resolver = null) | ||
{ | ||
if($resolver != null) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
return $this->parent; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function disjoin(string $tag, callable $stage = null) | ||
{ | ||
$pipeline = new DisjointAwarePipeline($this); | ||
|
||
if($stage != null) | ||
{ | ||
$pipeline = $pipeline->pipe($stage); | ||
} | ||
|
||
$this->forks[$tag] = $pipeline; | ||
return $pipeline; | ||
} | ||
|
||
/** | ||
* Chooses a fork or short-circuits based on $resolver | ||
* | ||
* @param mixed $payload | ||
* @return mixed | ||
*/ | ||
public function __invoke($payload) | ||
{ | ||
$flowTo = call_user_func($this->resolver, $payload); | ||
if($flowTo === false) return $payload; | ||
$result = $this->forks[$flowTo]->process($payload); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface ForkInterface extends StageInterface | ||
{ | ||
/** | ||
* Builder method that joins all the branches in a fork | ||
* | ||
* @param callable|null $resolver | ||
* | ||
* @return PipelineInterface | ||
*/ | ||
public function join(callable $resolver = null); | ||
|
||
/** | ||
* Adds a branch to the fork. | ||
* | ||
* @param string $tag | ||
* @param callable|null $stage | ||
* | ||
* @return DisjointAwarePipelineInterface | ||
*/ | ||
public function disjoin(string $tag, callable $stage = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 2:35 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface ForkResolver | ||
{ | ||
/** | ||
* Resolve the direction in the fork. Should return the tag of the disjoin. | ||
* Return false to short-circuit the fork and directly jump to join. | ||
* | ||
* @param mixed $payload | ||
* | ||
* @return string|integer|boolean | ||
*/ | ||
public function __invoke($payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters