Skip to content

Commit 909dcad

Browse files
authored
v2.0.0
2 parents 1c2a4d4 + 3be16ed commit 909dcad

File tree

78 files changed

+1444
-2249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1444
-2249
lines changed

.scrutinizer.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,19 @@ build_failure_conditions:
1010
- 'issues.label("coding-style").exists' # No coding style issues allowed
1111
build:
1212
dependencies:
13-
before:
14-
- composer global require hirak/prestissimo
1513
override:
1614
- make build
1715
tests:
1816
stop_on_failure: true
1917
override:
20-
- php-scrutinizer-run
21-
-
22-
command: make codestyle
23-
analysis:
24-
file: 'build/reports/cs-data'
25-
format: 'php-cs-checkstyle'
2618
-
2719
command: make coverage
2820
idle_timeout: 1200
2921
coverage:
3022
file: 'build/coverage/clover.xml'
3123
format: 'php-clover'
24+
- php-scrutinizer-run --enable-security-analysis
25+
- make codestyle
3226
cache:
3327
directories:
3428
- ~/.composer
@@ -38,11 +32,9 @@ build:
3832
variables:
3933
CI: 'true'
4034
TEST_OUTPUT_STYLE: 'pretty'
35+
COMPOSER_OPTIONS: '--optimize-autoloader'
4136
COVERAGE_OUTPUT_STYLE: 'clover'
4237
COVERAGE_CLOVER_FILE_PATH: 'build/coverage/clover.xml'
43-
PHPCS_REPORT_STYLE: 'checkstyle'
44-
PHPCS_REPORT_FILE: 'build/reports/cs-data'
45-
COMPOSER_OPTIONS: '--optimize-autoloader'
4638
php:
4739
version: "7.1"
4840
timezone: UTC

.travis.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: php
22

33
php:
4-
- '7.0'
54
- '7.1'
65
- '7.2'
6+
- '7.3'
77

88
env:
99
global:
@@ -12,8 +12,8 @@ env:
1212
PHPCS_REPORT_STYLE: 'full'
1313
COMPOSER_OPTIONS: '--optimize-autoloader'
1414
matrix:
15-
- SDK_VERSION: '~1.2'
16-
- SDK_VERSION: '~2.0'
15+
- SYMFONY_VERSION: '~3.0'
16+
- SYMFONY_VERSION: '~4.0'
1717

1818
sudo: false
1919

@@ -22,10 +22,10 @@ matrix:
2222

2323
before_install:
2424
# remove xdebug to speed up build
25-
- phpenv config-rm xdebug.ini
25+
- phpenv config-rm xdebug.ini || true
2626

2727
install:
28-
- composer require yoanm/jsonrpc-server-sdk:$SDK_VERSION
28+
- composer require symfony/http-foundation:$SYMFONY_VERSION symfony/http-kernel:$SYMFONY_VERSION symfony/config:$SYMFONY_VERSION symfony/dependency-injection:$SYMFONY_VERSION
2929
- make build
3030
script:
3131
- make test-technical
@@ -35,3 +35,7 @@ cache:
3535
directories:
3636
- $HOME/.composer
3737
- vendor
38+
39+
branches:
40+
except:
41+
- /.*\-dev$/

Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ COVERAGE_OUTPUT_STYLE ?= html
66
BUILD_DIRECTORY ?= build
77
REPORTS_DIRECTORY ?= ${BUILD_DIRECTORY}/reports
88
COVERAGE_DIRECTORY ?= ${BUILD_DIRECTORY}/coverage
9+
BEHAT_COVERAGE_DIRECTORY ?= ${BUILD_DIRECTORY}/behat-coverage
910
COVERAGE_CLOVER_FILE_PATH ?= ${COVERAGE_DIRECTORY}/clover.xml
1011

1112
## Commands options
1213
### Composer
1314
#COMPOSER_OPTIONS=
1415
### Phpcs
1516
PHPCS_REPORT_STYLE ?= full
17+
PHPCS_DISABLE_WARNING ?= "false"
1618
#PHPCS_REPORT_FILE=
1719
#PHPCS_REPORT_FILE_OPTION=
1820

@@ -51,6 +53,12 @@ ifneq ("${PHPCS_REPORT_FILE}","")
5153
PHPCS_REPORT_FILE_OPTION ?= --report-file=${PHPCS_REPORT_FILE}
5254
endif
5355

56+
ifneq ("${PHPCS_DISABLE_WARNING}","true")
57+
PHPCS_DISABLE_WARNING_OPTION=
58+
else
59+
PHPCS_DISABLE_WARNING_OPTION=-n
60+
endif
61+
5462

5563
## Project build (install and configure)
5664
build: install configure
@@ -76,20 +84,26 @@ test-functional:
7684
./vendor/bin/behat ${BEHAT_COLOR_OPTION} ${BEHAT_OUTPUT_STYLE_OPTION} --no-snippets
7785

7886
codestyle: create-reports-directory
79-
./vendor/bin/phpcs --standard=phpcs.xml.dist ${PHPCS_COLOR_OPTION} ${PHPCS_REPORT_FILE_OPTION} --report=${PHPCS_REPORT_STYLE}
87+
./vendor/bin/phpcs ${PHPCS_DISABLE_WARNING_OPTION} --standard=phpcs.xml.dist ${PHPCS_COLOR_OPTION} ${PHPCS_REPORT_FILE_OPTION} --report=${PHPCS_REPORT_STYLE}
8088

8189
coverage: create-coverage-directory
8290
./vendor/bin/phpunit ${PHPUNIT_COLOR_OPTION} ${PHPUNIT_OUTPUT_STYLE_OPTION} ${PHPUNIT_COVERAGE_OPTION}
8391

92+
behat-coverage: create-behat-coverage-directory
93+
composer required leanphp/behat-code-coverage
94+
./vendor/bin/behat ${BEHAT_COLOR_OPTION} ${BEHAT_OUTPUT_STYLE_OPTION} --no-snippets --profile coverage
8495

8596

8697
# Internal commands
8798
create-coverage-directory:
8899
mkdir -p ${COVERAGE_DIRECTORY}
89100

101+
create-behat-coverage-directory:
102+
mkdir -p ${BEHAT_COVERAGE_DIRECTORY}
103+
90104
create-reports-directory:
91105
mkdir -p ${REPORTS_DIRECTORY}
92106

93107

94-
.PHONY: build install configure test test-technical test-functional codestyle coverage create-coverage-directory create-reports-directory
108+
.PHONY: build install configure test test-technical test-functional codestyle coverage behat-coverage create-coverage-directory create-behat-coverage-directory create-reports-directory
95109
.DEFAULT: build

README.md

Lines changed: 73 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1-
# JSON-RPC server symfony plugin
2-
[![License](https://img.shields.io/github/license/yoanm/symfony-jsonrpc-http-server.svg)](https://github.com/yoanm/symfony-jsonrpc-http-server) [![Code size](https://img.shields.io/github/languages/code-size/yoanm/symfony-jsonrpc-http-server.svg)](https://github.com/yoanm/symfony-jsonrpc-http-server) [![PHP Versions](https://img.shields.io/badge/php-7.0%20%2F%207.1%20%2F%207.2-8892BF.svg)](https://php.net/)
1+
# Symfony JSON-RPC server
2+
[![License](https://img.shields.io/github/license/yoanm/symfony-jsonrpc-http-server.svg)](https://github.com/yoanm/symfony-jsonrpc-http-server) [![Code size](https://img.shields.io/github/languages/code-size/yoanm/symfony-jsonrpc-http-server.svg)](https://github.com/yoanm/symfony-jsonrpc-http-server) [![Dependencies](https://img.shields.io/librariesio/github/yoanm/symfony-jsonrpc-http-server.svg)](https://libraries.io/packagist/yoanm%2Fsymfony-jsonrpc-http-server)
33

4-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/badges/build.png?b=master)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/build-status/master) [![Code Coverage](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/?branch=master)
4+
[![Scrutinizer Build Status](https://img.shields.io/scrutinizer/build/g/yoanm/symfony-jsonrpc-http-server.svg?label=Scrutinizer&logo=scrutinizer)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/build-status/master) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/yoanm/symfony-jsonrpc-http-server/master.svg?logo=scrutinizer)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/?branch=master) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/yoanm/symfony-jsonrpc-http-server/master.svg?logo=scrutinizer)](https://scrutinizer-ci.com/g/yoanm/symfony-jsonrpc-http-server/?branch=master)
55

6-
[![Travis Build Status](https://img.shields.io/travis/yoanm/symfony-jsonrpc-http-server/master.svg?label=travis)](https://travis-ci.org/yoanm/symfony-jsonrpc-http-server) [![Travis PHP versions](https://img.shields.io/travis/php-v/yoanm/symfony-jsonrpc-http-server.svg)](https://travis-ci.org/yoanm/symfony-jsonrpc-http-server)
6+
[![Travis Build Status](https://img.shields.io/travis/yoanm/symfony-jsonrpc-http-server/master.svg?label=Travis&logo=travis)](https://travis-ci.org/yoanm/symfony-jsonrpc-http-server) [![Travis PHP versions](https://img.shields.io/travis/php-v/yoanm/symfony-jsonrpc-http-server.svg?logo=travis)](https://php.net/) [![Travis Symfony Versions](https://img.shields.io/badge/Symfony-v3%20%2F%20v4-8892BF.svg?logo=travis)](https://symfony.com/)
77

88
[![Latest Stable Version](https://img.shields.io/packagist/v/yoanm/symfony-jsonrpc-http-server.svg)](https://packagist.org/packages/yoanm/symfony-jsonrpc-http-server) [![Packagist PHP version](https://img.shields.io/packagist/php-v/yoanm/symfony-jsonrpc-http-server.svg)](https://packagist.org/packages/yoanm/symfony-jsonrpc-http-server)
99

10-
Symfony JSON-RPC HTTP Server to convert an HTTP json-rpc request into HTTP json-rpc response
10+
Symfony JSON-RPC HTTP Server to convert an HTTP json-rpc request into HTTP json-rpc response.
11+
12+
Symfony bundle for [`yoanm/jsonrpc-server-sdk`](https://raw.githubusercontent.com/yoanm/php-jsonrpc-server-sdk)
13+
14+
See [yoanm/symfony-jsonrpc-params-validator](https://github.com/yoanm/symfony-jsonrpc-params-validator) for params validation.
15+
16+
See [yoanm/symfony-jsonrpc-http-server-doc](https://github.com/yoanm/symfony-jsonrpc-http-server-doc) for documentation generation.
1117

1218
## How to use
1319

14-
You can either use this library as a simple extension or like any symfony bundle.
20+
Once configured, your project is ready to handle HTTP `POST` request on `/json-rpc` endpoint.
1521

16-
*[Behat demo app configuration folders](./features/demo_app/) can be used as examples.*
22+
See below how to configure it.
1723

18-
### With Symfony bundle
24+
## Configuration
25+
26+
Bundle requires only one thing :
27+
- JSON-RPC Methods which are compatible with [`yoanm/jsonrpc-server-sdk`](https://raw.githubusercontent.com/yoanm/php-jsonrpc-server-sdk)
28+
29+
It comes with [built-in method resolver](./src/Resolver/MethodResolver.php) which use a [service locator](https://symfony.com/doc/3.4/service_container/service_subscribers_locators.html#defining-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).
30+
31+
*[Behat demo app configuration folders](./features/demo_app/) can be used as examples.*
1932

2033
- Add the bundles in your `config/bundles.php` file:
2134
```php
2235
// config/bundles.php
2336
return [
2437
...
38+
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
2539
Yoanm\SymfonyJsonRpcHttpServer\JsonRpcHttpServerBundle::class => ['all' => true],
2640
...
2741
];
@@ -37,138 +51,69 @@ You can either use this library as a simple extension or like any symfony bundle
3751
- Add the following in your configuration :
3852
```yaml
3953
# config/config.yaml
40-
json_rpc_http_server: ~
41-
```
54+
framework:
55+
secret: '%env(APP_SECRET)%'
4256

43-
### With Symfony extension only
44-
- Load the extension in your kernel :
45-
```php
46-
// src/Kernel.php
47-
...
48-
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
49-
use Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension;
50-
...
51-
class Kernel
52-
{
53-
use MicroKernelTrait;
54-
....
55-
/**
56-
* {@inheritdoc}
57-
*/
58-
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
59-
{
60-
/**** Add and load extension **/
61-
$container->registerExtension($extension = new JsonRpcHttpServerExtension());
62-
// If you use Symfony Config component, add "json_rpc_http_server: ~" in your configuration.
63-
// Else load it there
64-
$container->loadFromExtension($extension->getAlias());
65-
66-
...
67-
}
68-
....
69-
}
70-
```
71-
72-
- Map your your JSON-RPC methods, see **JSON-RPC Method mapping** section below
73-
- Manually configure an endpoint, see **Routing** section below
74-
75-
## JSON-RPC Method mapping
76-
You have many ways to inject you json-rpc methods :
77-
- If you use the bundle, you can do it by configuration :
78-
```yaml
79-
# config/config.yaml
80-
json_rpc_http_server:
81-
methods_mapping:
82-
method-a: '@method-a.service-id'
83-
method-b:
84-
service: '@method-b.service-id'
85-
aliases: 'method-b-alias'
86-
method-c:
87-
service: '@method-c.service-id'
88-
aliases: ['method-c-alias-1', 'method-c-alias-2']
89-
```
90-
- You can use tag in the service definition as below :
91-
```yaml
92-
services:
93-
method-a.service-id:
94-
class: Method\A\Class
95-
public: true # <= do no forget the set visibility to public !
96-
tags:
97-
- { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a' }
98-
- { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a-alias' }
99-
```
100-
- Inject manually your mapping during container building
101-
```php
102-
// src/Kernel.php
103-
...
104-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
105-
use Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension;
106-
...
107-
class Kernel implements CompilerPassInterface
108-
{
109-
....
110-
/**
111-
* {@inheritdoc}
112-
*/
113-
public function process(ContainerBuilder $container)
114-
{
115-
$container->getDefinition(JsonRpcHttpServerExtension::SERVICE_NAME_RESOLVER_SERVICE_NAME)
116-
->addMethodCall('addMethodMapping', ['method-a', 'method-a.service-id'])
117-
->addMethodCall('addMethodMapping', ['method-b', 'method-b.service-id'])
118-
->addMethodCall('addMethodMapping', ['method-b-alias', 'method-b.service-id'])
119-
;
120-
}
121-
....
122-
}
123-
```
124-
- Or inject manually your mapping after container building
125-
```php
126-
$container->get(JsonRpcHttpServerExtension::SERVICE_NAME_RESOLVER_SERVICE_NAME)
127-
->addMethodMapping('method-a', 'method-a.service-id')
128-
->addMethodMapping('method-b', 'method-b.service-id')
129-
->addMethodMapping('method-b-alias', 'method-b.service-id')
130-
;
131-
```
132-
133-
## Routing
134-
- If you use the bundle, the default endpoint is `/json-rcp`. You can custome it by using :
135-
```yaml
136-
# config/config.yaml
137-
json_rpc_http_server:
138-
http_endpoint_path: '/my-custom-endpoint'
139-
```
140-
141-
- Or you can define your own route and bind the endpoint as below :
142-
```yaml
143-
# config/routes.yaml
144-
index:
145-
path: /my-json-rpc-endpoint
146-
defaults: { _controller: 'json_rpc_http_server.endpoint:index' }
57+
json_rpc_http_server: ~
58+
# Or the following in case you want to customize endpoint path
59+
#json_rpc_http_server:
60+
# endpoint: '/my-custom-endpoint' # Default to '/json-rpc'
14761
```
148-
149-
## Custom method resolver
15062

151-
By default this bundle use [`yoanm/jsonrpc-server-sdk-psr11-resolver`](https://github.com/yoanm/php-jsonrpc-server-sdk-psr11-resolver).
63+
### JSON-RPC Method mapping
64+
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 :
65+
```yaml
66+
services:
67+
method-a.service-id:
68+
class: Method\A\Class
69+
tags:
70+
- { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a' }
71+
- { name: 'json_rpc_http_server.jsonrpc_method', method: 'method-a-alias' }
72+
```
15273
153-
In case you want to use your own, you can do it by using :
74+
### Methods mapping aware
75+
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`.
76+
77+
```php
78+
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface;
79+
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
80+
81+
class MappingCollector implements JsonRpcMethodAwareInterface
82+
{
83+
/** @var JsonRpcMethodInterface[] */
84+
private $mappingList = [];
85+
86+
public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method): void
87+
{
88+
$this->mappingList[$methodName] = $method;
89+
}
90+
91+
/**
92+
* @return JsonRpcMethodInterface[]
93+
*/
94+
public function getMappingList() : array
95+
{
96+
return $this->mappingList;
97+
}
98+
}
99+
```
154100

155-
### Service definition tag
156-
Use `json_rpc_http_server.method_resolver` tag as following:
101+
```yaml
102+
mapping_aware_service:
103+
class: App\Collector\MappingCollector
104+
tags: ['json_rpc_http_server.method_aware']
105+
```
106+
107+
### Custom method resolver
108+
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:
157109
```yaml
158110
services:
159111
my.custom_method_resolver.service:
160112
class: Custom\Method\Resolver\Class
161113
tags: ['json_rpc_http_server.method_resolver']
162114
```
163115

164-
### Bundle configuration
165-
Configure the bundle as below
166-
```yaml
167-
# config/config.yaml
168-
json_rpc_http_server:
169-
method_resolver: '@my.custom_method_resolver.service'
170-
```
171-
 
116+
You can take advantage of method mapping aware mechanism or write your custom resolution logic.
172117

173118
## Contributing
174119
See [contributing note](./CONTRIBUTING.md)

0 commit comments

Comments
 (0)