jjanvier / yamo
又一个中间件编排器。一个简单的PSR-15实现。
0.1.0
2017-05-25 13:45 UTC
Requires
- http-interop/http-factory: ^0.3.0
- http-interop/http-middleware: ^0.4.1
- zendframework/zend-diactoros: ^1.4
Requires (Dev)
- http-interop/http-factory-diactoros: ^0.3.0
- katzgrau/klogger: ^1.2
- php-di/php-di: ^5.4
- phpspec/phpspec: ^3.4
- phpunit/phpunit: ^6.1
- psr/cache: ^1.0
- symfony/cache: ^3.2
This package is auto-updated.
Last update: 2024-09-17 09:59:02 UTC
README
Yamo是又一个PSR-15中间件编排器。
这个库的目的只是为了帮助发现和理解PSR-15关于HTTP中间件的内容。
用法
只需提供您想使用的中间件列表
$orchestrator = new Orchestrator([
// a list of Psr\Http\ServerMiddleware\MiddlewareInterface middlewares
]
);
$response = $orchestrator->process($request);
请注意,中间件的调用顺序与它们定义的顺序完全一致。例如
$orchestrator = new Orchestrator([
$middleware1,
$middleware2,
$middleware3,
]
);
$response = $orchestrator->process($request);
将导致以下执行流程
每个中间件都可以(如果需要)丰富传入的请求,直到它达到核心应用程序。然后,核心应用程序处理请求以生成响应。然后将此响应传递给每个中间件,中间件也可以(如果需要)丰富响应。最后,当所有中间件都已到达时,编排器返回最终响应。
或者,您还可以定义您想使用的PSR-17响应工厂工厂。如果没有中间件提供响应,此工厂将用于返回默认的200响应。默认响应工厂是Http\Factory\Diactoros\ResponseFactory
。
$customResponseFactory = new Http\Factory\Guzzle\ResponseFactory; // any Psr\Http\Message\ResponseFactoryInterface
$orchestrator = new Orchestrator([
// a list of Psr\Http\ServerMiddleware\MiddlewareInterface middlewares
],
$customResponseFactory
);
$response = $orchestrator->process($request);
示例
出于“学术”目的,一些中间件示例提供在examples文件夹中。这些类不应使用。它们可以被视为“高级伪代码”,以帮助读者理解。示例应用程序可以通过php -S 127.0.0.1:8080 -t examples/src/
启动。
真实的PSR-15中间件可以在middlewares/psr15-middlewares存储库中找到。