php实现管道
PHP 实现管道模式
管道模式(Pipeline Pattern)是一种将复杂任务分解为多个独立步骤的设计模式,每个步骤由一个处理器(Processor)处理,数据通过管道依次传递。PHP 可以通过多种方式实现管道模式。
基础实现方式
使用闭包和数组实现简单的管道:

$pipeline = array_reduce(
array_reverse([
function ($value) { return $value * 2; },
function ($value) { return $value + 5; },
function ($value) { return $value / 3; }
]),
function ($carry, $item) {
return function ($value) use ($carry, $item) {
return $item($carry($value));
};
},
function ($value) { return $value; }
);
echo $pipeline(10); // 输出: ((10 / 3) + 5) * 2
面向对象实现
创建更结构化的管道类:
interface PipelineStage {
public function process($payload);
}
class MultiplyStage implements PipelineStage {
private $factor;
public function __construct($factor) {
$this->factor = $factor;
}
public function process($payload) {
return $payload * $this->factor;
}
}
class Pipeline {
private $stages = [];
public function addStage(PipelineStage $stage) {
$this->stages[] = $stage;
return $this;
}
public function process($initial) {
return array_reduce(
$this->stages,
function ($carry, $stage) {
return $stage->process($carry);
},
$initial
);
}
}
$pipeline = new Pipeline();
$pipeline->addStage(new MultiplyStage(2))
->addStage(new MultiplyStage(3));
echo $pipeline->process(5); // 输出: 30
Laravel 风格的管道实现
Laravel 框架提供了更高级的管道实现:

interface Pipe {
public function handle($passable, $next);
}
class TrimStrings implements Pipe {
public function handle($passable, $next) {
$passable = trim($passable);
return $next($passable);
}
}
class Pipeline {
protected $pipes = [];
public function through(array $pipes) {
$this->pipes = $pipes;
return $this;
}
public function then($passable) {
$pipeline = array_reduce(
array_reverse($this->pipes),
$this->carry(),
function ($passable) { return $passable; }
);
return $pipeline($passable);
}
protected function carry() {
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if (is_callable($pipe)) {
return $pipe($passable, $stack);
} elseif (is_object($pipe)) {
return $pipe->handle($passable, $stack);
}
throw new RuntimeException("Invalid pipe type");
};
};
}
}
$result = (new Pipeline)
->through([new TrimStrings()])
->then(' hello ');
echo $result; // 输出: "hello"
中间件管道模式
适用于HTTP中间件的实现方式:
interface Middleware {
public function handle($request, $next);
}
class Pipeline {
private $middlewares;
public function __construct(array $middlewares) {
$this->middlewares = $middlewares;
}
public function handle($request) {
$pipeline = $this->createPipeline();
return $pipeline($request);
}
private function createPipeline() {
return array_reduce(
array_reverse($this->middlewares),
function ($next, $middleware) {
return function ($request) use ($next, $middleware) {
return $middleware->handle($request, $next);
};
},
function ($request) { return $request; }
);
}
}
使用生成器的管道
处理大数据流的高效方式:
function generatorPipeline(array $stages) {
return function ($initial) use ($stages) {
$value = $initial;
foreach ($stages as $stage) {
$value = $stage($value);
}
return $value;
};
}
$pipeline = generatorPipeline([
function ($x) { return $x * 2; },
function ($x) { return $x + 3; }
]);
echo $pipeline(5); // 输出: 13
每种实现方式适用于不同场景,基础闭包方式适合简单转换,面向对象方式更适合复杂系统,Laravel风格适合框架集成,而生成器方式适合处理数据流。






