定制 woohoolabs/yin-middlewares 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

woohoolabs/yin-middlewares

最新稳定版本:4.2.1

Composer 安装命令:

composer require woohoolabs/yin-middlewares

包简介

Woohoo Labs. Yin Middleware

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads Gitter

Woohoo Labs. Yin Middleware is a collection of middleware which helps you to integrate Woohoo Labs. Yin into your PHP applications.

Table of Contents

Introduction

Features

  • 100% PSR-15 compatibility
  • 100% PSR-7 compatibility
  • Validation of requests against the JSON schema
  • Validation of responses against the JSON and JSON:API schema
  • Dispatching of JSON:API-aware controllers
  • JSON:API exception handling

Install

The only thing you need before getting started is Composer.

Install a PSR-7 implementation:

Because Yin Middleware requires a PSR-7 implementation (a package which provides the psr/http-message-implementation virtual package), you must install one first. You may use Zend Diactoros or any other library of your preference:

$ composer require zendframework/zend-diactoros

Install Yin Middleware:

To install the latest version of this library, run the command below:

$ composer require woohoolabs/yin-middleware

Note: The tests and examples won't be downloaded by default. You have to use composer require woohoolabs/yin-middleware --prefer-source or clone the repository if you need them.

Yin Middleware 4.1 requires PHP 7.4 at least, but you may use Yin Middleware 4.0.0 for PHP 7.1.

Install the optional dependencies:

If you want to use JsonApiRequestValidatorMiddleware and JsonApiResponseValidatorMiddleware from the default middleware stack then you have to require the following dependencies too:

$ composer require seld/jsonlint
$ composer require justinrainbow/json-schema

Basic Usage

Supported middleware interface design

The interface design of Yin-Middleware is based on the PSR-15 de-facto standard. That's why it is compatible with Woohoo Labs. Harmony, Zend-Stratigility, Zend-Expressive and many other frameworks.

The following sections will guide you through how to use and configure the provided middleware.

Note: When passing a ServerRequestInterface instance to your middleware dispatcher, a WoohooLabs\Yin\JsonApi\Request\JsonApiRequestInterface instance must be used in fact (the WoohooLabs\Yin\JsonApi\Request\JsonApiRequest class possibly), otherwise the JsonApiDispatcherMiddleware and the JsonApiExceptionHandlerMiddleware will throw an exception.

YinCompatibilityMiddleware

This middleware facilitates the usage of Yin and Yin-Middleware in other frameworks. It does so by upgrading a basic PSR-7 request object to JsonApiRequest, which is suitable for working with Yin. Please keep in mind, that this middleware should precede any other middleware that uses JsonApiRequest as $request parameter.

$harmony->addMiddleware(new YinCompatibilityMiddleware());

Available configuration options for the middleware (they can be passed to the constructor):

JsonApiRequestValidatorMiddleware

The middleware is mainly useful in a development environment, and it is able to validate a PSR-7 request against the JSON and the JSON:API schema. Just add it to your application (the example is for Woohoo Labs. Harmony):

$harmony->addMiddleware(new JsonApiRequestValidatorMiddleware());

If validation fails, an exception containing the appropriate JSON:API errors will be thrown. If you want to customize the error messages or the response, provide an Exception Factory of your own. For other customizations, feel free to extend the class.

Available configuration options for the middleware (they can be passed to the constructor):

  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • includeOriginalMessageInResponse: If true, the original request body will be included in the "meta" top-level member
  • negotiate: If true, the middleware performs content-negotiation as specified by the JSON:API spec. In this case, the "Content-Type" and the "Accept" header is checked.
  • validateQueryParams: If true, query parameters are validated against the JSON:API specification
  • validateJsonBody: If true, the request body gets validated against the JSON schema

JsonApiResponseValidatorMiddleware

The middleware is mainly useful in a development environment, and it is able to validate a PSR-7 response against the JSON and the JSON:API schema. Just add it to your application (the example is for Woohoo Labs. Harmony):

$harmony->addMiddleware(new JsonApiResponseValidatorMiddleware());

If validation fails, an exception containing the appropriate JSON:API errors will be thrown. If you want to customize the error messages or the response, provide an Exception Factory of your own. For other customizations, feel free to extend the class.

Available configuration options for the middleware (they can be passed to the constructor):

  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • serializer: The SerializerInterface instance to be used
  • includeOriginalMessageInResponse: If true, the original response will be included in the "meta" top-level member
  • validateJsonBody: If true, the response body gets validated against the JSON schema
  • validateJsonApiBody: If true, the response is validated against the JSON:API schema

JsonApiDispatcherMiddleware

This middleware is able to dispatch JSON:API-aware controllers. Just add it to your application (the example is for Woohoo Labs. Harmony):

$harmony->addMiddleware(new JsonApiDispatcherMiddleware());

This middleware works exactly as the one in Woohoo Labs. Harmony, the only difference is that it dispatches controller actions with the following signature:

public function myAction(JsonApi $jsonApi): ResponseInterface;

instead of:

public function myAction(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface;

The difference is subtle, as the $jsonApi object contains a PSR-7 compatible request, and PSR-7 responses can also be created with it. Learn more from the documentation of Woohoo Labs. Yin.

Available configuration options for the middleware (they can be passed to the constructor):

  • container: A PSR-11 compliant container instance to be used to instantiate the controller
  • exceptionFactory: The ExceptionFactoryInterface instance to be used (e.g.: when dispatching fails)
  • serializer: The SerializerInterface instance to be used
  • handlerAttribute: The name of the request attribute which stores a dispatchable controller (it is usually provided by a router).

JsonApiExceptionHandlerMiddleware

It catches exceptions and responds with an appropriate JSON:API error response.

Available configuration options for the middleware (they can be passed to the constructor):

  • errorResponsePrototype: In case of an error, this response object will be manipulated and returned
  • catching: If false, the middleware won't catch JsonApiExceptions
  • verbose: If true, additional meta information will be provided about the exception thrown
  • exceptionFactory: The ExceptionFactoryInterface instance to be used
  • serializer: The SerializerInterface instance to be used

Versioning

This library follows SemVer v2.0.0.

Change Log

Please see CHANGELOG for more information what has changed recently.

Testing

Woohoo Labs. Yin Middleware has a PHPUnit test suite. To run the tests, run the following command from the project folder after you have copied phpunit.xml.dist to phpunit.xml:

$ phpunit

Additionally, you may run docker-compose up or make test in order to execute the tests.

Contributing

Please see CONTRIBUTING for details.

Support

Please see SUPPORT for details.

Credits

License

The MIT License (MIT). Please see the License File for more information.

woohoolabs/yin-middlewares 适用场景与选型建议

woohoolabs/yin-middlewares 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142 次下载、GitHub Stars 达 14, 最近一次更新时间为 2015 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「middleware」 「Woohoo Labs.」 「json api」 「psr-7」 「Yin」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 woohoolabs/yin-middlewares 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 woohoolabs/yin-middlewares 我们能提供哪些服务?
定制开发 / 二次开发

基于 woohoolabs/yin-middlewares 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 142
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-16