Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/composer.lock
/coverage/
/coverage.xml
.idea
6 changes: 4 additions & 2 deletions src/FingersCrossedProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace League\Pipeline;

class FingersCrossedProcessor implements ProcessorInterface
class FingersCrossedProcessor implements ProcessorInterface, ParametersInterface
{
use ParametersTrait;

public function process($payload, callable ...$stages)
Copy link
Member

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:

function process(...$parameters) { ... }

But the processor is different because it receives the stages:

function process($payload, callable ...$stages) { ... }

{
foreach ($stages as $stage) {
$payload = $stage($payload);
$payload = $stage($payload, ...$this->getParameters());
}

return $payload;
Expand Down
6 changes: 4 additions & 2 deletions src/InterruptibleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace League\Pipeline;

class InterruptibleProcessor implements ProcessorInterface
class InterruptibleProcessor implements ProcessorInterface, ParametersInterface
{
use ParametersTrait;

/**
* @var callable
*/
Expand All @@ -20,7 +22,7 @@ public function process($payload, callable ...$stages)
$check = $this->check;

foreach ($stages as $stage) {
$payload = $stage($payload);
$payload = $stage($payload, ...$this->getParameters());

if (true !== $check($payload)) {
return $payload;
Expand Down
23 changes: 23 additions & 0 deletions src/ParametersInterface.php
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);
}
33 changes: 33 additions & 0 deletions src/ParametersTrait.php
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;
}
}
23 changes: 20 additions & 3 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, PipelineInterface already provides all the docblocks. Same for other docblocks added in this PR.

Copy link
Author

@roquie roquie Jun 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For others -- ok. But for this docblock for PipelineInterface returns only interface without $this. IDE autocompletion works only within interface scope and not touch process method which should be there.

public function pipe(callable $stage): PipelineInterface
{
$pipeline = clone $this;
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Let me think about this for a couple of days.

ok.

}

public function __invoke($payload)
public function __invoke($payload, ...$params)
{
return $this->process($payload);
return $this->process($payload, ...$params);
}
}
1 change: 1 addition & 0 deletions src/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ProcessorInterface
* Process the payload using multiple stages.
*
* @param mixed $payload
* @param callable[] $stages
*
* @return mixed
*/
Expand Down