-
Notifications
You must be signed in to change notification settings - Fork 76
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
Pass any variables between stages #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/composer.lock | ||
/coverage/ | ||
/coverage.xml | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace League\Pipeline; | ||
|
||
interface ParametersInterface | ||
{ | ||
/** | ||
* Get parameters for each stage. | ||
* | ||
* @return array | ||
*/ | ||
public function getParameters(); | ||
|
||
/** | ||
* Set parameters for each stage. | ||
* | ||
* @param array $params | ||
* | ||
* @return void | ||
*/ | ||
public function setParameters(...$params); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace League\Pipeline; | ||
|
||
trait ParametersTrait | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $parameters = []; | ||
|
||
/** | ||
* Get parameters for each stage. | ||
* | ||
* @return array | ||
*/ | ||
public function getParameters() | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
/** | ||
* Set parameters for each stage. | ||
* | ||
* @param array $params | ||
* @return void | ||
*/ | ||
public function setParameters(...$params) | ||
{ | ||
$this->parameters = $params; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,24 @@ class Pipeline implements PipelineInterface | |
*/ | ||
private $processor; | ||
|
||
/** | ||
* Pipeline constructor. | ||
* | ||
* @param \League\Pipeline\ProcessorInterface|null $processor | ||
* @param callable ...$stages | ||
*/ | ||
public function __construct(ProcessorInterface $processor = null, callable ...$stages) | ||
{ | ||
$this->processor = $processor ?? new FingersCrossedProcessor; | ||
$this->stages = $stages; | ||
} | ||
|
||
/** | ||
* One pipe for your task. | ||
* | ||
* @param callable $stage | ||
* @return \League\Pipeline\PipelineInterface|$this | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For others -- ok. But for this docblock for |
||
public function pipe(callable $stage): PipelineInterface | ||
{ | ||
$pipeline = clone $this; | ||
|
@@ -29,13 +41,18 @@ public function pipe(callable $stage): PipelineInterface | |
return $pipeline; | ||
} | ||
|
||
public function process($payload) | ||
public function process($payload, ...$params) | ||
{ | ||
if ($this->processor instanceof ParametersInterface) { | ||
$this->processor->setParameters(...$params); | ||
return $this->processor->process($payload, ...$this->stages); | ||
} | ||
|
||
return $this->processor->process($payload, ...$this->stages); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make more sense for this to be: return $this->processor->withParameters($params)->process(...$this->stages); Doing so will change the signatures significantly though, and cause a major version bump. Let me think about this for a couple of days. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, your variant is more elegant, but needs to bump the major version.
ok. |
||
} | ||
|
||
public function __invoke($payload) | ||
public function __invoke($payload, ...$params) | ||
{ | ||
return $this->process($payload); | ||
return $this->process($payload, ...$params); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I forgot about this signature, makes it significantly trickier to handle. It's pretty straight forward for the Pipeline:
But the processor is different because it receives the stages: