filipefernandes007/symfony-json-rpc

This package is abandoned and no longer maintained. No replacement package was suggested.

Symfony Bundle to convert an HTTP json-rpc request into HTTP json-rpc response

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 Server to convert an HTTP json-rpc request into HTTP json-rpc response.

Symfony bundle for yoanm/jsonrpc-server-sdk

See yoanm/symfony-jsonrpc-params-validator for params validation.

See yoanm/symfony-jsonrpc-http-server-doc for documentation generation.

How to use

Once configured, your project is ready to handle HTTP POST request on /json-rpc endpoint.

See below how to configure it.

Configuration

Bundle requires only one thing :

It comes with built-in method resolver which use a service locator. Using a service locator allow to load (and so instanciate dependencies, dependencies of dependencies, etc) method only when required (usually only one method is required by request, except for batch requests which will load one or more methods).

Behat demo app configuration folders can be used as examples.

  • Add the bundles in your config/bundles.php file:

    // config/bundles.php
    return [
        ...
        Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        Yoanm\SymfonyJsonRpcHttpServer\JsonRpcHttpServerBundle::class => ['all' => true],
        ...
    ];
  • Add the following in your routing configuration :

    # config/routes.yaml
    json-rpc-endpoint:
      resource: '@JsonRpcHttpServerBundle/Resources/config/routing/endpoint.xml'
  • Add the following in your configuration :

    # 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 Method mapping

In order to inject yours JSON-RPC method into the server add the tag json_rpc_http_server.jsonrpc_method and the key/value method like following example :

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' }

Methods mapping aware

In case you want to be aware of which methods are registered inside the JSON-RPC server, you can use the json_rpc_http_server.method_aware. Your class must implements 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']

Custom method resolver

In case you want to use your method resolver implementation, use the tag json_rpc_http_server.method_resolver, it will be automatically injected inside JSON-RPC server:

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

You can take advantage of method mapping aware mechanism or write your custom resolution logic.

Contributing

See contributing note