定制 yiisoft/data-response 二次开发

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

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

yiisoft/data-response

Composer 安装命令:

composer require yiisoft/data-response

包简介

Allows responding with data that is automatically converted into PSR-7 response

README 文档

README

Yii

Yii Data Response


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge Static analysis type-coverage

The package allows responding with data that is automatically converted into PSR-7 response.

Requirements

  • PHP 8.1 - 8.5.
  • DOM PHP extension.

Installation

The package could be installed with Composer:

composer require yiisoft/data-response

General usage

Response Factories

The package provides response factories that create PSR-7 responses with DataStream body. The data is formatted lazily when the response body is read.

use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory;
use Yiisoft\DataResponse\Formatter\JsonFormatter;

/**
 * @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
 */

$factory = new JsonResponseFactory($responseFactory, new JsonFormatter());
$response = $factory->createResponse(['key' => 'value']);

$response->getBody()->rewind();
echo $response->getBody()->getContents(); // {"key":"value"}
echo $response->getHeaderLine('Content-Type'); // application/json; charset=UTF-8

The following response factories are available:

  • JsonResponseFactory — creates responses with JSON-formatted body;
  • XmlResponseFactory — creates responses with XML-formatted body;
  • HtmlResponseFactory — creates responses with HTML-formatted body;
  • PlainTextResponseFactory — creates responses with plain text body;
  • DataResponseFactory — creates responses without a predefined formatter, use middleware to format.

Middleware

The package provides PSR-15 middleware that formats DataStream responses without a predefined formatter.

use Yiisoft\DataResponse\Middleware\JsonDataResponseMiddleware;
use Yiisoft\DataResponse\Formatter\JsonFormatter;

$middleware = new JsonDataResponseMiddleware(new JsonFormatter());

The following middleware are available:

  • HtmlDataResponseMiddleware
  • JsonDataResponseMiddleware
  • XmlDataResponseMiddleware
  • PlainTextDataResponseMiddleware

Content Negotiation

The package provides content negotiation via middleware and response factory.

Middleware

ContentNegotiatorDataResponseMiddleware selects a formatter based on the request's Accept header:

use Yiisoft\DataResponse\Formatter\HtmlFormatter;
use Yiisoft\DataResponse\Formatter\XmlFormatter;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
use Yiisoft\DataResponse\Middleware\ContentNegotiatorDataResponseMiddleware;

$middleware = new ContentNegotiatorDataResponseMiddleware(
    formatters: [
        'text/html' => new HtmlFormatter(),
        'application/xml' => new XmlFormatter(),
        'application/json' => new JsonFormatter(),
    ],
    fallback: new JsonFormatter(),
);

The fallback parameter also accepts a RequestHandlerInterface, for example NotAcceptableRequestHandler to return a 406 response when no formatter matches.

Response Factory

ContentNegotiatorResponseFactory selects a response factory based on the request's Accept header:

use Yiisoft\DataResponse\ResponseFactory\ContentNegotiatorResponseFactory;
use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory;
use Yiisoft\DataResponse\ResponseFactory\XmlResponseFactory;

/**
 * @var JsonResponseFactory $jsonResponseFactory
 * @var XmlResponseFactory $xmlResponseFactory
 */

$factory = new ContentNegotiatorResponseFactory(
    factories: [
        'application/json' => $jsonResponseFactory,
        'application/xml' => $xmlResponseFactory,
    ],
    fallback: $jsonResponseFactory,
);

$response = $factory->createResponse($request, ['key' => 'value']);

The fallback parameter also accepts a RequestHandlerInterface, for example NotAcceptableRequestHandler to return a 406 response when no factory matches.

DataStream

DataStream is a PSR-7 stream that lazily formats data. It wraps raw data and a formatter, and performs formatting only when the stream is read. A formatter is optional at construction time, but must be set before reading the stream, otherwise a LogicException will be thrown.

use Yiisoft\DataResponse\DataStream\DataStream;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
use Yiisoft\DataResponse\Formatter\XmlFormatter;

$stream = new DataStream(['key' => 'value'], new JsonFormatter());

echo (string) $stream; // {"key":"value"}

// You can change the data or formatter dynamically
$stream->changeData(['new' => 'data']);
$stream->changeFormatter(new XmlFormatter());

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Data Response is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

yiisoft/data-response 适用场景与选型建议

yiisoft/data-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 313.78k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2020 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yiisoft/data-response 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 313.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 18
  • 依赖项目数: 38
  • 推荐数: 2

GitHub 信息

  • Stars: 19
  • Watchers: 14
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-07-15