定制 pew-pew/http-factory 二次开发

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

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

pew-pew/http-factory

Composer 安装命令:

composer require pew-pew/http-factory

包简介

HTTP factory for decoding request and encoding responses with symfony integration

README 文档

README

PHP 8.3+ Latest Stable Version Latest Unstable Version License MIT

HTTP Factory

A set of drivers for encoding HTTP responses and decoding HTTP requests.

Installation

PewPew HTTP Factory is available as Composer repository and can be installed using the following command in a root of your project:

$ composer require pew-pew/http-factory

More detailed installation instructions are here.

Usage

Decoder

// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();

// Requests Factory
$requests = new \PewPew\HttpFactory\RequestDecoderFactory([
    new \PewPew\HttpFactory\Driver\JsonDriver(),
]);

$payload = $requests
    ->createDecoder($request) // Detect passed "content-type" header and
                              // create decoder if available.
    ?->decode($request->getContent(true));   // Decode request body.

Encoder

// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();

// Responses Factory
$responses = new \PewPew\HttpFactory\ResponseEncoderFactory([
    new \PewPew\HttpFactory\Driver\JsonDriver(),
]);

$response = $responses
    ->createEncoder($request) // Detect passed "accept" header and create
                              // encoder if available.
    ?->encode(['some' => 'any'], 200);  // Encode payload and create response.

Symfony Integration

Add the bundle to your bundles.php file:

// bundles.php
return [
    // ...
    PewPew\HttpFactory\HttpFactoryBundle::class => ['all' => true],
];

Use ResponseEncoderFactoryInterface and RequestDecoderFactoryInterface in your services:

use PewPew\HttpFactory\ResponseEncoderFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

final readonly class ExampleController
{
    public function __construct(
        private ResponseEncoderFactoryInterface $responses,
    ) {}
    
    public function someAction(Request $request): Response 
    {
        $encoder = $this->responses->createEncoder($request);
        
        if ($encoder === null) {
            throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException(
                'Unsupported "accept" request header',
            );
        }
        
        return $encoder->encode([
            'status' => 'ok'
        ], Response::HTTP_OK);
    }
}

统计信息

  • 总下载量: 3.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固