diff --git a/src/Guide.php b/src/Guide.php index 10436b7..b0b54f5 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -4,7 +4,9 @@ namespace Sajya\Server; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use ReflectionClass; use ReflectionMethod; @@ -22,6 +24,11 @@ class Guide */ protected Collection $map; + /** + * @var bool + */ + private bool $debugMode = false; + /** * Guide constructor. * @@ -29,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), @@ -40,9 +49,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 +64,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); } /** @@ -92,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); @@ -109,6 +122,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))