diff --git a/README.md b/README.md index 3b0ae5f..4915c71 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ composer require php-skir/runtime ## What it provides - `DenseJson` for encoding and decoding Skir dense JSON values. -- `Cbor` helpers for optional CBOR bodies and SkirRPC envelopes. - `Type`, `Field`, and `Variant` descriptors for generated DTO metadata. - `EnumValue` for Skir enum constants and wrapper variants. - `MethodDescriptor` for generated SkirRPC method metadata. @@ -47,23 +46,6 @@ $data = DenseJson::fromJson($user, $json); Struct values are encoded as field-number indexed arrays. Removed fields and sparse field numbers are preserved so generated DTOs can stay compatible with existing Skir schemas. -## CBOR - -CBOR support is optional and requires `spomky-labs/cbor-php` in the consuming app: - -```bash -composer require spomky-labs/cbor-php -``` - -The runtime helper keeps CBOR wrapping shared between server and client packages: - -```php -use Skir\Runtime\Cbor; - -$body = Cbor::encodeEnvelope($methodDescriptor, $request); -$payload = Cbor::decodeEnvelope($body); -``` - ## Current scope This runtime does not register routes, discover procedures, or make HTTP calls. Those concerns belong in the `php-skir/server` and `php-skir/client` packages. diff --git a/composer.json b/composer.json index 8a505b1..447b1c0 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,7 @@ }, "require-dev": { "laravel/pint": "^1.29", - "phpunit/phpunit": "^12.5", - "spomky-labs/cbor-php": "^3.2" + "phpunit/phpunit": "^12.5" }, "autoload": { "psr-4": { @@ -41,16 +40,13 @@ }, "extra": { "branch-alias": { - "dev-main": "0.1.x-dev" + "dev-main": "0.2.x-dev" } }, "scripts": { "format": "pint", "test": "phpunit" }, - "suggest": { - "spomky-labs/cbor-php": "Required when using Skir\\Runtime\\Cbor helpers." - }, "config": { "sort-packages": true }, diff --git a/src/Cbor.php b/src/Cbor.php deleted file mode 100644 index f9056ab..0000000 --- a/src/Cbor.php +++ /dev/null @@ -1,109 +0,0 @@ -encode($value); - } catch (Throwable $exception) { - throw SkirRuntimeException::invalidCbor($exception->getMessage(), $exception); - } - } - - public static function decode(string $content): mixed - { - self::ensureAvailable(); - - try { - return Decoder::create() - ->decode(StringStream::create($content)) - ->normalize(); - } catch (Throwable $exception) { - throw SkirRuntimeException::invalidCbor($exception->getMessage(), $exception); - } - } - - public static function encodeDenseValue(Type $type, mixed $value): string - { - return self::encode(self::encodeValuePayload($type, $value)); - } - - public static function decodeDenseValue(Type $type, mixed $value): mixed - { - return self::decodeValuePayload($type, $value); - } - - public static function encodeValuePayload(Type $type, mixed $value): mixed - { - return DenseJson::encode($type, $value); - } - - public static function decodeValuePayload(Type $type, mixed $value): mixed - { - return DenseJson::decode($type, $value); - } - - public static function encodeValue(Type $type, mixed $value): string - { - return self::encodeDenseValue($type, $value); - } - - public static function decodeValue(Type $type, string $content): mixed - { - return self::decodeDenseValue($type, self::decode($content)); - } - - public static function encodeEnvelope(MethodDescriptor $descriptor, mixed $request): string - { - return self::encode([ - 'method' => $descriptor->name, - 'request' => self::encodeValuePayload($descriptor->requestType, $request), - ]); - } - - /** - * @return array{ - * method?: mixed, - * request?: mixed - * } - */ - public static function decodeEnvelope(string $content): array - { - $payload = self::decode($content); - - if (! is_array($payload)) { - throw SkirRuntimeException::invalidCbor('Skir CBOR envelopes must be maps.'); - } - - return $payload; - } - - private static function ensureAvailable(): void - { - if (self::available()) { - return; - } - - throw SkirRuntimeException::missingCborDependency(); - } -} diff --git a/src/Exceptions/SkirRuntimeException.php b/src/Exceptions/SkirRuntimeException.php index 1042738..7c4aa6c 100644 --- a/src/Exceptions/SkirRuntimeException.php +++ b/src/Exceptions/SkirRuntimeException.php @@ -14,18 +14,8 @@ public static function invalidDenseJson(string $message, ?Throwable $previous = return new self("Invalid dense JSON: {$message}", previous: $previous); } - public static function invalidCbor(string $message, ?Throwable $previous = null): self - { - return new self("Invalid CBOR: {$message}", previous: $previous); - } - public static function invalidValue(string $message): self { return new self($message); } - - public static function missingCborDependency(): self - { - return new self('Skir CBOR support requires the [spomky-labs/cbor-php] Composer package.'); - } } diff --git a/tests/CborTest.php b/tests/CborTest.php deleted file mode 100644 index ffcad73..0000000 --- a/tests/CborTest.php +++ /dev/null @@ -1,86 +0,0 @@ - 42, - 'name' => 'Maxim', - ]); - - $this->assertSame([ - 'id' => 42, - 'name' => 'Maxim', - ], Cbor::decodeValue($userType, $encoded)); - } - - public function test_it_round_trips_constant_enums_as_cbor(): void - { - $status = Type::enum([ - Variant::constant('active', 1), - ]); - - $encoded = Cbor::encodeValue($status, EnumValue::constant('active')); - - $this->assertEquals(EnumValue::constant('active'), Cbor::decodeValue($status, $encoded)); - } - - public function test_it_round_trips_the_unknown_enum_value_as_cbor(): void - { - $status = Type::enum([ - Variant::constant('active', 1), - ]); - - $encoded = Cbor::encodeValue($status, EnumValue::constant('UNKNOWN')); - - $this->assertEquals(EnumValue::constant('UNKNOWN'), Cbor::decodeValue($status, $encoded)); - } - - public function test_it_encodes_and_decodes_rpc_envelopes(): void - { - $userType = Type::struct([ - Field::value('id', 0, Type::int32()), - Field::value('name', 1, Type::string()), - ]); - - $descriptor = new MethodDescriptor('RenameUser', 1002, $userType, $userType); - - $envelope = Cbor::decodeEnvelope(Cbor::encodeEnvelope($descriptor, [ - 'id' => 42, - 'name' => 'Maxim', - ])); - - $this->assertSame('RenameUser', $envelope['method']); - $this->assertSame([ - 'id' => 42, - 'name' => 'Maxim', - ], Cbor::decodeDenseValue($userType, $envelope['request'])); - } - - public function test_it_rejects_invalid_cbor_payloads(): void - { - $this->expectException(SkirRuntimeException::class); - $this->expectExceptionMessage('Invalid CBOR:'); - - Cbor::decodeEnvelope('not-cbor'); - } -}