承接 tourze/json-rpc-core 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tourze/json-rpc-core

Composer 安装命令:

composer require tourze/json-rpc-core

包简介

JSON-RPC 2.0 Core

README 文档

README

PHP Version License: MIT Build Status Code Coverage

English | 中文

This library provides core components compliant with the JSON-RPC 2.0 specification for building JSON-RPC servers and clients in PHP.

Table of Contents

Features

  • Fully compliant with JSON-RPC 2.0 specification
  • Supports all JSON-RPC request types: single, batch, and notification
  • Robust error handling for all standard JSON-RPC error types
  • Flexible method resolution interfaces
  • Clean object-oriented API

Installation

composer require tourze/json-rpc-core

Requirements:

  • PHP 8.1+
  • nesbot/carbon ^2.72 || ^3
  • psr/log ^1|^2|^3
  • symfony/dependency-injection ^7.3
  • symfony/event-dispatcher-contracts ^3
  • symfony/http-foundation ^7.3
  • symfony/property-access ^7.3
  • symfony/service-contracts ^3.6
  • symfony/validator ^7.3
  • tourze/arrayable 0.0.*
  • tourze/backtrace-helper 0.1.*

Components

Models

  • JsonRpcRequest - Represents a JSON-RPC request
  • JsonRpcResponse - Represents a JSON-RPC response
  • JsonRpcParams - Wraps request parameters
  • JsonRpcCallRequest - Handles batch requests
  • JsonRpcCallResponse - Handles batch responses

Domain Interfaces

  • JsonRpcMethodInterface - Defines the interface for JSON-RPC methods
  • JsonRpcMethodResolverInterface - Resolves method names to implementations
  • JsonRpcMethodParamsValidatorInterface - Validates method parameters
  • MethodWithValidatedParamsInterface - Methods with parameter validation
  • MethodWithResultDocInterface - Methods with result documentation

Exceptions

  • JsonRpcException - Base JSON-RPC exception
  • JsonRpcParseErrorException - Parse error exception
  • JsonRpcInvalidRequestException - Invalid request exception
  • JsonRpcMethodNotFoundException - Method not found exception
  • JsonRpcInvalidParamsException - Invalid params exception
  • JsonRpcInternalErrorException - Internal error exception

Usage Example

Creating a JSON-RPC Method

use Tourze\JsonRPC\Core\Domain\JsonRpcMethodInterface;
use Tourze\JsonRPC\Core\Model\JsonRpcRequest;

class EchoMethod implements JsonRpcMethodInterface
{
    public function __invoke(JsonRpcRequest $request): mixed
    {
        return $request->getParams()->all();
    }

    public function execute(): array
    {
        return [];
    }
}

处理 JSON-RPC 请求

use Tourze\JsonRPC\Core\Exception\JsonRpcException;
use Tourze\JsonRPC\Core\Model\JsonRpcParams;
use Tourze\JsonRPC\Core\Model\JsonRpcRequest;
use Tourze\JsonRPC\Core\Model\JsonRpcResponse;

// 创建请求
$request = new JsonRpcRequest();
$request->setJsonrpc('2.0');
$request->setMethod('echo');
$request->setId('1');
$request->setParams(new JsonRpcParams(['message' => 'Hello, World!']));

// 创建方法实例
$method = new EchoMethod();

// 执行方法并获取响应
$response = new JsonRpcResponse();
$response->setJsonrpc('2.0');
$response->setId($request->getId());

try {
    $result = $method($request);
    $response->setResult($result);
} catch (JsonRpcException $e) {
    $response->setError($e);
} catch (\Throwable $e) {
    // 将普通异常包装为 JSON-RPC 异常
    $jsonRpcException = new JsonRpcException(-32000, $e->getMessage());
    $response->setError($jsonRpcException);
}

批量请求

通过 JsonRpcCallRequestJsonRpcCallResponse 处理批量请求。详见单元测试中的示例。

Advanced Usage

Custom Parameter Validation

Use the BaseProcedure class for automatic parameter validation:

use Tourze\JsonRPC\Core\Procedure\BaseProcedure;
use Tourze\JsonRPC\Core\Attribute\MethodParam;
use Symfony\Component\Validator\Constraints as Assert;

class CalculatorMethod extends BaseProcedure
{
    #[MethodParam('First number')]
    #[Assert\Type('numeric')]
    #[Assert\NotBlank]
    public int $a;

    #[MethodParam('Second number')]
    #[Assert\Type('numeric')]
    #[Assert\NotBlank]
    public int $b;

    public function execute(): int
    {
        return $this->a + $this->b;
    }
}

Event-Driven Architecture

Listen to JSON-RPC events for logging and monitoring:

use Tourze\JsonRPC\Core\Event\BeforeMethodApplyEvent;
use Tourze\JsonRPC\Core\Event\AfterMethodApplyEvent;

// Before method execution
$dispatcher->addListener(BeforeMethodApplyEvent::class, function ($event) {
    $logger->info('Method called', [
        'method' => $event->getName(),
        'params' => $event->getParams()->all()
    ]);
});

// After method execution
$dispatcher->addListener(AfterMethodApplyEvent::class, function ($event) {
    $logger->info('Method completed', [
        'method' => $event->getName(),
        'result' => $event->getResult()
    ]);
});

Helper Classes for Complex Validation

The package includes helper classes for advanced parameter processing:

  • TypeValidatorFactory: Creates type validators from reflection
  • PropertyConstraintExtractor: Extracts validation constraints from properties
  • ParameterProcessor: Handles parameter assignment and validation

Testing

vendor/bin/phpunit packages/json-rpc-core/tests

License

This project is licensed under the MIT License - see the LICENSE file for details.

tourze/json-rpc-core 适用场景与选型建议

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

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

围绕 tourze/json-rpc-core 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-12