-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4114d84
commit fa2f42c
Showing
8 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidJsonException extends Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\exceptions; | ||
|
||
use Exception; | ||
|
||
class ValidationException extends Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\modules\rpc; | ||
|
||
use Sagittaracc\PhpPythonDecorator\modules\Module; | ||
|
||
class JsonRpc extends Module | ||
{ | ||
public static function parseError() | ||
{ | ||
return json_encode(['error' => 'Parse error', 'code' => -32700]); | ||
} | ||
|
||
public static function invalidRequest() | ||
{ | ||
return json_encode(['error' => 'Invalid request', 'code' => -32600]); | ||
} | ||
|
||
public static function methodNotFound() | ||
{ | ||
return json_encode(['error' => 'Method not found', 'code' => -32601]); | ||
} | ||
|
||
public static function result($object, $request) | ||
{ | ||
$id = $request->id; | ||
$method = $request->method; | ||
$params = $request->params; | ||
|
||
return json_encode([ | ||
'json-rpc' => '2.0', | ||
'id' => $id, | ||
'result' => call_user_func_array([$object, $method], $params), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\modules\rpc\core; | ||
|
||
use Exception; | ||
use Sagittaracc\PhpPythonDecorator\Decorator; | ||
use Sagittaracc\PhpPythonDecorator\exceptions\InvalidJsonException; | ||
use Sagittaracc\PhpPythonDecorator\exceptions\ValidationException; | ||
use Sagittaracc\PhpPythonDecorator\modules\validation\core\primitives\Number; | ||
use Sagittaracc\PhpPythonDecorator\modules\validation\core\primitives\Str; | ||
|
||
class Request | ||
{ | ||
use Decorator; | ||
|
||
#[Number] | ||
public $id; | ||
|
||
#[Str] | ||
public $method; | ||
|
||
public array $params; | ||
|
||
function __construct($rawRequest) | ||
{ | ||
$request = json_decode($rawRequest); | ||
|
||
if ($request === null) { | ||
throw new InvalidJsonException; | ||
} | ||
|
||
try { | ||
set_decorator_prop($this, 'id', $request->id ?? null); | ||
set_decorator_prop($this, 'method', $request->method ?? null); | ||
set_decorator_prop($this, 'params', $request->params ?? []); | ||
} catch (Exception $e) { | ||
throw new ValidationException; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\modules\rpc\core; | ||
|
||
use Attribute; | ||
use Sagittaracc\PhpPythonDecorator\exceptions\InvalidJsonException; | ||
use Sagittaracc\PhpPythonDecorator\exceptions\ValidationException; | ||
use Sagittaracc\PhpPythonDecorator\modules\rpc\JsonRpc; | ||
use Sagittaracc\PhpPythonDecorator\PythonDecorator; | ||
|
||
#[Attribute] | ||
class Rpc extends PythonDecorator | ||
{ | ||
public function wrapper(mixed $object) | ||
{ | ||
return function (...$args) use ($object) { | ||
try { | ||
$error = false; | ||
$request = new Request($args[0]); | ||
} catch (InvalidJsonException $e) { | ||
$error = true; | ||
echo JsonRpc::parseError(); | ||
} catch (ValidationException $e) { | ||
$error = true; | ||
echo JsonRpc::invalidRequest(); | ||
} | ||
|
||
if (!$error) { | ||
if (!method_exists($object, $request->method)) { | ||
echo JsonRpc::methodNotFound(); | ||
} else { | ||
echo JsonRpc::result($object, $request); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sagittaracc\PhpPythonDecorator\tests\examples\RpcController; | ||
|
||
final class RpcTest extends TestCase | ||
{ | ||
public function testRpc(): void | ||
{ | ||
ob_start(); | ||
|
||
// STUB | ||
$requestBody = '{"id":1,"method":"sum","params":[1,2]}'; | ||
|
||
$controller = new RpcController(); | ||
$controller($requestBody); | ||
|
||
$this->assertSame('{"json-rpc":"2.0","id":1,"result":3}', ob_get_clean()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Sagittaracc\PhpPythonDecorator\tests\examples; | ||
|
||
use Sagittaracc\PhpPythonDecorator\Decorator; | ||
use Sagittaracc\PhpPythonDecorator\modules\rpc\core\Rpc; | ||
|
||
#[Rpc] | ||
class RpcController | ||
{ | ||
use Decorator; | ||
|
||
public function sum($a, $b) | ||
{ | ||
return $a + $b; | ||
} | ||
} |