diff --git a/src/Transport/AbstractTransport.php b/src/Transport/AbstractTransport.php deleted file mode 100644 index d565cc0..0000000 --- a/src/Transport/AbstractTransport.php +++ /dev/null @@ -1,87 +0,0 @@ -toJson(); - - if (strlen($json) > static::MAX_MESSAGE_SIZE) { - throw new TransportException("Encoded message exceeds size limit of " . static::MAX_MESSAGE_SIZE . " bytes."); - } - - return $json; - } - - /** - * Decodes and validates a received JSON string into a JsonRpcMessage. - * - * @param string $data The raw JSON data received from the transport. - * @return JsonRpcMessage|null The decoded message, or null if decoding fails (e.g., invalid JSON). - * @throws TransportException If the received data string exceeds MAX_MESSAGE_SIZE before decoding. - */ - protected function decodeMessage(string $data): ?JsonRpcMessage - { - if (strlen($data) > static::MAX_MESSAGE_SIZE) { - throw new TransportException("Received data exceeds size limit of " . static::MAX_MESSAGE_SIZE . " bytes."); - } - - try { - return JsonRpcMessage::fromJson($data); - } catch (\Exception $e) { - // Log but don't throw - allow server to handle invalid messages - $this->log("Error parsing received JSON message: " . $e->getMessage() . ". Data: " . substr($data, 0, 200) . "..."); - return null; - } - } - - /** - * Logs a message related to the transport's operation. - * - * This default implementation writes messages to PHP's error_log. - * Subclasses can override this to use a more sophisticated logging mechanism. - * - * @param string $message The message to log. - */ - public function log(string $message): void - { - error_log(get_class($this) . ": " . $message); - } - - /** - * Indicates a preference for using Server-Sent Events (SSE) for streaming responses, if applicable. - * - * Transports that support SSE (like HttpTransport) can use this hint to switch - * their response mode. Other transports can ignore this. - * - * @param bool $prefer True to prefer SSE, false otherwise. - */ - public function preferSseStream(bool $prefer = true): void - { - // Default implementation does nothing, to be overridden by transports that support SSE. - } -} diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index 98c1945..7a7b21a 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -24,7 +24,7 @@ * Origin checks, complex ACK logic, and related session properties have been removed. * It primarily focuses on basic JSON-RPC via POST. */ -class HttpTransport extends AbstractTransport +class HttpTransport implements TransportInterface { // Kept essential properties for basic POST JSON-RPC. /** @var ResponseInterface|null The PSR-7 response object being prepared. */ @@ -266,4 +266,20 @@ public function getResponse(): ResponseInterface } return $this->response; } + + /** + * Indicates a preference for using Server-Sent Events (SSE) for streaming responses, if applicable. + * + * Transports that support SSE (like HttpTransport) can use this hint to switch + * their response mode. Other transports can ignore this. + * + * @param bool $prefer True to prefer SSE, false otherwise. + */ + public function preferSseStream(bool $prefer = true): void + { + // Default implementation does nothing, to be overridden by transports that support SSE. + // In a real HttpTransport that supports SSE, this method would set a flag + // to change response content-type and formatting in the send() method. + // For this simplified version, it remains a no-op. + } } diff --git a/src/Transport/StdioTransport.php b/src/Transport/StdioTransport.php index 3d30a57..c96a73f 100644 --- a/src/Transport/StdioTransport.php +++ b/src/Transport/StdioTransport.php @@ -13,7 +13,7 @@ * Implements a transport using STDIN, STDOUT, and STDERR for line-based JSON messaging. * Each JSON-RPC message (or batch of messages) is expected to be on a single line. */ -class StdioTransport extends AbstractTransport +class StdioTransport implements TransportInterface { /** @var resource The standard input stream. */ private $stdin; @@ -203,4 +203,15 @@ public function isStreamOpen(): bool { return false; } + + /** + * Indicates a preference for using Server-Sent Events (SSE) for streaming responses, if applicable. + * StdioTransport does not support SSE, so this is a no-op. + * + * @param bool $prefer True to prefer SSE, false otherwise. + */ + public function preferSseStream(bool $prefer = true): void + { + // StdioTransport does not support SSE. + } } diff --git a/src/Transport/TransportInterface.php b/src/Transport/TransportInterface.php index 6cd95fc..b922e9d 100644 --- a/src/Transport/TransportInterface.php +++ b/src/Transport/TransportInterface.php @@ -50,16 +50,6 @@ public function receive(): ?array; */ public function send(JsonRpcMessage|array $message): void; - /** - * Logs a message specific to the transport's operation. - * - * Example: For StdioTransport, this might write to STDERR. - * For HttpTransport, it might use a PSR-3 logger if integrated. - * - * @param string $message The message to log. - */ - public function log(string $message): void; - /** * Checks if the transport connection is considered closed. *