diff --git a/src/Pool/Descriptor.php b/src/Pool/Descriptor.php deleted file mode 100644 index 74bd111..0000000 --- a/src/Pool/Descriptor.php +++ /dev/null @@ -1,39 +0,0 @@ - $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/File.php b/src/Pool/File.php deleted file mode 100644 index 4fd30dc..0000000 --- a/src/Pool/File.php +++ /dev/null @@ -1,26 +0,0 @@ - $dependencies - * @param list $messages - * @param list $enums - * @param list $services - */ - public function __construct( - public string $name, - public array $dependencies = [], - public array $messages = [], - public array $enums = [], - public array $services = [], - ) {} -} diff --git a/src/Pool/File/EnumDescriptor.php b/src/Pool/File/EnumDescriptor.php deleted file mode 100644 index f5152ae..0000000 --- a/src/Pool/File/EnumDescriptor.php +++ /dev/null @@ -1,20 +0,0 @@ -, 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 deleted file mode 100644 index 8dc5fb7..0000000 --- a/src/Pool/Registrar.php +++ /dev/null @@ -1,17 +0,0 @@ - */ - private array $descriptors = []; - - /** @var array */ - private array $files = []; - - /** @var array map File\MessageDescriptor by typename, used by google.protobuf.Any */ - private array $messages = []; - - /** @var array map File\EnumDescriptor by typename */ - private array $enums = []; - - /** @var array map File\ServiceDescriptor by typename, used by server reflection */ - private array $services = []; - - /** @var array map typename by fqcn, used by google.protobuf.Any */ - private array $types = []; - - /** @var array map filename by typename, used by server reflection */ - private array $symbols = []; - - /** - * @param non-empty-string $type - */ - public function messageDescriptorByType(string $type): File\MessageDescriptor - { - return $this->messages[$type] ?? self::throwTypeNotFound($type); - } - - /** - * @param class-string $fqcn - * @return non-empty-string - */ - public function classType(string $fqcn): string - { - return $this->types[$fqcn] ?? self::throwClassTypeNotFound($fqcn); - } - - /** - * @param non-empty-string $type - */ - public function enumDescriptorByType(string $type): File\EnumDescriptor - { - return $this->enums[$type] ?? self::throwTypeNotFound($type); - } - - /** - * @param class-string $fqcn - * @return non-empty-string - */ - public function enumType(string $fqcn): string - { - return $this->types[$fqcn] ?? self::throwEnumTypeNotFound($fqcn); - } - - /** - * @param non-empty-string $type - */ - public function serviceDescriptorByType(string $type): File\ServiceDescriptor - { - return $this->services[$type] ?? self::throwTypeNotFound($type); - } - - /** - * @param non-empty-string $filename - */ - public function fileByName(string $filename): File - { - return $this->files[$filename] ?? self::throwTypeNotFound($filename); - } - - /** - * @param non-empty-string $symbol - */ - public function fileBySymbol(string $symbol): File - { - return $this->files[$this->symbols[$symbol] ?? self::throwTypeNotFound($symbol)] ?? self::throwTypeNotFound($symbol); - } - - /** - * @param non-empty-string $filename - */ - public function descriptorByFilename(string $filename): Descriptor - { - return $this->descriptors[$filename] ?? self::throwTypeNotFound($filename); - } - - public function register(Registrar ...$registries): self - { - $pool = self::get(); - - foreach ($registries as $registry) { - $registry->register($pool); - } - - return $pool; - } - - public function add(Descriptor $descriptor, File $file): self - { - $pool = self::get(); - - $pool->descriptors[$file->name] = $descriptor; - $pool->files[$file->name] = $file; - - foreach ($file->messages as $message) { - if (isset($pool->messages[$message->name])) { - self::throwTypeAlreadyRegistered($message->name); - } - - $pool->messages[$message->name] = $message; - $pool->symbols[$message->name] = $file->name; - $pool->types[$message->fqcn] = $message->name; - } - - foreach ($file->enums as $enum) { - if (isset($pool->enums[$enum->name])) { - self::throwTypeAlreadyRegistered($enum->name); - } - - $pool->enums[$enum->name] = $enum; - $pool->symbols[$enum->name] = $file->name; - $pool->types[$enum->fqcn] = $enum->name; - } - - foreach ($file->services as $service) { - if (isset($pool->services[$service->name])) { - self::throwTypeAlreadyRegistered($service->name); - } - - $pool->services[$service->name] = $service; - $pool->symbols[$service->name] = $file->name; - } - - return $pool; - } - - /** - * @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 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)); - } - - private function __construct() {} -} diff --git a/tests/Pool/RegistryTest.php b/tests/Pool/RegistryTest.php deleted file mode 100644 index 0b6bee8..0000000 --- a/tests/Pool/RegistryTest.php +++ /dev/null @@ -1,116 +0,0 @@ -add($descriptor = Descriptor::raw('xyz'), $file = new File( - name: 'api.proto', - messages: [ - $md = new File\MessageDescriptor( - name: 'thesis.api.Request', - fqcn: \stdClass::class, - ), - ], - )); - - self::assertEquals($md, $pool->messageDescriptorByType('thesis.api.Request')); - self::assertEquals('thesis.api.Request', $pool->classType(\stdClass::class)); - self::assertEquals($file, $pool->fileBySymbol('thesis.api.Request')); - self::assertEquals($descriptor, $pool->descriptorByFilename($file->name)); - $this->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, new File( - name: 'api.proto', - messages: [ - new File\MessageDescriptor( - name: 'thesis.api.Request', - fqcn: \stdClass::class, - ), - ], - )); - } - - public function testEnumRegistered(): void - { - $pool = Registry::get(); - $pool->add($descriptor = Descriptor::raw('xyz'), $file = new File( - name: 'api.proto', - enums: [ - $md = new File\EnumDescriptor( - name: 'thesis.api.RequestType', - fqcn: \stdClass::class, - ), - ], - )); - - self::assertEquals($md, $pool->enumDescriptorByType('thesis.api.RequestType')); - self::assertEquals('thesis.api.RequestType', $pool->enumType(\stdClass::class)); - self::assertEquals($file, $pool->fileBySymbol('thesis.api.RequestType')); - self::assertEquals($descriptor, $pool->descriptorByFilename($file->name)); - - $this->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, new File( - name: 'api.proto', - enums: [ - new File\EnumDescriptor( - name: 'thesis.api.RequestType', - fqcn: \stdClass::class, - ), - ], - )); - } - - public function testServiceRegistered(): void - { - $pool = Registry::get(); - $pool->add($descriptor = Descriptor::raw('xyz'), $file = new File( - name: 'api.proto', - services: [ - $md = new File\ServiceDescriptor( - name: 'thesis.api.RequestService', - clientFqcn: \stdClass::class, - ), - ], - )); - - self::assertEquals($md, $pool->serviceDescriptorByType('thesis.api.RequestService')); - self::assertEquals($file, $pool->fileBySymbol('thesis.api.RequestService')); - self::assertEquals($descriptor, $pool->descriptorByFilename($file->name)); - - $this->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, new File( - name: 'api.proto', - services: [ - new File\ServiceDescriptor( - name: 'thesis.api.RequestService', - clientFqcn: \stdClass::class, - ), - ], - )); - } - - 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->messageDescriptorByType('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); - } -}