forked from thephpleague/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes thephpleague#38, Implement Fork-Join with builder support
- Loading branch information
Juzer Ali
committed
Jan 9, 2018
1 parent
d80cb46
commit a4d876f
Showing
10 changed files
with
267 additions
and
14 deletions.
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 8:20 PM | ||
*/ | ||
|
||
namespace spec\League\Pipeline; | ||
|
||
|
||
use League\Pipeline\PipelineBuilder; | ||
use PhpSpec\Exception\Exception; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class NewBuilderSpec extends ObjectBehavior | ||
{ | ||
public function it_should_pass() | ||
{ | ||
$pipeline = (new PipelineBuilder()) | ||
->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;}) | ||
->build() | ||
; | ||
|
||
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,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 7:47 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface BuilderInterface | ||
{ | ||
public function build(); | ||
} |
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,31 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 6:52 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
class DisjointPipelineBuilder extends PipelineBuilder implements DisjointPipelineBuilderInterface | ||
{ | ||
private $parent; | ||
|
||
public function __construct(ForkBuilder $parent, callable $stage = null) | ||
{ | ||
if($stage != null) $this->stages[] = $stage; | ||
$this->parent = $parent; | ||
} | ||
|
||
public function disjoin(string $tag, callable $stage = null) | ||
{ | ||
return $this->parent->disjoin($tag, $stage); | ||
} | ||
|
||
public function join() | ||
{ | ||
return $this->parent->join(); | ||
} | ||
} |
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: 6:51 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface DisjointPipelineBuilderInterface extends PipelineBuilderInterface, ForkBuilderInterface | ||
{ | ||
public function join(); | ||
} |
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,63 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 7:35 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
class ForkBuilder implements ForkBuilderInterface | ||
{ | ||
/** | ||
* @var PipelineBuilder | ||
*/ | ||
private $parent; | ||
|
||
|
||
private $forks = []; | ||
|
||
private $resolver; | ||
|
||
/** | ||
* @var BuilderInterface | ||
*/ | ||
private $currentBuilder; | ||
private $currentTag; | ||
|
||
public function __construct(callable $resolver, PipelineBuilder $parent) | ||
{ | ||
$this->resolver = $resolver; | ||
$this->parent = $parent; | ||
} | ||
|
||
public function disjoin(string $tag, callable $stage = null) | ||
{ | ||
$this->buildPrevious(); | ||
$this->currentTag = $tag; | ||
$this->currentBuilder = new DisjointPipelineBuilder($this, $stage); | ||
return $this->currentBuilder; | ||
} | ||
|
||
public function join() | ||
{ | ||
$this->buildPrevious(); | ||
return $this->parent; | ||
} | ||
|
||
private function buildPrevious() | ||
{ | ||
if($this->currentTag == null) return; | ||
$this->forks[$this->currentTag] = $this->currentBuilder->build(); | ||
|
||
$this->currentTag = null; | ||
$this->currentBuilder = null; | ||
} | ||
|
||
public function build() | ||
{ | ||
return new Fork($this->resolver, $this->forks); | ||
} | ||
} |
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: 7:28 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface ForkBuilderInterface extends BuilderInterface | ||
{ | ||
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,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: juzerali | ||
* Date: 09/01/18 | ||
* Time: 7:29 PM | ||
*/ | ||
|
||
namespace League\Pipeline; | ||
|
||
|
||
interface ForkedPipelineBuilderInterface extends ForkBuilderInterface | ||
{ | ||
public function join(); | ||
} |
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 |
---|---|---|
@@ -1,23 +1,30 @@ | ||
<?php | ||
namespace League\Pipeline; | ||
|
||
interface PipelineBuilderInterface | ||
interface PipelineBuilderInterface extends BuilderInterface | ||
{ | ||
/** | ||
* Add an stage. | ||
* Add a stage. | ||
* | ||
* @param callable $stage | ||
* | ||
* @return $this | ||
*/ | ||
public function add(callable $stage); | ||
public function pipe(callable $stage); | ||
|
||
/** | ||
* Forks the pipeline | ||
* | ||
* @param callable $resolver | ||
* @return mixed | ||
*/ | ||
public function fork(callable $resolver); | ||
|
||
/** | ||
* Build a new Pipeline object | ||
* | ||
* @param ProcessorInterface|null $processor | ||
* | ||
* @return PipelineInterface | ||
*/ | ||
public function build(ProcessorInterface $processor = null); | ||
public function build(); | ||
} |