Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Илья Лесняк committed Aug 29, 2022
0 parents commit d1ce08d
Show file tree
Hide file tree
Showing 42 changed files with 5,779 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 8
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PHP_XDEBUG_CLIENT_HOST=host.docker.internal
PHP_XDEBUG_CLIENT_PORT=9003
PHP_XDEBUG_MODE=off
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/docker export-ignore
/.* export-ignore
/src/Test export-ignore
/src/**/*Test.php export-ignore
/src/TestHandlerResolver.php export-ignore
/composer.lock export-ignore
/Dockerfile export-ignore
/Makefile export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.coverage
/vendor
/.php-cs-fixer.php
/phpstan.neon
/phpunit.xml
/.env
24 changes: 24 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

return (new Config())
->setFinder(
Finder::create()
->in(__DIR__ . '/src')
)
->setRules([
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false, 'always_move_variable' => true],
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'php_unit_method_casing' => false,
'php_unit_test_annotation' => ['style' => 'annotation'],
])
->setRiskyAllowed(true)
->setCacheFile('/tmp/php-cs-fixer.cache');
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM php:8-cli-alpine as php
FROM mlocati/php-extension-installer as php-ext-installer
FROM composer:2 as composer

FROM php as runtime
ARG GID
ARG UID
COPY --from=composer /usr/bin/composer /usr/local/bin/
COPY --from=php-ext-installer /usr/bin/install-php-extensions /usr/local/bin/ipe
COPY ./docker/php/*.ini /usr/local/etc/php/conf.d/
RUN addgroup -S -g $GID host && adduser -S -D -G host -u $UID host && \
apk add --no-cache --update make && \
cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini && \
ipe xdebug-3.1.5
WORKDIR /opt/project
VOLUME ["/opt/project"]
EXPOSE 8080
USER host:host
14 changes: 14 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright 2022 Ilya Lesnyak <me@ilsenem.ru>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY: build
build:
docker build . \
-t json-rpc/server:dev \
--build-arg GID=$$(id -u) \
--build-arg UID=$$(id -u)
cp .env.dist .env
.PHONY: sh
sh:
docker run --rm -it --env-file=.env -v $(PWD):/opt/project --entrypoint=sh json-rpc/server:dev
.PHONY: test
test:
./vendor/bin/phpunit
.PHONY: coverage
test-coverage:
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html=./.coverage
.PHONY: analyse
analyse:
./vendor/bin/phpstan
.PHONY: format
format:
./vendor/bin/php-cs-fixer fix
.PHONY: preview-format
format-preview:
./vendor/bin/php-cs-fixer fix --dry-run --diff -vvv
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# JSON-RPC Server

Library provides JSON-RPC server following
[2.0](https://www.jsonrpc.org/specification) specs.

## Not stable

Version `0.x` is unstable and API is subject to change.

## Requirements

* PHP >=8.1

## Installation

Use [composer](https://getcomposer.org) to install library:

```shell
composer require json-rpc/server
```

## Usage

Implement `HandlerResolver` interface to provide server with method
handlers. Create server and pass handler resolver. After that you
can call methods with `respond` server method. Both request and response
must be JSON strings. If server have no respond an empty string will be
returned.

```php
<?php

/**
* Handler resolver
*/
final class CustomHandlerResponse implements \JsonRpc\HandlerResolver
{
/**
* @param array<string,\JsonRpc\RequestHandler|\JsonRpc\NotificationHandler> $handlers
*/
public function __construct(private array $handlers = [])
{}

public function resolve(string $method) : \JsonRpc\RequestHandler|\JsonRpc\NotificationHandler{
if (!array_key_exists($method, $this->handlers)) {
throw new \JsonRpc\MethodHandlerNotFound($method);
}

return $this->handlers[$method];
}
}

/**
* "sum" method handler
*/
final class SumMethodHandler implements \JsonRpc\RequestHandler {
public function handle(\JsonRpc\Request\Request $request): int
{
return $request->params[0] + $request->params[1];
}
}

$request = <<<JSON
{
"jsonrpc": "2.0",
"method": "sum",
"params": [1,2],
"id": 1
}
JSON;

$resolver = CustomHandlerResolver([
'sum' => new SumMethodHandler(),
]);
$server = new Server($resolver);
$response = $server->respond($request);

/**
* {
* "jsonrpc": "2.0",
* "result": 3,
* "id": 1
* }
*/
```

## Development

1. Build development container:

```shell
make buid
```
2. Make changes to `.env` file if needed.
3. `sh` inside container:

```shell
make sh
```

From inside the container you can use various `make` recipes:

* `make test` — run PHPUnit tests.
* `make test-coverage` — generate Code Coverage report to `./coverage`
directory.
* `make analyse` — run PHPStan static code analysis.
* `make format` — run PHP-CS-Fixer code formatter.
* `make format-preview` — preview PHP-CS-Fixer output before format.

## License

[MIT](./LICENSE)
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "json-rpc/server",
"description": "JSON-RPC server",
"license": "MIT",
"type": "library",
"version": "0.1.0",
"authors": [
{
"name": "Ilya Lesnyak",
"email": "me@ilsenem.ru"
}
],
"require": {
"php": "^8.1"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.28.3",
"friendsofphp/php-cs-fixer": "^3.10",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.8.2",
"phpstan/phpstan-phpunit": "^1.1.1",
"phpunit/phpunit": "^9.5.23"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"JsonRpc\\": "src/"
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"platform": {
"php": "8.1"
},
"sort-packages": true
}
}
Loading

0 comments on commit d1ce08d

Please sign in to comment.