kleijnweb/php-api-middleware
最新稳定版本:v1.0.0-alpha1
Composer 安装命令:
composer require kleijnweb/php-api-middleware
包简介
Middleware for kleijnweb/php-api-descriptions.
README 文档
README
Middleware for kleijnweb/php-api-descriptions.
Components
DefaultPipe
A complete chain of middleware that can process OpenAPI requests from begin to end, once you add some command handlers. The middleware in this section is executed in order listed.
You can append and prepend 3rd party middleware, ResultSerializer is appended just before dispatching.
OperationMatcher
Determines which Operation object a request maps to (routing), or returning 404/405 responses respectively. Returns an request object some API description objects as well as the path parameters added as attributes.
BodyParsing
Parses the request body. In DefaultPipe uses JsonBodyParser but the parser is injectable.
ParameterAssembler
Coerces all the request data that qualify as OpenAPI parameters (bar the body) and sets them as request attributes.
MessageValidator
Validates the incoming request using the OpenAPI document and returns a 400 response with failure messages when invalid.
ParameterHydrator
Uses kleijnweb/php-api-hydrator to hydrate custom typed objects and \DateTime.
CommandDispatcher
Dead simple command dispatcher that invokes callables with the arguments in order as found in the request.
ResponseBodyDehydrator
Picks up the result of CommandDispatcher and produces a response object.
Example
require __DIR__.'/../vendor/autoload.php'; use Doctrine\Common\Cache\ApcuCache; use KleijnWeb\PhpApi\Descriptions\Description\Repository; use KleijnWeb\PhpApi\Middleware\DefaultPipe; use KleijnWeb\PhpApi\Middleware\Tests\Functional\Pet; use Middlewares\Utils\Factory; use Zend\Diactoros\Response\SapiEmitter; use Zend\Diactoros\ServerRequestFactory; $cache = new ApcuCache(); $commands = [ '/pets/{id}:get' => function (int $id) use ($cache): Pet { return unserialize($cache->fetch($id)); }, '/pets:post' => function (Pet $pet) use ($cache) { $count = $cache->fetch('count'); $pet->setId($id = $count + 1); $cache->save($id, serialize($pet)); $cache->save('count', $id); return $pet; }, ]; $repository = new Repository(null, $cache); $repository->register(__DIR__.'/../tests/Functional/petstore.yml'); $pipe = new DefaultPipe( $repository, $commands, ['KleijnWeb\PhpApi\Middleware\Tests\Functional'] ); $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $response = $pipe->dispatch($request, function () { return Factory::createResponse(500); }); (new SapiEmitter())->emit($response);
Verify that it works properly by starting a dev server and issuing some cURL requests:
php -S localhost:1234 test.php curl -vH "Content-Type: application/json" -d '{"name":"doggie"}' http://localhost:1234/pets * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 1234 (#0) > POST /pets HTTP/1.1 > Host: localhost:1234 > User-Agent: curl/7.47.0 > Accept: */* > Content-Type: application/json > Content-Length: 17 > * upload completely sent off: 17 out of 17 bytes < HTTP/1.1 200 OK < Host: localhost:1234 < Connection: close < X-Powered-By: PHP/7.0.15-0ubuntu0.16.04.4 < Content-Type: application/json < Content-Length: 24 < * Closing connection 0 {"id":1,"name":"doggie"} $ curl -vH "Content-Type: application/json" -d '{}' http://localhost:1234/pets * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 1234 (#0) > POST /pets HTTP/1.1 > Host: localhost:1234 > User-Agent: curl/7.47.0 > Accept: */* > Content-Type: application/json > Content-Length: 2 > * upload completely sent off: 2 out of 2 bytes < HTTP/1.1 400 Bad Request < Host: localhost:1234 < Connection: close < X-Powered-By: PHP/7.0.15-0ubuntu0.16.04.4 < Content-Type: application/json < Content-Length: 55 < * Closing connection 0 {"errors":{"pet.name":"The property name is required"}} curl -v http://localhost:1234/pets/1 * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 1234 (#0) > GET /pets/1 HTTP/1.1 > Host: localhost:1234 > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 200 OK < Host: localhost:1234 < Connection: close < X-Powered-By: PHP/7.0.15-0ubuntu0.16.04.4 < Content-Type: application/json < Content-Length: 24 < * Closing connection 0 {"id":1,"name":"doggie"}
Contributing
Pull requests are very welcome, but the code has to be PSR2 compliant, follow used conventions concerning parameter and return type declarations, and the coverage can not go down.
License
KleijnWeb\PhpApi\Middleware is made available under the terms of the LGPL, version 3.0.
kleijnweb/php-api-middleware 适用场景与选型建议
kleijnweb/php-api-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.35k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「response」 「middleware」 「request」 「swagger」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kleijnweb/php-api-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kleijnweb/php-api-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kleijnweb/php-api-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
repository php library
HTTP request logger middleware for Laravel
Class to generate a standard structure for api json responses
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 6.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2017-04-08