Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mergin changes from kba-team/json-rpc #18

Merged
merged 24 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d126acc
require phpunit 9
gregor-j Mar 20, 2024
8395f46
replace legacy PHPUnit_Framework_TestCase class with TestCase
gregor-j Mar 20, 2024
1802274
replace legacy array() syntax with brackets
gregor-j Mar 20, 2024
03b8c1a
adapt function setUp() to parent return value and visibility
gregor-j Mar 20, 2024
c89fb21
add phpunit.result.cache to git ignore list
gregor-j Mar 20, 2024
966e197
replace setExpectedException with expectException
gregor-j Mar 20, 2024
5d5fcc2
replace deprecated $this->at() calls with static::exactly() call pred…
gregor-j Mar 20, 2024
1a3b0cf
Merge pull request #4 from the-kbA-team/1-move-to-phpunit9
gregor-j Mar 20, 2024
8244632
add minimal github workflow
gregor-j Mar 20, 2024
21c5477
delete .travis.yml
gregor-j Mar 20, 2024
4989b2d
Merge pull request #5 from the-kbA-team/2-replace-travis-with-github-…
gregor-j Mar 20, 2024
e789fc4
require squizlabs/php_codesniffer in dev
gregor-j Mar 20, 2024
abee773
add PHPCS config file
gregor-j Mar 20, 2024
51eaa52
apply PSR12 coding standard
gregor-j Mar 20, 2024
90a8ad1
add PHP CodeSniffer test to github workflow jobs
gregor-j Mar 20, 2024
0679356
Merge pull request #7 from the-kbA-team/6-apply-psr12-coding-standard
gregor-j Mar 20, 2024
b4cd9a9
add missing composer install to phpcs job
gregor-j Mar 20, 2024
e7a6a47
git export ignore .phpcs.xml
gregor-j Mar 20, 2024
201eaee
update composer name and homepage
gregor-j Mar 21, 2024
cf0c62e
update readme
gregor-j Mar 21, 2024
adafa63
Merge pull request #10 from the-kbA-team/9-switch-composer-namespace-…
gregor-j Mar 21, 2024
ebd01b2
README updated
matasarei Jun 8, 2024
a9038be
Updated composer config
matasarei Jun 8, 2024
3d33fd3
CI workflow badge
matasarei Jun 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/ export-ignore
vendor/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
composer.* export-ignore
phpunit.xml export-ignore
README.* export-ignore
tests/ export-ignore
vendor/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.phpcs.xml export-ignore
composer.* export-ignore
phpunit.xml export-ignore
README.* export-ignore
51 changes: 51 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: "CI workflow"
on:
# Triggers the workflow on push or pull request events
push:
pull_request:

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
permissions:
contents: "read"
jobs:
# Composer config validation
composer:
name: Composer config validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Validate composer.json
run: composer validate --strict
# PHP syntax validation
php:
name: PHP syntax validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check PHP syntax
run: php -l src/ tests/
# PHP coding standard validation
phpcs:
name: PHP coding standard
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: 8.2
- name: Check PHP coding standard
run: vendor/bin/phpcs
# run PHP unit tests
phpunit:
name: PHPUnit tests
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [7.4, 8.0, 8.1, 8.2]
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php_version }}
- run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
vendor/
composer.lock
/.phpunit.result.cache
21 changes: 21 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="PSR12-src">
<description>PSR12 programming standard</description>
<!-- display progress -->
<arg value="p"/>
<arg value="n"/>
<arg value="s"/>
<!-- directories and files to check -->
<file>./src</file>
<file>./tests</file>
<!-- Use PSR12 as a standard -->
<rule ref="PSR12">
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
<exclude-pattern>src/*</exclude-pattern>
</rule>
<rule ref="PSR12">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

50 changes: 19 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
JsonRPC PHP Client and Server
JSON-RPC PHP Client and Server
=============================

A simple Json-RPC client/server that just works.
![CI workflow](https://github.com/matasarei/json-rpc/actions/workflows/main.yml/badge.svg)

Complete fork of `fguillot/JsonRPC` with latest changes.
A simple JSON-RPC client/server that just works.

Features
--------
Expand All @@ -13,7 +13,6 @@ Features
- Authentication and IP based client restrictions
- Custom Middleware
- Fully unit tested
- Requirements: PHP >= 5.4
- License: MIT

Author
Expand All @@ -23,26 +22,15 @@ Frédéric Guillot

Installation with Composer
--------------------------

For PHP >= 5.4
```bash
composer require fguillot/json-rpc
```

For PHP 5.3 (last supported version):
```bash
composer require fguillot/json-rpc 1.2.5
```

Running Test Cases
--------
1. Clone the repo
2. `cd JsonRPC`
3. Run composer: `composer install`
4. Run tests: `vendor/bin/phpunit`

Examples
--------
### Symfony
* https://github.com/matasarei/json-rpc-demo


### Server

Expand Down Expand Up @@ -73,11 +61,11 @@ Callback binding from array:

use JsonRPC\Server;

$callbacks = array(
$callbacks = [
'getA' => function() { return 'A'; },
'getB' => function() { return 'B'; },
'getC' => function() { return 'C'; }
);
];

$server = new Server();
$server->getProcedureHandler()->withCallbackArray($callbacks);
Expand Down Expand Up @@ -148,12 +136,12 @@ class MathApi
}
}

$callbacks = array(
'addition' => array( 'MathApi', addition ),
'subtraction' => array( 'MathApi', subtraction ),
'multiplication' => array( 'MathApi', multiplication ),
'division' => array( 'MathApi', division )
);
$callbacks = [
'addition' => [ 'MathApi', addition ],
'subtraction' => [ 'MathApi', subtraction ],
'multiplication' => [ 'MathApi', multiplication ,
'division' => [ 'MathApi', division ],
];

$server = new Server();
$server->getProcedureHandler()->withClassAndMethodArray($callbacks);
Expand Down Expand Up @@ -295,7 +283,7 @@ You can configure the log destination in your `php.ini`.

Output example:

```json
```
==> Request:
{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -327,7 +315,7 @@ $server = new Server;
// IP client restrictions
$server->allowHosts(['192.168.0.1', '127.0.0.1']);

[...]
...

// Return the response to the client
echo $server->execute();
Expand All @@ -349,7 +337,7 @@ $server = new Server;
// List of users to allow
$server->authentication(['user1' => 'password1', 'user2' => 'password2']);

[...]
...

// Return the response to the client
echo $server->execute();
Expand Down Expand Up @@ -404,7 +392,7 @@ $server
->withLocalException('MyException2')
;

[...]
...

echo $server->execute();
```
Expand All @@ -420,7 +408,7 @@ Example:

$client = new Client();
$client->getHttpClient()->withBeforeRequestCallback(function(HttpClient $client, $payload) {
$client->withHeaders(array('Content-Length: '.strlen($payload)));
$client->withHeaders(['Content-Length: '.strlen($payload)]);
});

$client->myProcedure(123);
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fguillot/json-rpc",
"description": "Simple Json-RPC client/server library that just works",
"description": "Simple JSON-RPC client/server library that just works",
"homepage": "https://github.com/matasarei/JsonRPC",
"type": "library",
"license": "MIT",
Expand All @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=7.4",
"ext-json": "*"
},
"autoload": {
Expand All @@ -19,6 +19,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.9"
}
}
2 changes: 1 addition & 1 deletion src/JsonRPC/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function send()
{
$this->isBatch = false;

return $this->sendPayload('['.implode(', ', $this->batch).']');
return $this->sendPayload('[' . implode(', ', $this->batch) . ']');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public function handleExceptions(array $headers, $isJsonResponse = false)
return;
}

$errors = array_filter($headers, function($value) {
$errors = array_filter($headers, function ($value) {
return preg_match('/HTTP.*[4-5]\d{2}\s\w/', $value);
});

Expand Down
6 changes: 3 additions & 3 deletions src/JsonRPC/ProcedureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function executeCallback(Closure $callback, $params)
*/
public function executeMethod($class, $method, $params)
{
$instance = is_string($class) ? new $class : $class;
$instance = is_string($class) ? new $class() : $class;
$reflection = new ReflectionMethod($class, $method);

$this->executeBeforeMethod($instance, $method);
Expand Down Expand Up @@ -295,13 +295,13 @@ public function getNamedArguments(array $requestParams, array $methodParams)
} elseif ($p->isDefaultValueAvailable()) {
$params[$name] = $p->getDefaultValue();
} else {
throw new InvalidArgumentException('Missing argument: '.$name);
throw new InvalidArgumentException('Missing argument: ' . $name);
}
}

if ($undefinedRequestParams = array_diff_key($requestParams, $params)) {
throw new InvalidArgumentException(
'Undefined arguments: '.implode(', ', array_keys($undefinedRequestParams))
'Undefined arguments: ' . implode(', ', array_keys($undefinedRequestParams))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Request/BatchRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function parse()
}

$responses = array_filter($responses);
return empty($responses) ? '' : '['.implode(',', $responses).']';
return empty($responses) ? '' : '[' . implode(',', $responses) . ']';
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/JsonRPC/Request/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function withLocalException($exception)
} else {
$this->localExceptions[] = $exception;
}

return $this;
}

Expand Down Expand Up @@ -126,7 +126,6 @@ public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler)
public function parse()
{
try {

JsonFormatValidator::validate($this->payload);
RpcFormatValidator::validate($this->payload);

Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Response/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function sendHeaders()
}

foreach ($this->headers as $name => $value) {
header($name.': '.$value);
header($name . ': ' . $value);
}

return $this;
Expand Down
8 changes: 4 additions & 4 deletions src/JsonRPC/Response/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ private function handleExceptions()
{
switch ($this->payload['error']['code']) {
case -32700:
throw new InvalidJsonFormatException('Parse error: '.$this->payload['error']['message']);
throw new InvalidJsonFormatException('Parse error: ' . $this->payload['error']['message']);
case -32600:
throw new InvalidJsonRpcFormatException('Invalid Request: '.$this->payload['error']['message']);
throw new InvalidJsonRpcFormatException('Invalid Request: ' . $this->payload['error']['message']);
case -32601:
throw new BadFunctionCallException('Procedure not found: '.$this->payload['error']['message']);
throw new BadFunctionCallException('Procedure not found: ' . $this->payload['error']['message']);
case -32602:
throw new InvalidArgumentException('Invalid arguments: '.$this->payload['error']['message']);
throw new InvalidArgumentException('Invalid arguments: ' . $this->payload['error']['message']);
default:
throw new ResponseException(
$this->payload['error']['message'],
Expand Down
3 changes: 1 addition & 2 deletions src/JsonRPC/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function __construct(
public function setAuthenticationHeader($header)
{
if (! empty($header)) {
$header = 'HTTP_'.str_replace('-', '_', strtoupper($header));
$header = 'HTTP_' . str_replace('-', '_', strtoupper($header));
$value = $this->getServerVariable($header);

if (! empty($value)) {
Expand Down Expand Up @@ -309,7 +309,6 @@ public function execute()
;

$response = $this->parseRequest();

} catch (Exception $e) {
$response = $this->handleExceptions($e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonRPC/Validator/HostValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function validate(array $hosts, $remoteAddress)
throw new AccessDeniedException('Access Forbidden');
}
}

/**
* Validate remoteAddress match host
*
Expand Down
1 change: 0 additions & 1 deletion src/JsonRPC/Validator/JsonFormatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ public static function validate($payload)
}
}
}

Loading
Loading