yoanm / symfony-jsonrpc-http-server
Symfony 扩展,用于将 HTTP json-rpc 请求转换为 HTTP json-rpc 响应
Requires
- php: ^8.0
- psr/container: ^1.0
- symfony/config: ^4.4 || ^5.4 || ^6.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0
- symfony/event-dispatcher: ^4.4 || ^5.4 || ^6.0
- symfony/event-dispatcher-contracts: ^1.0 || ^2.0 || ^3.0
- symfony/http-foundation: ^4.4 || ^5.4 || ^6.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0
- yoanm/jsonrpc-server-sdk: ^3.3
Requires (Dev)
- behat/behat: ^3.9.0
- dvdoug/behat-code-coverage: ^5.0
- matthiasnoback/symfony-config-test: ^4.0
- matthiasnoback/symfony-dependency-injection-test: ^4.0
- phpspec/prophecy: ^1.15
- phpspec/prophecy-phpunit: ^2.0
- phpunit/php-code-coverage: ^9.2.4
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/framework-bundle: ^4.4 || ^5.4 || ^6.0
- symfony/routing: ^4.4 || ^5.4 || ^6.0
- yoanm/php-unit-extended: ^2.0
Suggests
- yoanm/symfony-jsonrpc-http-server-doc: JSON-RPC documentation Bundle
- yoanm/symfony-jsonrpc-params-validator: Symfony bundle for easy JSON-RPC params validation
README
Symfony JSON-RPC HTTP Server,用于将 HTTP json-rpc 请求转换为 HTTP json-rpc 响应。
Symfony 扩展,用于 yoanm/jsonrpc-server-sdk
有关参数验证,请参阅 yoanm/symfony-jsonrpc-params-validator
有关文档生成,请参阅 yoanm/symfony-jsonrpc-http-server-doc
版本
-
Symfony v3/4 - PHP >=7.1 :
^2.0
⚠️⚠️
v2.1.0
和v2.1.1
标签错误,请使用v3.0.0
代替!⚠️⚠️ -
Symfony v4/5 - PHP >=7.2 :
~3.0.0
-
Symfony v4/5 - PHP >=7.3 :
^3.1
-
Symfony v4.4/5.4 - PHP ^8.0 :
^3.2
-
Symfony v4.4/5.4/6.x - PHP ^8.0 :
^3.3
如何使用
配置完成后,您的项目即可处理在 /json-rpc
端点上的 HTTP POST
请求。
以下是如何配置它的示例。
配置
扩展只需要一件事情
- 与
yoanm/jsonrpc-server-sdk
兼容的 JSON-RPC 方法
它自带 内置方法解析器,它使用 服务定位器。使用服务定位器可以在需要时加载(以及实例化依赖项、依赖项的依赖项等)方法(通常只有请求需要一个方法,除了批处理请求,它将加载一个或多个方法)。
Behat 示例应用程序配置文件夹可以用作示例。
-
在您的
config/bundles.php
文件中添加扩展// config/bundles.php return [ ... Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Yoanm\SymfonyJsonRpcHttpServer\JsonRpcHttpServerBundle::class => ['all' => true], ... ];
-
在您的路由配置中添加以下内容
# config/routes.yaml json-rpc-endpoint: resource: '@JsonRpcHttpServerBundle/Resources/config/routing/endpoint.xml'
-
在您的配置中添加以下内容
# config/config.yaml framework: secret: '%env(APP_SECRET)%' json_rpc_http_server: ~ # Or the following in case you want to customize endpoint path #json_rpc_http_server: # endpoint: '/my-custom-endpoint' # Default to '/json-rpc'
JSON-RPC 方法映射
为了将您的 JSON-RPC 方法注入到服务器中,请添加标签 json_rpc_http_server.jsonrpc_method
和键/值 method
,如下例所示
services: method-a.service-id: class: Method\A\Class tags: - { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a' } - { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a-alias' }
方法映射感知
如果您想了解 JSON-RPC 服务器中注册了哪些方法,可以使用 json_rpc_http_server.method_aware
。您的类必须实现 JsonRpcMethodAwareInterface
。
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface; use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; class MappingCollector implements JsonRpcMethodAwareInterface { /** @var JsonRpcMethodInterface[] */ private $mappingList = []; public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method): void { $this->mappingList[$methodName] = $method; } /** * @return JsonRpcMethodInterface[] */ public function getMappingList() : array { return $this->mappingList; } }
mapping_aware_service: class: App\Collector\MappingCollector tags: ['json_rpc_http_server.method_aware']
调试模式
您可以为 JSON-RPC 服务器设置 '调试' 模式,这允许在响应 JSON 主体中显示详细错误信息。这些信息包含实际的异常类名、代码、消息和堆栈跟踪。
注意:您绝对不应该在生产环境中启用 '调试' 模式,因为它会将关键内部信息暴露给 API 消费者。
配置示例
# file 'config/packages/json_rpc.yaml' json_rpc_http_server: endpoint: '/json-rpc' debug: enabled: false max_trace_size: 10 show_trace_arguments: true simplify_trace_arguments: true when@dev: json_rpc_http_server: debug: enabled: true when@test: json_rpc_http_server: debug: enabled: true
贡献
请参阅 贡献说明