olegkunitsyn / json-rpc-bundle
Symfony的JSON-RPC 2.0实现
7.0.1
2024-03-04 14:25 UTC
Requires
- php: ^8.2
- ext-json: *
- symfony/framework-bundle: ^7.0
- symfony/options-resolver: ^7.0
- symfony/property-access: 7.*
- symfony/serializer: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan-symfony: ^1.0
- phpunit/phpunit: ^9.6
- symfony/browser-kit: ^7.0
- symfony/css-selector: ^7.0
- symfony/phpunit-bridge: ^7.0
README
Symfony的JSON-RPC 2.0实现。
安装
请确保已全局安装Composer,如Composer文档中的安装章节所述。
使用Symfony Flex的应用程序
打开命令行,进入您的项目目录并执行
composer require olegkunitsyn/json-rpc-bundle
未使用Symfony Flex的应用程序
步骤1:下载Bundle
打开命令行,进入您的项目目录并执行以下命令以下载此Bundle的最新稳定版本
composer require olegkunitsyn/json-rpc-bundle
步骤2:启用Bundle
然后,通过将其添加到项目config/bundles.php文件中注册的Bundle列表中来启用Bundle
# config/bundles.php return [ // ... OlegKunitsyn\JsonRpcBundle::class => ['all' => true], ];
启动开发服务器
php -S localhost:8000 -t public
配置
导入主路由文件或创建自定义路由
# config/routes.yaml rpc: path: /api controller: json_rpc_bundle.api.rpc methods: POST
使用方法
标记您要公开的服务并发送到RPC端点的json-rpc有效载荷。方法参数必须遵循{serviceKey}.{method}约定
# config/services.yaml services: App\RpcServices\: resource: '../src/RpcServices/' tags: - 'json_rpc_bundle'
namespace App\RpcServices; use App\Dto\EchoDto; use OlegKunitsyn\JsonRpcBundle\Service\AbstractRpcService; class MyService extends AbstractRpcService { public static function getServiceKey(): string { return 'myService'; } public function echo(EchoDto $dto) : EchoDto { return $dto; } }
namespace App\Dto; readonly class EchoDto { public function __construct(public string $value) { } }
按名称(对象)
请求
curl -X POST localhost:8000/api -d '{"method": "myService.echo", "params": {"value": "hello"}, "id": "1", "jsonrpc": "2.0"}' -H 'Content-Type: application/json'
响应
{"jsonrpc":"2.0","result":{"value":"hello"},"id":"1"}
按位置(数组)
请求
curl -X POST localhost:8000/api -d '{"method": "myService.echo", "params": ["hello"], "id": "second", "jsonrpc": "2.0"}' -H 'Content-Type: application/json'
响应
{"jsonrpc":"2.0","result":{"value":"hello"},"id":"second"}
批量
请求
curl -X POST localhost:8000/api -d '[{"method": "myService.echo", "params": {"value": "hello"}, "id": "1", "jsonrpc": "2.0"}, {"method": "myService.echo", "params": ["hello"], "id": "second", "jsonrpc": "2.0"}]' -H 'Content-Type: application/json'
响应
[{"jsonrpc":"2.0","result":{"value":"hello"},"id":"1"},{"jsonrpc":"2.0","result":{"value":"hello"},"id":"second"}]
响应规范化
响应由Symfony规范化器处理。使用RpcNormalizationContext属性来指定规范化上下文
namespace App\RpcServices; use App\Dto\DateTimeDto; use OlegKunitsyn\JsonRpcBundle\Response\RpcNormalizationContext; use OlegKunitsyn\JsonRpcBundle\Service\AbstractRpcService; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; class MyService extends AbstractRpcService { public static function getServiceKey(): string { return 'myService'; } #[RpcNormalizationContext([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])] public function echo(DateTimeDto $timestamp): DateTimeDto { return $timestamp; } }
namespace App\Dto; readonly class DateTimeDto { public function __construct(public readonly \DateTimeImmutable $timestamp) { } }
请求
curl -X POST localhost:8000/api -d '{"method": "myService.echo", "params": {"timestamp": "2012-04-23T18:25:43.511Z"}, "id": "1", "jsonrpc": "2.0"}' -H 'Content-Type: application/json'
响应
{"jsonrpc":"2.0","result":{"timestamp":"2012-04-23"},"id":"1"}
路线图
- 性能
- 安全
- 代码覆盖率
致谢
- Gabriel Soullier,NanofelisJsonRpcBundle