mirror of
https://github.com/pestphp/pest.git
synced 2026-03-09 09:17:23 +01:00
implements decorators pipeline
This commit is contained in:
@ -30,4 +30,9 @@ final class Extendable
|
||||
{
|
||||
$this->extendableClass::extend($name, $extend);
|
||||
}
|
||||
|
||||
public function decorate(string $name, Closure $extend): void
|
||||
{
|
||||
$this->extendableClass::decorate($name, $extend);
|
||||
}
|
||||
}
|
||||
|
||||
69
src/Support/Pipeline.php
Normal file
69
src/Support/Pipeline.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
final class Pipeline
|
||||
{
|
||||
/** @var array<Closure> */
|
||||
private $pipes = [];
|
||||
|
||||
/** @var array<mixed> */
|
||||
private $passable;
|
||||
|
||||
/**
|
||||
* @param array<mixed> $passable
|
||||
*/
|
||||
public function __construct(...$passable)
|
||||
{
|
||||
$this->passable = $passable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<mixed> $passable
|
||||
*/
|
||||
public static function send(...$passable): self
|
||||
{
|
||||
return new self(...$passable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<Closure> $pipes
|
||||
*/
|
||||
public function through(array $pipes): self
|
||||
{
|
||||
$this->pipes = $pipes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function finally(Closure $finalClosure): void
|
||||
{
|
||||
$pipeline = array_reduce(
|
||||
array_reverse($this->pipes),
|
||||
$this->carry(),
|
||||
$this->prepareFinalClosure($finalClosure)
|
||||
);
|
||||
|
||||
$pipeline(...$this->passable);
|
||||
}
|
||||
|
||||
public function carry(): Closure
|
||||
{
|
||||
return function ($stack, $pipe): Closure {
|
||||
return function (...$passable) use ($stack, $pipe) {
|
||||
return $pipe($stack, ...$passable);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
private function prepareFinalClosure(Closure $finalClosure): Closure
|
||||
{
|
||||
return function (...$passable) use ($finalClosure) {
|
||||
return $finalClosure($passable);
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user