From 5c948da9f6ba8977832557dc175532dd036760f9 Mon Sep 17 00:00:00 2001 From: Jack Shpartko <3300101@gmail.com> Date: Sun, 16 Aug 2020 11:36:44 +0200 Subject: [PATCH 1/3] Return json with proper content-type --- src/Guide.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Guide.php b/src/Guide.php index 10436b7..afc9921 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -4,6 +4,7 @@ namespace Sajya\Server; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; use Illuminate\Support\Str; use ReflectionClass; @@ -40,9 +41,9 @@ public function __construct(array $procedures = []) /** * @param string $content * - * @return string + * @return JsonResponse */ - public function handle(string $content = ''): string + public function handle(string $content = ''): JsonResponse { $parser = new Parser($content); @@ -55,7 +56,7 @@ public function handle(string $content = ''): string $response = $parser->isBatch() ? $result->all() : $result->first(); - return json_encode($response, JSON_THROW_ON_ERROR, 512); + return response()->json($response); } /** From c4b5328859f321c78391f5bf48c9b07308ff816c Mon Sep 17 00:00:00 2001 From: Jack Shpartko <3300101@gmail.com> Date: Sun, 16 Aug 2020 11:37:33 +0200 Subject: [PATCH 2/3] Allow use Procedures with default /handle/ method to simplify configuration --- src/Guide.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Guide.php b/src/Guide.php index afc9921..fa97583 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -110,6 +110,10 @@ public function findProcedure(Request $request): ?string $class = Str::beforeLast($request->getMethod(), '@'); $method = Str::afterLast($request->getMethod(), '@'); + if (Str::contains($request->getMethod(), '@') === false) { + $method = 'handle'; + } + return $this->map ->filter(fn (string $procedure) => $this->getProcedureName($procedure) === $class) ->filter(fn (string $procedure) => $this->checkExistPublicMethod($procedure, $method)) From 0786a1d990d27978ecccae169ed567c537fe0d92 Mon Sep 17 00:00:00 2001 From: Jack Shpartko <3300101@gmail.com> Date: Mon, 17 Aug 2020 00:04:40 +0200 Subject: [PATCH 3/3] Added debug mode --- src/Guide.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Guide.php b/src/Guide.php index fa97583..b0b54f5 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -6,6 +6,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use ReflectionClass; use ReflectionMethod; @@ -23,6 +24,11 @@ class Guide */ protected Collection $map; + /** + * @var bool + */ + private bool $debugMode = false; + /** * Guide constructor. * @@ -30,6 +36,8 @@ class Guide */ public function __construct(array $procedures = []) { + $this->debugMode = config('app.debug'); + $this->map = collect($procedures) ->each(fn (string $class) => abort_unless( is_subclass_of($class, Procedure::class), @@ -93,6 +101,10 @@ public function handleProcedure(Request $request, bool $notification): Response return $this->makeResponse(new MethodNotFound(), $request); } + if ($this->debugMode) { + Log::info(sprintf('JSON-RPC Call: %s', $procedure), $request->jsonSerialize()); + } + $result = $notification ? HandleProcedure::dispatchAfterResponse($procedure) : HandleProcedure::dispatchNow($procedure);