upscale/swoole-reflection 问题修复 & 功能扩展

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

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

upscale/swoole-reflection

最新稳定版本:3.2.0

Composer 安装命令:

composer require upscale/swoole-reflection

包简介

Reflection API for Swoole web-server

README 文档

README

This library provides a Reflection API for classes of Swoole / Open Swoole web-server.

Features:

  • Server middleware manipulation
  • Response lifecycle tracking

Installation

The library is to be installed via Composer as a dependency:

composer require upscale/swoole-reflection

Usage

Middleware Manipulation

Override server middleware optionally reusing the original callback:

$server = new \Swoole\Http\Server('127.0.0.1', 8080);
$server->on('request', function ($request, $response) {
   $response->end("Served by Swoole server\n");
});

$reflection = new \Upscale\Swoole\Reflection\Http\Server($server);
$middleware = $reflection->getMiddleware();
$reflection->setMiddleware(function ($request, $response) use ($middleware) {
   $response->header('Content-Type', 'text/plain');
   $middleware($request, $response);
});

$server->start();

Response Lifecycle

Headers Tracking

Modify response headers before they have been sent:

$server->on('request', function ($request, $response) {
    $callback = function () use ($request, $response) {
        $response->header('Content-Type', 'text/plain');
    };
    $response = new \Upscale\Swoole\Reflection\Http\Response\Observable($response);
    $response->onHeadersSentBefore($callback);    
    $response->end("Served by Swoole server\n");
});

Callback is invoked on every request upon the first call to \Swoole\Http\Response::write/end/sendfile().

Warning! Callbacks that need to modify response must use the original response rather than its observable proxy. Dependency on observable proxy creates circular reference between the proxy and callbacks registered within it. Instances involved in orphan circular cross-references will not be destroyed until the next garbage collection cycle. Swoole calls \Swoole\Http\Response::end() in the response destructor executed upon the request completion. The response destructor will not be called causing the the worker to hang up without sending the response.

Body Interception

Modify response body before sending it out:

$server->on('request', function ($request, $response) use ($server) {
    $response = new \Upscale\Swoole\Reflection\Http\Response\Observable($response);
    $response->onBodyAppend(function (&$content) use ($server) {
        $content .= "<!-- Served by worker {$server->worker_id} -->\n";
    });
    $response->header('Content-Type', 'text/html');
    $response->end("Served by <b>Swoole server</b>\n");
});

Callback is invoked on every call to \Swoole\Http\Response::write/end() with non-empty contents.

Contributing

Pull Requests with fixes and improvements are welcome!

License

Copyright © Upscale Software. All rights reserved.

Licensed under the Apache License, Version 2.0.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-03-17