yjx/easy-pipeline

An easy pipeline, learn Laravel.

v1.0.1 2018-05-28 07:45 UTC

This package is auto-updated.

Last update: 2024-03-29 03:20:04 UTC


README

一个简单管道的实现, 参考Laravel的管道实现.

本项目创建的目的在于个人理解管道设计模式, 同时想要有一个不依赖于任何框架的管道实现.

使用

(new EasyPipeline\Pipeline())->send($passable)->through($pipes)->then($lastPipe);

若将管道用于web请求的中间件组合

$response = (new EasyPipeline\Pipeline())
	->send($request)
	->through($middlewares)
	->then($dispatch);

中间件的定义方式

$middlewares = [
	VerifyCsrfToken::class,	// 类名
	
	[ThrottleRequests::class, [60, 1]],	// 数组形式: 类名 + 数组参数
	
	ThrottleRequests::class."?60:1",	// 字符串形式: 类名 + 参数
	
	"throttle?60:1",	// 字符串别名形式: 管理实例化时需传入容器实例
	
	function ($request, \Closure $next) {	// 闭包
		//...
		return $next($request);
	},
];

使用非闭包方式定义的中间件时, 需注入容器(实现ContainerInterface)以便Pipeline实例化中间件.

若指定某个类, 则该类需实现 'handle'方法 或 '__invoke'方法.