2017 © Pedro Peláez
 

symfony-bundle rpc-server-bundle

RPC server symfony bundle

image

bankiru/rpc-server-bundle

RPC server symfony bundle

  • Tuesday, August 1, 2017
  • by scaytrase
  • Repository
  • 3 Watchers
  • 1 Stars
  • 8,192 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 6 Versions
  • 7 % Grown

The README.md

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight, (*2)

HTTP RPC Server bundle

This bundle provides default controller realisation to handle RPC requests which come to the application via HTTP requests, (*3)

Implementations

RPC server does not declares any implementation requirements. Some could be, (*4)

  • JSON-RPC
  • SOAP (extends XML-RPC)

or other custom RPC which operates with method+parameters and utilizes single endpoint for several methods, (*5)

HTTP Endpoints

Endpoint is a HTTP route which process basic HTTP request, providing initial parsing and processing request data, (*6)

To enable HTTP endpoint you should enable custom endpoint router loader via the following router configuration:, (*7)

# app/config/routing.yml
rpc:
  resource: .
  type: endpoint

Resource value is not important, it is ignored when loading routes as the endpoints are configured vua config, (*8)

Configuration

Basic endpoint configuration looks like, (*9)

rpc:
  router:
    endpoints:
      my-public-endpoint:
        path: /
        defaults:
          _controller: JsonRpcBundle:JsonRpc:jsonRpc
          _format: json
        context: Default
        resources:
        - "@MyBundle/Resources/config/service_rpc.yml"

This creates endpoint on URL / with generic symfony controller. Also it pre-populates the methods from the service_rpc.yml config file, (*10)

my-public-endpoint will become a route name, so make sure it does not overlap with other routes until you want it do to this., (*11)

Method routing

Each RPC request has method and parameters. You can configure the application to handle different methods within different endpoints with different actions, (*12)

Generic configuration looks like, (*13)

my_bundle:
  resource: "@MyBundle/Rpc/"
  prefix: my_bundle/
  type: annotation

Different resource types are supported. Built-in are, (*14)

Annotation

@Method("my-bundle/my-method", context={"private"}, defaultContext=false) 

Yaml

Different endpoint implementation may utilize different controller name parsers, so MyBundle:Test:entity notation is endpoint-dependent. I.e JSON-RPC may search TestController controller in MyBundle\JsonRpc\TestController, (*15)

my-bundle/my-method:
  controller: "MyBundle:Test:entity"
  default_context: true
  context: private

Resource

You can pass directory, class, file, yaml config as method source with prefix and context inheritance, (*16)

The following chaing will result in prefix/annotation/sub method handled by AnnotationController::subAction with private+default context, (*17)

private:
  resource: jsonrpc_private_nested.yml
  context: private
annotation:
  resource: "@JsonRpcTestBundle/JsonRpc"
  prefix: prefix/
/**
 * Class AnnotationController
 *
 * @package Bankiru\Api\JsonRpc\Test\JsonRpc
 * @Method("annotation/")
 */
class AnnotationController extends Controller
{
    /**
     * @return array
     * @Method("sub")
     */
    public function subAction()
    {
        return [];
    }
}

Events

This bundle repeats the generic symfony request processing flow. You can hook your extension into given system events, (*18)

  • rpc.request is triggered on handling RPC call
    • Routing happens here
  • rpc.controller is triggered to filter controller (i.e. allows security filtering)
  • rpc.response is triggered whenever response is acquired by the endpoint processor
  • rpc.view is triggered if response, returned from controller is not instance of RpcResponseInterface
  • rpc.exception is triggered when exception is raised during RPC call processing
  • rpc.finish_request is used to finalize RPC response before it is returned to HTTP controller

RPC Controller implementation

The goal of implementing controller is to extend abstract RpcController passing RequestInterface and endpoint name into getResponse method., (*19)

RequestInterface is an extension of RpcRequestInterface with extra attributes allowing request metadata carrying alongside the request., (*20)

The generic solution is to convert incoming symfony Request instance into your own implementation of RequestInterface (i.e extract method and args from XML-RPC or JSON-RPC requests) and send serialized response back, transforming RpcResponseInterface back to your response object., (*21)

You can also automatically convert RpcResponseInterface into your serialized response via generic symfony view event processing, (*22)

The Versions