tourze/json-rpc-container-bundle
最新稳定版本:2.0.0
Composer 安装命令:
composer require tourze/json-rpc-container-bundle
包简介
JsonRPC容器模块
README 文档
README
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.
统计信息
- 总下载量: 2.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-27