|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Console\Commands; |
| 6 | + |
| 7 | +use Illuminate\Routing\Route; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Illuminate\Support\Stringable; |
| 11 | +use ReflectionClass; |
| 12 | +use ReflectionMethod; |
| 13 | +use Sajya\Server\Annotations\Param; |
| 14 | +use Sajya\Server\Attributes\RpcMethod; |
| 15 | + |
| 16 | +/** @deprecated Remove as soon as this pull-request is accepted : https://github.com/sajya/server/pull/87 */ |
| 17 | +class Docs extends \Sajya\Server\Docs |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Docs constructor. |
| 21 | + * |
| 22 | + * @param Route $route |
| 23 | + */ |
| 24 | + public function __construct(Route $route) |
| 25 | + { |
| 26 | + parent::__construct($route); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @return \Illuminate\Support\Collection |
| 31 | + */ |
| 32 | + public function getAnnotations(): Collection |
| 33 | + { |
| 34 | + return collect($this->procedures) |
| 35 | + ->map(function (string $class) { |
| 36 | + |
| 37 | + $reflectionClass = new ReflectionClass($class); |
| 38 | + $name = $reflectionClass->getProperty('name')->getValue(); |
| 39 | + |
| 40 | + return collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)) |
| 41 | + ->map(function (ReflectionMethod $method) use ($name) { |
| 42 | + |
| 43 | + $attributes = $this->getMethodAnnotations($method); |
| 44 | + |
| 45 | + $request = [ |
| 46 | + 'jsonrpc' => '2.0', |
| 47 | + 'id' => 1, |
| 48 | + 'method' => $name . $this->delimiter . $method->getName(), |
| 49 | + 'params' => $attributes?->params, |
| 50 | + ]; |
| 51 | + |
| 52 | + $response = [ |
| 53 | + 'jsonrpc' => '2.0', |
| 54 | + 'id' => 1, |
| 55 | + 'result' => $attributes?->result, |
| 56 | + ]; |
| 57 | + |
| 58 | + return [ |
| 59 | + 'name' => $name, |
| 60 | + 'delimiter' => $this->delimiter, |
| 61 | + 'method' => $method->getName(), |
| 62 | + 'description' => $attributes?->description, |
| 63 | + 'params' => $attributes?->params, |
| 64 | + 'result' => $attributes?->result, |
| 65 | + 'request' => $this->highlight($request), |
| 66 | + 'response' => $this->highlight($response), |
| 67 | + ]; |
| 68 | + }); |
| 69 | + }) |
| 70 | + ->flatten(1); |
| 71 | + } |
| 72 | + |
| 73 | + private function getMethodAnnotations(ReflectionMethod $method): ?RpcMethod |
| 74 | + { |
| 75 | + $attributes = $method->getAttributes(RpcMethod::class, \ReflectionAttribute::IS_INSTANCEOF); |
| 76 | + |
| 77 | + foreach ($attributes as $attribute) { |
| 78 | + /** @var RpcMethod $instance */ |
| 79 | + $instance = $attribute->newInstance(); |
| 80 | + |
| 81 | + return $instance; |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Highlights a JSON structure using HTML span tags with colors. |
| 89 | + * |
| 90 | + * @param array $value The JSON data to be highlighted. |
| 91 | + * |
| 92 | + * @return \Illuminate\Support\Stringable The highlighted JSON as a string. |
| 93 | + * @throws \JsonException If encoding fails. |
| 94 | + * |
| 95 | + */ |
| 96 | + private function highlight(array $value): Stringable |
| 97 | + { |
| 98 | + $json = json_encode($value, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
| 99 | + |
| 100 | + return Str::of($json) |
| 101 | + // Highlight keys (both string and numeric) |
| 102 | + ->replaceMatches('/"(\w+)":/i', '"<span style="color:#A0AEC0;">$1</span>":') |
| 103 | + ->replaceMatches('/"(\d+)":/i', '"<span style="color:#A0AEC0;">$1</span>":') |
| 104 | + |
| 105 | + // Highlight null values |
| 106 | + ->replaceMatches('/":\s*(null)/i', '": <span style="color:#F7768E;">$1</span>') |
| 107 | + |
| 108 | + // Highlight string values |
| 109 | + ->replaceMatches('/":\s*"([^"]*)"/', '": "<span style="color:#9ECE6A;">$1</span>"') |
| 110 | + |
| 111 | + // Highlight numeric values |
| 112 | + ->replaceMatches('/":\s*(\d+(\.\d+)?)/', '": <span style="color:#E0AF68;">$1</span>') |
| 113 | + |
| 114 | + // Highlight boolean values (true/false) |
| 115 | + ->replaceMatches('/":\s*(true|false)/i', '": <span style="color:#7AA2F7;">$1</span>') |
| 116 | + ->wrap('<pre style="color:rgba(212,212,212,0.75);">', '</pre>'); |
| 117 | + } |
| 118 | +} |
0 commit comments