pine3ree/pine3ree-invokable-request-handler 问题修复 & 功能扩展

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

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

pine3ree/pine3ree-invokable-request-handler

Composer 安装命令:

composer require pine3ree/pine3ree-invokable-request-handler

包简介

Autowired invokable server-request handlers

README 文档

README

Continuous Integration

This package provides a psr server-request handler that can be invoked directly using method-injection for dependencies.

Descendant classes or classes using the InvokableRequestHandlerTrait must implement the __invoke method, whose dependencies are resolved either using the request attributes or by the container, via the composed params-resolver. A type-hinted ServerRequestInterface dependency is resolved to the current request.

The default implementation requires the return type to be a psr-response object.

In scenarios where you might need to return other types, you will also need to override the default psr handle method and build a response object using the return value of the protected method invokeHandler(ServerRequestInterface) which is responsible for executing the handler itself after resolving its arguments.

How it works

$handler->handle($request) calls protected method $this->invokeHandler($request)

$this->invokeHandler($request) resolve __invoke $args and calls $this(...$args)

Examples

Signature example:

namespace App\Handler;

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use pine3ree\Http\Server\InvokableRequestHandler;
// ...more use(s)

/**
 * Signature example for a generic invokable request-handler
 */
class MyRequestHandler extends InvokableRequestHandler // implements RequestHandlerInterface
{
    public function __invoke(
        // ServerRequestInterface $request, // this is optional...but at the end... it is a request-handler.
        // Type-hinted dependencies and optional named parameters matching request attributes names.
    ): ResponseInterface {
        // do something with dependency and request and return a psr-response
    }
}

Basic example:

namespace App\Controller\Shop\Product;

use App\Model\Entity\Product;
use App\Model\ORMInterface;
use App\Session\SessionInterface;
use App\Http\Message\Response\HtmlResponse;
use App\Http\Message\Response\NotFoundResponse;
use App\View\TemplateRendererInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use pine3ree\Http\Server\InvokableRequestHandler;

/**
 * An invokable controller for route '/shop/product/{id}'
 */
class Read extends InvokableRequestHandler implements RequestHandlerInterface
{
    public function __invoke(
        ServerRequestInterface $request,
        ORMInterface $orm,
        TemplateRendererInterface $view,
        SessionInterface $session, // stored as request attribute with the SessionInterface:class key
        ?int $id = null // Route-match parameter stored as request attribute with the 'id' key
    ): ResponseInterface {

        $id = $id ?? 0;
        if (id < 1) {
            return new NotFoundResponse();
        }

        $product = $orm->findById(Product::class, $id);

        if ($product === null) {
            return new NotFoundResponse("Product not found for id={$id}");
        }

        $session->set('last_visited_product_id', $id);

        return new HtmlResponse(
            $view->render('shop/product/read.html.php', [
                'product' => $product,
            ]);
        );
    }
}

Same example using the provided trait when a custom constructor is needed:

namespace App\Controller\Shop\Product;

use App\Model\Entity\Product;
use App\Model\ORMInterface;
use App\Session\SessionInterface;
use App\Http\Message\Response\HtmlResponse;
//..
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
//..
use pine3ree\Http\Server\InvokableRequestHandlerTrait;

class Read implements RequestHandlerInterface
{
    use InvokableRequestHandlerTrait;

    private array $options;

    public function __construct(
        ParamsResolverInterface $paramsResolver,
        array $options
    ) {
        $this->paramsResolver = $paramsResolver;
        $this->options = $options;
    }

    public function __invoke(
        ServerRequestInterface $request,
        ORMInterface $orm,
        TemplateRendererInterface $view,
        SessionInterface $session, // stored as request attribute under the SessionInterface:class key
        ?int $id = null // Route-match parameter stored as request attribute with the 'id' key
    ): ResponseInterface {

        // Do something with $id

        // Do something using $this->options

        // Return a response
    }
}

Examples of __invoke() not returning a response object:

namespace App\Http\Server;

// ...
use App\Http\Message\Response\HtmlResponse;
use App\Http\Message\Response\JsonResponse;
use App\View\TemplateRendererInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use pine3ree\Http\Server\InvokableRequestHandler;

/**
 * Example of base invokable handler that returns rendered-templates strings for
 * html-responses
 */

abstract class TemplateInvokableRequestHandler extends InvokableRequestHandler implements RequestHandlerInterface
{
    // Override the default trait implementation using the protected method `invokeHandler`
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new HtmlResponse(
            $this->invokeHandler($request);
        );
    }

    /**
     * In this example the implementation must return strings
     *
     * public function __invoke(
     *     TemplateRendererInterface $view,
     *     // Other dependencies and/or route params here
     * ): string {
     *     // build the template $vars map here
     *     return $view->render('some/template/file.html.php', $vars);
     * }
     */
}

/**
 * Example of base invokable handler that returns arrays for json-responses
 */

abstract class JsonInvokableRequestHandler extends InvokableRequestHandler implements RequestHandlerInterface
{
    // Override the default trait implementation using the protected method `invokeHandler`
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new JsonResponse(
            $this->invokeHandler($request);
        );
    }

    /**
     * In this example the implementation must return arrays
     *
     * public function __invoke(
     *     // Dependencies and/or route params here
     * ): array {
     *     // build the json-content $vars map here
     *     return $vars;
     * }
     */
}

pine3ree/pine3ree-invokable-request-handler 适用场景与选型建议

pine3ree/pine3ree-invokable-request-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pine3ree/pine3ree-invokable-request-handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-04-30