filipefernandes007/symfony-json-rpc

此包已 废弃,不再维护。未建议替代包。

Symfony 包,用于将 HTTP json-rpc 请求转换为 HTTP json-rpc 响应

v2.0.3 2019-04-21 08:30 UTC

This package is auto-updated.

Last update: 2020-03-08 09:38:10 UTC


README

License Code size Dependabot Status

Scrutinizer Build Status Scrutinizer Code Quality Code Coverage

Travis Build Status Travis PHP versions Travis Symfony Versions

Latest Stable Version Packagist PHP version

Symfony JSON-RPC HTTP 服务器,用于将 HTTP json-rpc 请求转换为 HTTP json-rpc 响应。

Symfony 包,用于 yoanm/jsonrpc-server-sdk

有关参数验证,请参阅 yoanm/symfony-jsonrpc-params-validator

有关文档生成,请参阅 yoanm/symfony-jsonrpc-http-server-doc

如何使用

配置完成后,您的项目即可准备处理在 /json-rpc 端点上的 HTTP POST 请求。

下面是配置方法。

配置

包需要以下内容:

它包含一个内置的方法解析器,该解析器使用 服务定位器。使用服务定位器可以在需要时加载(以及因此实例化依赖项、依赖项的依赖项等)方法(通常情况下,请求只需要一个方法,除非是批处理请求,这将会加载一个或多个方法)。

您可以将 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_http_server.method_resolver,它将被自动注入到 JSON-RPC 服务器中

services:
  my.custom_method_resolver.service:
    class: Custom\Method\Resolver\Class
    tags: ['json_rpc_http_server.method_resolver']

您可以利用方法映射感知机制或编写您自己的解析逻辑。

贡献

请参阅 贡献说明