From 7944207eca46bfd4165358a22394fe8ec714a02a Mon Sep 17 00:00:00 2001 From: roquie Date: Wed, 13 Jun 2018 21:31:05 +0300 Subject: [PATCH] Pass any variables between stages. --- .gitignore | 1 + src/FingersCrossedProcessor.php | 6 ++++-- src/InterruptibleProcessor.php | 6 ++++-- src/ParametersInterface.php | 23 +++++++++++++++++++++++ src/ParametersTrait.php | 33 +++++++++++++++++++++++++++++++++ src/Pipeline.php | 23 ++++++++++++++++++++--- src/ProcessorInterface.php | 1 + 7 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/ParametersInterface.php create mode 100644 src/ParametersTrait.php diff --git a/.gitignore b/.gitignore index d809c25..30d6065 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /composer.lock /coverage/ /coverage.xml +.idea \ No newline at end of file diff --git a/src/FingersCrossedProcessor.php b/src/FingersCrossedProcessor.php index b76d3c0..560f85e 100644 --- a/src/FingersCrossedProcessor.php +++ b/src/FingersCrossedProcessor.php @@ -3,12 +3,14 @@ namespace League\Pipeline; -class FingersCrossedProcessor implements ProcessorInterface +class FingersCrossedProcessor implements ProcessorInterface, ParametersInterface { + use ParametersTrait; + public function process($payload, callable ...$stages) { foreach ($stages as $stage) { - $payload = $stage($payload); + $payload = $stage($payload, ...$this->getParameters()); } return $payload; diff --git a/src/InterruptibleProcessor.php b/src/InterruptibleProcessor.php index f609774..8b29726 100644 --- a/src/InterruptibleProcessor.php +++ b/src/InterruptibleProcessor.php @@ -3,8 +3,10 @@ namespace League\Pipeline; -class InterruptibleProcessor implements ProcessorInterface +class InterruptibleProcessor implements ProcessorInterface, ParametersInterface { + use ParametersTrait; + /** * @var callable */ @@ -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; diff --git a/src/ParametersInterface.php b/src/ParametersInterface.php new file mode 100644 index 0000000..71798fc --- /dev/null +++ b/src/ParametersInterface.php @@ -0,0 +1,23 @@ +parameters; + } + + /** + * Set parameters for each stage. + * + * @param array $params + * @return void + */ + public function setParameters(...$params) + { + $this->parameters = $params; + } +} \ No newline at end of file diff --git a/src/Pipeline.php b/src/Pipeline.php index 5cbb5cb..b6dae3b 100644 --- a/src/Pipeline.php +++ b/src/Pipeline.php @@ -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 + */ 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); } - public function __invoke($payload) + public function __invoke($payload, ...$params) { - return $this->process($payload); + return $this->process($payload, ...$params); } } diff --git a/src/ProcessorInterface.php b/src/ProcessorInterface.php index 56f9b35..1785f45 100644 --- a/src/ProcessorInterface.php +++ b/src/ProcessorInterface.php @@ -9,6 +9,7 @@ interface ProcessorInterface * Process the payload using multiple stages. * * @param mixed $payload + * @param callable[] $stages * * @return mixed */