desilva/microserve 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

desilva/microserve

Composer 安装命令:

composer require desilva/microserve

包简介

Lightweight API for creating PHP application servers

README 文档

README

Minimal. Agnostic. Zero dependencies.

About

This package provides a framework for creating application servers and is intended to be used as a starting point for other packages. Think if this as a layer between the low-level PHP server implementations and your higher level application logic, allowing you to interact with the requests and responses in an abstracted object-oriented way that you may be used to. The name comes from it being based on Pikoserve.

Installation

Install the package using Composer (desilva/microserve)

composer require desilva/microserve

Usage

Please see the example project in the docs directory.

This project is also used for the server core of the HydePHP Realtime Compiler (V2.0), I recommend you take a look at the implementation there as well.

High level overview

You'll need to take care of bootstrapping yourself as each use case is different.

In general, you'll want to route all requests to a single entry point, which should be an extension of the HttpKernelInterface. This is where you would bind a router or similar to handle the requests.

General implementation

The recommended way to implement a server is to route all HTTP requests to the server.php script. This script should register the Composer autoloader, run the bootstrap.php script, then finally create a new Application instance to capture and handle the incoming HTTP request.

Here's an example of a server.php script:

require_once 'vendor/autoload.php';

$app = \Desilva\Microserve\Microserve::boot(\App\Http\MyHttpKernel::class);
$app->handle(); // Process the request to create and send the response

The boot() method will construct your Kernel, and then return an Application instance containing it.

We then call the handle() method which will inject a Request object, then call the Kernel handle method which returns a Response object which is used to send the HTTP response to the client.

HttpKernel example:

class HttpKernel implements HttpKernelInterface
{
    public function handle(Request $request): Response
    {
        return Response::make(200, 'OK', [
            'body' => 'Hello World!',
        ]);
    }
}

Note: The application will automatically send the response created by the Kernel.

Release Notes for v2.0

Breaking/Major Changes

  • Breaking: The ResponseInterface::send() method now returns static instead of void. This change affects the interface and all implementing classes.
  • Major: The Application class now automatically sends the response returned by the Kernel.

New Features

  • Headers are now buffered in the Response class instead of being sent immediately.
  • New protected sendHeaders() method added to the Response class for sending all buffered headers.
  • New HtmlResponse class added for handling HTML responses with appropriate headers.

Improvements

  • The Response::send() and JsonResponse::send() methods now return $this, allowing for method chaining and providing more flexibility when working with responses.
  • The application now automatically sends the response returned by the Kernel, removing the need to call send() manually. This fixes a common "gotcha" where users forget to call send() after creating a response.
  • Type hints now use static returns instead of self to more accurately reflect the return type of the methods.
  • More flexibility in manipulating headers throughout the response lifecycle.
  • Better alignment with common practices in modern PHP frameworks.

Upgrade Guide

If you're upgrading from v1.x to v2.0, here are the key changes you need to be aware of:

Response::send() Method Return Type

  1. The send() method in the ResponseInterface now has a return type of static. This is a breaking change as it requires all implementing classes to update their method signature.

  2. If you have any custom classes implementing ResponseInterface, you must update their send() method to return static:

    public function send(): static
    {
        // Your implementation
    
        return $this;
    }

Please review your codebase for any implementations of ResponseInterface and update them accordingly. This change is made to allow for method chaining and provide more flexibility when working with responses, and to allow for working with sent responses in a more fluent way.

Response Creation and Sending

The Application class is now responsible for sending the response after the kernel handles the request, meaning you should no longer call send() manually after creating a response.

Example of updated usage:

// In your HttpKernel or similar class
public function handle(Request $request): Response
{
    return Response::make(200, 'OK', ['body' => 'Hello World!']);
    // OR: return new Response(200, 'OK', ['body' => 'Hello World!']);
}

// In your application entry point
$app = new Application(new MyHttpKernel());
$app->handle(); // This will now send the response

Please review your codebase for any cases where you send responses manually, as it's no longer necessary to call send() after creating a response. The application will automatically send the response returned by the kernel.

Header Sending Changes

  1. The withHeaders() method now adds headers to a buffer instead of sending them immediately. If you were relying on immediate header sending, you may need to adjust your code.
  2. Headers are now sent when the send() method is called on the Response object. Make sure you're calling send() at the appropriate time in your application lifecycle.
  3. If you've extended the Response or JsonResponse classes, you may need to update your implementations to work with the new buffered header approach.
  4. Update any tests that were checking for immediate header sending. You may need to use reflection or mock the header functions to test the new buffering behavior.

New HtmlResponse Class

A new HtmlResponse class has been added to handle HTML responses. This class automatically sets the appropriate Content-Type and Content-Length headers for HTML content. If you're returning HTML responses, consider using this new class:

use Desilva\Microserve\HtmlResponse;

// In your HttpKernel or similar class
public function handle(Request $request): Response
{
    return new HtmlResponse(200, 'OK', ['body' => '<html><body>Hello World!</body></html>']);
}

Conclusion

If you encounter any issues during the upgrade process, please open an issue on the GitHub repository.

desilva/microserve 适用场景与选型建议

desilva/microserve 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 desilva/microserve 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

与 desilva/microserve 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 170.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-03