tourze/json-rpc-container-bundle

JsonRPC容器模块

Installs: 2 355

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/json-rpc-container-bundle

0.0.4 2025-06-01 06:06 UTC

This package is auto-updated.

Last update: 2025-10-31 07:41:50 UTC


README

English | 中文

Latest Version PHP Version License Build Status Code Coverage

A Symfony Bundle for registering and managing JSON-RPC methods. This bundle provides container integration for tourze/json-rpc-core, enabling JSON-RPC methods to be registered and resolved through the Symfony container.

Features

  • Manage JSON-RPC methods using Symfony container
  • Automatic registration of JSON-RPC methods through tags
  • Method resolver implementation for automatic JSON-RPC method resolution
  • Support for method remapping via environment variables

Installation

Install via Composer:

composer require tourze/json-rpc-container-bundle

Requirements

  • PHP >= 8.1
  • Symfony >= 7.3
  • tourze/json-rpc-core

Configuration

Register the bundle in your Symfony application:

// config/bundles.php
return [
    // ...
    Tourze\JsonRPCContainerBundle\JsonRPCContainerBundle::class => ['all' => true],
];

Usage

Creating JSON-RPC Methods

First, create a class that implements JsonRpcMethodInterface:

<?php

namespace App\JsonRpc;

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

class ExampleMethod implements JsonRpcMethodInterface
{
    public function __invoke(JsonRpcRequest $request): mixed
    {
        // Handle JSON-RPC request
        return ['success' => true, 'data' => 'example_result'];
    }
    
    public function execute(): array
    {
        // Interface compatibility implementation
        return ['success' => true, 'data' => 'example_result'];
    }
}

Registering JSON-RPC Methods

Register methods using the json_rpc_http_server.jsonrpc_method tag:

Via Service Configuration

# config/services.yaml
services:
    app.jsonrpc.example_method:
        class: App\JsonRpc\ExampleMethod
        tags:
            - { name: 'json_rpc_http_server.jsonrpc_method', method: 'example.method' }

Via Attributes

<?php

namespace App\JsonRpc;

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

#[MethodExpose('example.method')]
class ExampleMethod implements JsonRpcMethodInterface
{
    // ...
}

Resolving JSON-RPC Methods

Get an instance of JsonRpcMethodResolverInterface through the service container:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Tourze\JsonRPC\Core\Domain\JsonRpcMethodResolverInterface;

class ApiController extends AbstractController
{
    public function handleRequest(JsonRpcMethodResolverInterface $methodResolver): Response
    {
        // Resolve specific method
        $method = $methodResolver->resolve('example.method');
        
        // Get all registered method names
        $allMethods = $methodResolver->getAllMethodNames();
        
        // ...
    }
}

Method Remapping

You can remap method names through environment variables:

# .env
JSON_RPC_METHOD_REMAP_original.method=remapped.method

This will redirect requests for original.method to remapped.method.

Testing

Run unit tests:

./vendor/bin/phpunit packages/json-rpc-container-bundle/tests

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.