diff --git a/Makefile b/Makefile index 74a3576..04cdd75 100644 --- a/Makefile +++ b/Makefile @@ -95,8 +95,8 @@ phpstan: var vendor ## Analyze code using PHPStan $(RUN) phpstan analyze --memory-limit=1G $(ARGS) .PHONY: phpstan -test: var vendor up ## Run tests using PHPUnit - $(RUN) vendor/bin/phpunit $(ARGS) +test: var vendor ## Run tests using PHPUnit + $(RUN) vendor/bin/phpunit $(ARGS) --colors .PHONY: test infect: var vendor up ## Run mutation tests using Infection diff --git a/src/Pool/Descriptor.php b/src/Pool/Descriptor.php new file mode 100644 index 0000000..74bd111 --- /dev/null +++ b/src/Pool/Descriptor.php @@ -0,0 +1,39 @@ + $this->bytes ??= ($this->decode)($this->buffer); } + + /** + * @param non-empty-string $buffer + */ + public static function base64(string $buffer): self + { + return new self($buffer, \base64_decode(...)); // @phpstan-ignore argument.type + } + + /** + * @param non-empty-string $buffer + */ + public static function raw(string $buffer): self + { + return new self($buffer, static fn(string $buffer) => $buffer); + } + + /** + * @param non-empty-string $buffer + * @param \Closure(non-empty-string): non-empty-string $decode + */ + private function __construct( + private readonly string $buffer, + private readonly \Closure $decode, + ) {} +} diff --git a/src/Pool/EnumMetadata.php b/src/Pool/EnumMetadata.php new file mode 100644 index 0000000..8822560 --- /dev/null +++ b/src/Pool/EnumMetadata.php @@ -0,0 +1,18 @@ +, true> $registered */ + static $registered = []; + + $fqcn = $this->registrar::class; + + if (!isset($registered[$fqcn])) { + $pool->register($this->registrar); + $registered[$fqcn] = true; + } + } +} diff --git a/src/Pool/Registrar.php b/src/Pool/Registrar.php new file mode 100644 index 0000000..8dc5fb7 --- /dev/null +++ b/src/Pool/Registrar.php @@ -0,0 +1,17 @@ + */ + public private(set) array $descriptors = []; + + /** @var array */ + public private(set) array $messageTypes = []; + + /** @var array */ + private array $classToTypeIndex = []; + + /** @var array */ + private array $messageTypeToDescriptorIndex = []; + + /** @var array */ + public private(set) array $enumTypes = []; + + /** @var array */ + private array $enumToTypeIndex = []; + + /** @var array */ + private array $enumTypeToDescriptorIndex = []; + + /** @var array */ + public private(set) array $serviceTypes = []; + + /** @var array */ + private array $serviceTypeToDescriptorIndex = []; + + /** + * @param non-empty-string $type + */ + public function messageByType(string $type): MessageMetadata + { + return $this->messageTypes[$type] ?? self::throwTypeNotFound($type); + } + + /** + * @param class-string $fqcn + * @return non-empty-string + */ + public function classType(string $fqcn): string + { + return $this->classToTypeIndex[$fqcn] ?? self::throwClassTypeNotFound($fqcn); + } + + /** + * @param non-empty-string $messageType + */ + public function descriptorByMessage(string $messageType): Descriptor + { + return $this->descriptors[$this->messageTypeToDescriptorIndex[$messageType] ?? self::throwTypeNotFound($messageType)] + ?? self::throwDescriptorNotFound($messageType); + } + + /** + * @param non-empty-string $type + */ + public function enumByType(string $type): EnumMetadata + { + return $this->enumTypes[$type] ?? self::throwTypeNotFound($type); + } + + /** + * @param class-string $fqcn + * @return non-empty-string + */ + public function enumType(string $fqcn): string + { + return $this->enumToTypeIndex[$fqcn] ?? self::throwEnumTypeNotFound($fqcn); + } + + /** + * @param non-empty-string $enumType + */ + public function descriptorByEnum(string $enumType): Descriptor + { + return $this->descriptors[$this->enumTypeToDescriptorIndex[$enumType] ?? self::throwTypeNotFound($enumType)] + ?? self::throwDescriptorNotFound($enumType); + } + + /** + * @param non-empty-string $type + */ + public function serviceByType(string $type): ServiceMetadata + { + return $this->serviceTypes[$type] ?? self::throwTypeNotFound($type); + } + + /** + * @param non-empty-string $serviceType + */ + public function descriptorByService(string $serviceType): Descriptor + { + return $this->descriptors[$this->serviceTypeToDescriptorIndex[$serviceType] ?? self::throwTypeNotFound($serviceType)] + ?? self::throwDescriptorNotFound($serviceType); + } + + public function register(Registrar ...$registries): self + { + $pool = self::get(); + + foreach ($registries as $registry) { + $registry->register($pool); + } + + return $pool; + } + + /** + * @param array $types + */ + public function add(Descriptor $descriptor, array $types): self + { + $pool = self::get(); + + $idx = \count($pool->descriptors); + $pool->descriptors[] = $descriptor; + + foreach ($types as $type => $md) { + if ($md instanceof MessageMetadata) { + $pool->doAddMessageType($type, $md, $idx); + } elseif ($md instanceof EnumMetadata) { + $pool->doAddEnumType($type, $md, $idx); + } elseif ($md instanceof ServiceMetadata) { // @phpstan-ignore instanceof.alwaysTrue + $pool->doAddServiceType($type, $md, $idx); + } + } + + return $pool; + } + + /** + * @param non-empty-string $type + * @param non-negative-int $descriptorIdx + */ + private function doAddMessageType(string $type, MessageMetadata $md, int $descriptorIdx): void + { + if (isset($this->messageTypes[$type])) { + self::throwTypeAlreadyRegistered($type); + } + + $this->messageTypeToDescriptorIndex[$type] = $descriptorIdx; + $this->messageTypes[$type] = $md; + $this->classToTypeIndex[$md->fqcn] = $type; + } + + /** + * @param non-empty-string $type + * @param non-negative-int $descriptorIdx + */ + private function doAddEnumType(string $type, EnumMetadata $md, int $descriptorIdx): void + { + if (isset($this->enumTypes[$type])) { + self::throwTypeAlreadyRegistered($type); + } + + $this->enumTypeToDescriptorIndex[$type] = $descriptorIdx; + $this->enumTypes[$type] = $md; + $this->enumToTypeIndex[$md->fqcn] = $type; + } + + /** + * @param non-empty-string $type + * @param non-negative-int $descriptorIdx + */ + private function doAddServiceType(string $type, ServiceMetadata $md, int $descriptorIdx): void + { + if (isset($this->serviceTypes[$type])) { + self::throwTypeAlreadyRegistered($type); + } + + $this->serviceTypeToDescriptorIndex[$type] = $descriptorIdx; + $this->serviceTypes[$type] = $md; + } + + /** + * @param non-empty-string $type + */ + private static function throwTypeAlreadyRegistered(string $type): never + { + throw new \RuntimeException(\sprintf('Type "%s" is already registered in the \Thesis\Protobuf\Pool\Registry. Ensure that you are using protobuf compiler correctly, or use \Thesis\Protobuf\Pool\OnceRegistrar to prevent duplicate registration of types in the pool', $type)); + } + + /** + * @param class-string $fqcn + */ + private static function throwClassTypeNotFound(string $fqcn): never + { + throw new \RuntimeException(\sprintf('Associated with class "%s" metadata not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?', $fqcn)); + } + + /** + * @param class-string $fqcn + */ + private static function throwEnumTypeNotFound(string $fqcn): never + { + throw new \RuntimeException(\sprintf('Associated with enum "%s" metadata not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?', $fqcn)); + } + + /** + * @param non-empty-string $type + */ + private static function throwDescriptorNotFound(string $type): never + { + throw new \RuntimeException(\sprintf('Descriptor for type "%s" not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?', $type)); + } + + /** + * @param non-empty-string $type + */ + private static function throwTypeNotFound(string $type): never + { + throw new \RuntimeException(\sprintf('Type metadata "%s" not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?', $type)); + } +} diff --git a/src/Pool/ServiceMetadata.php b/src/Pool/ServiceMetadata.php new file mode 100644 index 0000000..7803f25 --- /dev/null +++ b/src/Pool/ServiceMetadata.php @@ -0,0 +1,22 @@ +add($descriptor = Descriptor::raw('xyz'), [ + 'thesis.api.Request' => $md = new MessageMetadata(\stdClass::class), + ]); + + self::assertEquals($md, $pool->messageByType('thesis.api.Request')); + self::assertEquals($descriptor, $pool->descriptorByMessage('thesis.api.Request')); + self::assertSame('thesis.api.Request', $pool->classType(\stdClass::class)); + + self::expectExceptionObject(new \RuntimeException('Type "thesis.api.Request" is already registered in the \Thesis\Protobuf\Pool\Registry. Ensure that you are using protobuf compiler correctly, or use \Thesis\Protobuf\Pool\OnceRegistrar to prevent duplicate registration of types in the pool')); + $pool->add($descriptor, ['thesis.api.Request' => $md]); + } + + public function testEnumRegistered(): void + { + $pool = Registry::get(); + $pool->add($descriptor = Descriptor::raw('xyz'), [ + 'thesis.api.RequestType' => $md = new EnumMetadata('Thesis\Api\RequestType'), + ]); + + self::assertEquals($md, $pool->enumByType('thesis.api.RequestType')); + self::assertEquals($descriptor, $pool->descriptorByEnum('thesis.api.RequestType')); + + self::expectExceptionObject(new \RuntimeException('Type "thesis.api.RequestType" is already registered in the \Thesis\Protobuf\Pool\Registry. Ensure that you are using protobuf compiler correctly, or use \Thesis\Protobuf\Pool\OnceRegistrar to prevent duplicate registration of types in the pool')); + $pool->add($descriptor, ['thesis.api.RequestType' => $md]); + } + + public function testServiceRegistered(): void + { + $pool = Registry::get(); + $pool->add($descriptor = Descriptor::raw('xyz'), [ + 'thesis.api.RequestService' => $md = new ServiceMetadata('Thesis\Api\RequestServiceClient'), + ]); + + self::assertEquals($md, $pool->serviceByType('thesis.api.RequestService')); + self::assertEquals($descriptor, $pool->descriptorByService('thesis.api.RequestService')); + + self::expectExceptionObject(new \RuntimeException('Type "thesis.api.RequestService" is already registered in the \Thesis\Protobuf\Pool\Registry. Ensure that you are using protobuf compiler correctly, or use \Thesis\Protobuf\Pool\OnceRegistrar to prevent duplicate registration of types in the pool')); + $pool->add($descriptor, ['thesis.api.RequestService' => $md]); + } + + public function testRegister(): void + { + $pool = Registry::get(); + $pool->register(new class implements Registrar { + #[\Override] + public function register(Registry $pool): void + { + $pool->add(Descriptor::raw('xyz'), [ + 'thesis.api.OtherType' => new EnumMetadata('Thesis\Api\OtherType'), + ]); + } + }); + + self::assertEquals(new EnumMetadata('Thesis\Api\OtherType'), $pool->enumByType('thesis.api.OtherType')); + } + + public function testTypeNotFound(): void + { + $pool = Registry::get(); + + self::expectExceptionObject(new \RuntimeException('Type metadata "thesis.api.OtherRequest" not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?')); + $pool->messageByType('thesis.api.OtherRequest'); + } + + public function testClassNotFound(): void + { + $pool = Registry::get(); + + self::expectExceptionObject(new \RuntimeException('Associated with class "Thesis\Protobuf\Pool\RegistryTest" metadata not found in the \Thesis\Protobuf\Pool\Registry. Perhaps you forgot to include autoload.metadata.php in composer.json or did not call the appropriate descriptor registrar to register types in the pool?')); + $pool->classType(self::class); + } +}