binsoul/net-http-dispatcher
Composer 安装命令:
composer require binsoul/net-http-dispatcher
包简介
Dispatcher using named parameters to invoke callables or methods of objects
关键字:
README 文档
README
This package allows to map arbitrary names to dispatchable objects using named parameters provided in a context array. Dispatchable objects are then invoked to return a response. They can be surrounded by middleware to alter the incoming request or the outgoing response.
Install
Via composer:
$ composer require binsoul/net-http-dispatcher
Responder
A responder can be a closure, an object with an __invoke method or an object with multiple methods. It can receive the request object, the context object or any value from the context as parameters and it has to return a response.
The dispatcher resolves parameters via reflection. If a parameter cannot be resolved and is not optional an exception is thrown.
For example the responder "blog.edit" will receive the following parameters:
$dispatcher->addResponder( 'blog.edit', function ($id, RequestInterface $request, Context $context, $optional = 'foo') { // $id = 1 // $request = object // $context = object // $optional = 'foo' return new Response(...); } ); $dispatcher->handle($request, ['responder' => 'blog.edit', 'id' => 1]);
Middleware
Middleware can be a closure or an object with an __invoke method. It has to accept a request, a context and the next middleware as parameters and it has to return a response.
For example middleware may or may not perform the various optional processes:
$middleware = function (RequestInterface $request, Context $context, callable $next) { // optionally return a response early if (...) { return new Response(...); } // optionally modify the request $request = $request->withUri(..); // optionally modify the context $context = $context->withParameter(...); // invoke the $next middleware $response = $next($request, $context); // optionally modify the response $response = $response->withStatus(...); // always return a response return $response; };
Dispatcher
A dispatcher uses the information provided by a context array to find a responder. The default keys are "responder" to indicate which responder should be invoked and "method" to indicate which method of the responder should be called.
The used array keys can be configured. The following example maps the reponder to the array key "controller" and the method to "action":
class BlogController { public function index() { return new Response(...); } } $dispatcher->addResponder('blog', new BlogController()); $dispatcher->defineParameters('controller', 'action'); // will invoke BlogController->index() $dispatcher->handle($request, ['controller' => 'blog', 'action' => 'index']);
Responders can be registered either as closures or concrete objects or as strings. If a responder is registered as a string the provided factory is used to lazily build responders.
$dispatcher->addResponder('blog', 'BlogResponder'); $dispatcher->setFactory($factory); // will call $factory->buildResponder('BlogResponder'); $dispatcher->handle($request, ['responder' => 'blog']);
The same applies to registering middlewares.
$dispatcher->addMiddleware('AuthMiddleware') $dispatcher->setFactory($factory); // will call $factory->buildMiddleware('AuthMiddleware'); $dispatcher->handle($request, ['responder' => 'blog']);
Middleware is added to an internal queue which surrounds the final call to the responder.
For example if the queue is build like:
$dispatcher->addResponder('blog', 'BlogResponder'); $dispatcher->addMiddleware('Foo'); $dispatcher->addMiddleware('Bar'); $dispatcher->addMiddleware('Baz'); $dispatcher->handle($request, ['controller' => 'blog']);
The request and response path through the middlewares will look like this:
Foo is 1st on the way in
Bar is 2nd on the way in
Baz is 3rd on the way in
BlogResponder is invoked
Baz is 1st on the way out
Bar is 2nd on the way out
Foo is 3rd on the way out
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.
binsoul/net-http-dispatcher 适用场景与选型建议
binsoul/net-http-dispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「network」 「http」 「dispatcher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 binsoul/net-http-dispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 binsoul/net-http-dispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 binsoul/net-http-dispatcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Package that provides an integration with Doctrine ORM to automatically dispatch Domain Events.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
Event dispatcher package
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
PHP SDK for Bands In Town API
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-24