timiki / rpc-server-bundle
JSON-RPC server bundle for symfony
Installs: 67 389
Dependents: 3
Suggesters: 1
Security: 0
Stars: 19
Watchers: 4
Forks: 5
Open Issues: 2
Type:symfony-bundle
Requires
- php: >=8.1
- ext-json: *
- symfony/console: ^6.0 | ^7.0
- symfony/dependency-injection: ^6.0 | ^7.0
- symfony/event-dispatcher: ^6.0 | ^7.0
- symfony/framework-bundle: ^6.0 | ^7.0
- symfony/http-foundation: ^6.0 | ^7.0
- symfony/http-kernel: ^6.0 | ^7.0
- symfony/property-access: ^6.0 | ^7.0
- symfony/serializer: ^6.0 | ^7.0
- symfony/stopwatch: ^6.0 | ^7.0
- timiki/rpc-common: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.8
- phpstan/phpstan-symfony: ^1.2
- phpunit/phpunit: ^9.0
- symfony/browser-kit: ^6.0 | ^7.0
- symfony/cache: ^6.0 | ^7.0
- symfony/maker-bundle: ^1.13
- symfony/phpunit-bridge: ^6.0 | ^7.0
- symfony/security-bundle: ^6.0 | ^7.0
- symfony/translation: ^6.0 | ^7.0
- symfony/validator: ^6.0 | ^7.0
- symfony/yaml: ^6.0 | ^7.0
Suggests
- symfony/cache: For add cache for method
- symfony/security-bundle: For add check security for method
- symfony/validator: For add validate params for method
- dev-master
- 6.4.0
- 6.2.0
- 6.1.1
- 6.1.0
- 6.0.2
- 6.0.1
- 6.0.0
- 5.1.0
- 5.0.0
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.10
- 4.1.9
- 4.1.8
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.x-dev
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.1.0
- dev-4.2-dev
- dev-4.1-dev
- dev-6.1.0-dev
- dev-issues-14
This package is auto-updated.
Last update: 2024-12-17 08:43:23 UTC
README
JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol (and very similar to XML-RPC) , defining only a handful of data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
Install
Symfony >= 6.4 || 7.0
composer require timiki/rpc-server-bundle "^6.4"
Symfony >= 6.0
composer require timiki/rpc-server-bundle "^6.0"
Deprecated versions
Symfony >= 5.0 use version ^5.0
composer require timiki/rpc-server-bundle "^5.0"
Symfony >= 4.3 use version ^4.1
composer require timiki/rpc-server-bundle "^4.1"
Symfony < 4.3 use version ^4.0
composer require timiki/rpc-server-bundle "^4.0"
Configs
rpc_server: # Mapping configs # Default mapping # mapping: '%kernel.project_dir%/src/Method' # # Multi dir mapping # mapping: # - '%kernel.project_dir%/src/Method1' # - '%kernel.project_dir%/src/Method2' # # Multi handler|dir mapping # mapping: # v1: # - '%kernel.project_dir%/src/Method/V1' # v2: # - '%kernel.project_dir%/src/Method/V2' # mapping: '%kernel.project_dir%/src/Method' # Cache pool name cache: null # Serializer service, must be instanced of Timiki\Bundle\RpcServerBundle\Serializer\SerializerInterface # By default use Timiki\Bundle\RpcServerBundle\Serializer\BaseSerializer serializer: null parameters: # Allow extra params in JSON request allow_extra_params: false
Add methods dir to exclude from autowire
App\: resource: '../src/*' exclude: '../src/{Method}'
Controller
Default:
You can use you own controller for JSON-RPC request. For example:
<?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; class RpcController extends AbstractController { public function indexAction(Request $request) { return $this->get('rpc.server.http_handler.default')->handleHttpRequest($request); } }
or add default JSON-RPC route (default POST to /rpc) to you routing.yml
rpc: path: /rpc defaults: { _controller: Timiki\Bundle\RpcServerBundle\Controller\RpcController::handlerAction } methods: [POST]
or controller for different handler (version)
rpc: path: /{version} defaults: { _controller: Timiki\Bundle\RpcServerBundle\Controller\RpcController::handlerAction, version: v1 } methods: [POST]
If web site and JSON-RPC server located on a different domain remember about CORS.
Method
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; #[RPC\Execute] public function execute() { $param = $this->param; ... return $result; } }
Or you can also use __invoke to declare a call method
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; public function __invoke() { $param = $this->param; ... return $result; } }
Inject method execute Context
<?php declare(strict_types=1); namespace App\Method; use Timiki\Bundle\RpcServerBundle\Attribute as RPC; use Timiki\Bundle\RpcServerBundle\Method\Context; use Symfony\Component\Validator\Constraints as Assert; #[RPC\Method("name")] #[RPC\Roles(["ROLE_NAME"])] #[RPC\Cache(3600)] class Method { #[RPC\Param] #[Assert\NotBlank] protected $param; public function __invoke(Context $context) { $param = $this->param; ... return $result; } }
Attributes
Method
Define class as JSON-RPC method.
#[Method("name")]
Roles
Set roles for access to method. If user not granted for access server return error with message "Method not granted" and code "-32001".
#[Roles(["ROLE_NAME", "ROLE_OTHER"])]
Cache
If define cache in configs it set response lifetime.
#[Cache(3600)]
Param
Define JSON-RPC params. Use Symfony\Component\Validator\Constraints for validate it.
#[Param] protected $param; #[Param] protected $param = null'; // Default value for param
Execute
Define execute function in class.
#[Execute] public function execute() { // Code }
or use __invoke
public function __invoke() { // Code }
Serialize
For convert output result from method to json in bundle used serializer
Config for serializer:
rpc: serializer: rpc.server.serializer.base # serialize service id
In bundle include next serializers:
rpc.server.serializer.base - (default) use Symfony serialize to convert result to json rpc.server.serializer.role - use user roles as @Group (@see https://symfony.com/doc/current/components/serializer.html) for control access to output array
Create custom serializer
Here is an example of a simple class for serialization.
<?php declare(strict_types=1); namespace App\Serializer; use Timiki\Bundle\RpcServerBundle\Serializer\SerializerInterface; class MySerializer implements SerializerInterface { public function serialize(mixed $data): string // You serialize logic } public function toArray(mixed $data): array // You serialize logic } }
And then add custom serializer service id to config
rpc: serializer: MySerializer # serialize service id