diff --git a/src/Plugin/Generator/ProtoGenerator.php b/src/Plugin/Generator/ProtoGenerator.php index 0daf069..2650102 100644 --- a/src/Plugin/Generator/ProtoGenerator.php +++ b/src/Plugin/Generator/ProtoGenerator.php @@ -307,7 +307,7 @@ private function generateMessage( return $namespace; } - $namespace->addUse('Thesis\Protobuf\Reflection'); + $runtime = new RuntimeImports($namespace); $constructor = $classType->addMethod('__construct'); @@ -338,25 +338,21 @@ private function generateMessage( $type = null; if ($field->map !== null) { - $keyType = $this->types->create($field->map->key); - $valueType = $this->types->create($field->map->value); + $keyType = $this->types->create($field->map->key, $runtime); + $valueType = $this->types->create($field->map->value, $runtime); + $map = $runtime->protobuf('Map'); $type = new TypeDeclaration( - phpType: 'Protobuf\Map', - reflectionType: Literal::new('Reflection\MapT', [$keyType->reflectionType, $valueType->reflectionType]), + phpType: $map, + reflectionType: Literal::new($runtime->reflection('MapT'), [$keyType->reflectionType, $valueType->reflectionType]), nullable: false, isMap: true, - docType: "Protobuf\\Map<{$keyType->resolvedType()}, {$valueType->resolvedType()}>", - default: Literal::new('Protobuf\Map'), - uses: ['Thesis\Protobuf'], + docType: "{$map}<{$keyType->resolvedType()}, {$valueType->resolvedType()}>", + default: Literal::new($map), ); } - $type ??= $this->types->create($field); - - foreach ($type->uses as $use) { - $namespace->addUse($use); - } + $type ??= $this->types->create($field, $runtime); $repeated = $field->label === FieldDescriptorProto\Label::LABEL_REPEATED && !$type->isMap; $required = $field->label === FieldDescriptorProto\Label::LABEL_REQUIRED || $features?->fieldPresence === FeatureSet\FieldPresence::LEGACY_REQUIRED; @@ -382,7 +378,7 @@ private function generateMessage( $parameters[] = $features?->repeatedFieldEncoding === null || $features->repeatedFieldEncoding === FeatureSet\RepeatedFieldEncoding::PACKED; } - $reflectionType = Literal::new('Reflection\ListT', $parameters); + $reflectionType = Literal::new($runtime->reflection('ListT'), $parameters); } $default = $nullable ? null : $type->default; @@ -396,7 +392,7 @@ private function generateMessage( $parameter ->setType($phpType) - ->addAttribute('Reflection\Field', [ + ->addAttribute($runtime->reflection('Field'), [ $field->number, $reflectionType, ]) @@ -456,7 +452,7 @@ private function generateMessage( ->setType($this->namespacer->fqcn("{$message->path}.{$oneofInterfaces[$idx]}")) ->setNullable() ->setDefaultValue(null) - ->addAttribute('Reflection\OneOf', [ + ->addAttribute($runtime->reflection('OneOf'), [ array_map( fn(Parser\FieldDescriptor $variant) => new Literal( \sprintf( diff --git a/src/Plugin/Generator/RuntimeImports.php b/src/Plugin/Generator/RuntimeImports.php new file mode 100644 index 0000000..288692e --- /dev/null +++ b/src/Plugin/Generator/RuntimeImports.php @@ -0,0 +1,63 @@ +refer(self::REFLECTION, $name); + } + + /** + * @param non-empty-string $name a class of Thesis\Protobuf: "Map" + */ + public function protobuf(string $name): string + { + return $this->refer(self::PROTOBUF, $name); + } + + /** + * @param non-empty-string $import + * @param non-empty-string $name + */ + private function refer(string $import, string $name): string + { + $this->namespace->addUse($import); + + $separator = strpos($name, '::'); + $class = $separator === false ? $name : substr($name, 0, $separator); + $member = $separator === false ? '' : substr($name, $separator); + + return $this->namespace->simplifyName("{$import}\\{$class}") . $member; + } +} diff --git a/src/Plugin/Generator/TypeDeclaration.php b/src/Plugin/Generator/TypeDeclaration.php index dd80e1b..56b9181 100644 --- a/src/Plugin/Generator/TypeDeclaration.php +++ b/src/Plugin/Generator/TypeDeclaration.php @@ -11,9 +11,6 @@ */ final readonly class TypeDeclaration { - /** - * @param list $uses - */ public function __construct( public string $phpType, public Literal $reflectionType, @@ -21,7 +18,6 @@ public function __construct( public bool $isMap = false, public ?string $docType = null, public mixed $default = null, - public array $uses = [], ) {} public function resolvedType(): string diff --git a/src/Plugin/Generator/TypeDeclarationFactory.php b/src/Plugin/Generator/TypeDeclarationFactory.php index 53c689a..a92a07c 100644 --- a/src/Plugin/Generator/TypeDeclarationFactory.php +++ b/src/Plugin/Generator/TypeDeclarationFactory.php @@ -19,7 +19,7 @@ public function __construct( private Dependency\Graph $graph, ) {} - public function create(Parser\FieldDescriptor $field): TypeDeclaration + public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): TypeDeclaration { if ($field->typeName !== null) { $fieldType = $this->graph->get($field->typeName); @@ -41,7 +41,7 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration return new TypeDeclaration( phpType: $fieldType->fqcn, - reflectionType: Literal::new('Reflection\\' . ($isEnum ? 'EnumT' : 'ObjectT'), [ + reflectionType: Literal::new($runtime->reflection($isEnum ? 'EnumT' : 'ObjectT'), [ new Literal("{$fieldType->fqcn}::class"), ]), nullable: !$isEnum, @@ -55,8 +55,8 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration Type::TYPE_DOUBLE => new TypeDeclaration( phpType: 'float', reflectionType: new Literal(match ($type) { - Type::TYPE_FLOAT => 'Reflection\FloatT::T', - Type::TYPE_DOUBLE => 'Reflection\DoubleT::T', + Type::TYPE_FLOAT => $runtime->reflection('FloatT::T'), + Type::TYPE_DOUBLE => $runtime->reflection('DoubleT::T'), }), default: 0, ), @@ -64,8 +64,8 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration Type::TYPE_FIXED64 => new TypeDeclaration( phpType: '\BcMath\Number', reflectionType: new Literal(match ($type) { - Type::TYPE_UINT64 => 'Reflection\Uint64T::T', - Type::TYPE_FIXED64 => 'Reflection\Fixed64T::T', + Type::TYPE_UINT64 => $runtime->reflection('Uint64T::T'), + Type::TYPE_FIXED64 => $runtime->reflection('Fixed64T::T'), }), default: Literal::new('\BcMath\Number', [0]), ), @@ -79,28 +79,28 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration Type::TYPE_SINT64 => new TypeDeclaration( phpType: 'int', reflectionType: new Literal(match ($type) { - Type::TYPE_INT32 => 'Reflection\Int32T::T', - Type::TYPE_FIXED32 => 'Reflection\Fixed32T::T', - Type::TYPE_UINT32 => 'Reflection\Uint32T::T', - Type::TYPE_SFIXED32 => 'Reflection\SFixed32T::T', - Type::TYPE_SINT32 => 'Reflection\SInt32T::T', - Type::TYPE_INT64 => 'Reflection\Int64T::T', - Type::TYPE_SFIXED64 => 'Reflection\SFixed64T::T', - Type::TYPE_SINT64 => 'Reflection\SInt64T::T', + Type::TYPE_INT32 => $runtime->reflection('Int32T::T'), + Type::TYPE_FIXED32 => $runtime->reflection('Fixed32T::T'), + Type::TYPE_UINT32 => $runtime->reflection('Uint32T::T'), + Type::TYPE_SFIXED32 => $runtime->reflection('SFixed32T::T'), + Type::TYPE_SINT32 => $runtime->reflection('SInt32T::T'), + Type::TYPE_INT64 => $runtime->reflection('Int64T::T'), + Type::TYPE_SFIXED64 => $runtime->reflection('SFixed64T::T'), + Type::TYPE_SINT64 => $runtime->reflection('SInt64T::T'), }), default: 0, ), Type::TYPE_BOOL => new TypeDeclaration( phpType: 'bool', - reflectionType: new Literal('Reflection\BoolT::T'), + reflectionType: new Literal($runtime->reflection('BoolT::T')), default: false, ), Type::TYPE_STRING, Type::TYPE_BYTES => new TypeDeclaration( phpType: 'string', reflectionType: new Literal(match ($type) { - Type::TYPE_STRING => 'Reflection\StringT::T', - Type::TYPE_BYTES => 'Reflection\BytesT::T', + Type::TYPE_STRING => $runtime->reflection('StringT::T'), + Type::TYPE_BYTES => $runtime->reflection('BytesT::T'), }), default: '', ), diff --git a/tests/GeneratedCode/samples/runtime_import_alias.php b/tests/GeneratedCode/samples/runtime_import_alias.php new file mode 100644 index 0000000..f4453ac --- /dev/null +++ b/tests/GeneratedCode/samples/runtime_import_alias.php @@ -0,0 +1,58 @@ + new Reflection( + scalar: 'scalar', + numbers: [1, -2, 3], + kind: Kind::KIND_ONE, + item: new Item('item'), + items: RuntimeMap::fromArray(['a' => new Item('a'), 'b' => new Item('b')]), + choice: new Reflection\ChoiceText('text'), + ), + 'message named after the Reflection alias, oneof set to a message' => new Reflection( + choice: new Reflection\ChoiceObject(new Item('object')), + ), + 'message named after the Protobuf alias' => new Protobuf( + counts: RuntimeMap::fromArray(['one' => 1, 'two' => 2]), + name: 'protobuf', + ), + 'nested message named after the Reflection alias' => new Outer( + inner: new Outer\Reflection('a', RuntimeMap::fromArray([1 => 'one', 2 => 'two'])), + ), + 'oneof variant differing from the Reflection alias in case' => new Variants( + reflect: new Variants\ReflectIon('ion'), + ), + 'oneof variant next to it' => new Variants( + reflect: new Variants\ReflectOther(7), + ), + 'messages named after runtime classes' => new Holder( + reflection: new Reflection(scalar: 'r'), + protobuf: new Protobuf(counts: RuntimeMap::fromArray(['c' => 3])), + nested: new Outer\Reflection('n'), + field: new Field('field'), + map: new Map(RuntimeMap::fromArray(['k' => 'v'])), + oneOf: new OneOf(new OneOf\ValueI(5)), + stringT: new StringT('string'), + list: [new Reflection(scalar: 'first'), new Reflection(kind: Kind::KIND_ONE)], + byName: RuntimeMap::fromArray(['x' => new Reflection(numbers: [9])]), + ), +]; diff --git a/tests/fixtures/runtime_import_alias/runtime_import_alias.proto b/tests/fixtures/runtime_import_alias/runtime_import_alias.proto new file mode 100644 index 0000000..29d577e --- /dev/null +++ b/tests/fixtures/runtime_import_alias/runtime_import_alias.proto @@ -0,0 +1,87 @@ +syntax = "proto3"; + +package runtime.alias; + +// The generated classes import the runtime as `use Thesis\Protobuf\Reflection;` (and `use Thesis\Protobuf;` for +// map fields) and refer to it through that alias. A class with the same short name takes the alias, so the import +// gets another one, and the references have to follow it. + +enum Kind { + KIND_UNSPECIFIED = 0; + KIND_ONE = 1; +} + +message Item { + string name = 1; +} + +// Takes the short name of `use Thesis\Protobuf\Reflection`. +message Reflection { + string scalar = 1; + repeated int32 numbers = 2; + Kind kind = 3; + Item item = 4; + map items = 5; + + oneof choice { + string text = 6; + Item object = 7; + } +} + +// Takes the short name of `use Thesis\Protobuf`, which only map fields import. +message Protobuf { + map counts = 1; + string name = 2; +} + +// A nested class is declared in a namespace of its own, the parent keeps the plain import. +message Outer { + message Reflection { + string a = 1; + map m = 2; + } + + Reflection inner = 1; +} + +// The variant class ReflectIon differs from the alias Reflection only in case, which PHP does not tell apart. +message Variants { + oneof reflect { + string ion = 1; + int32 other = 2; + } +} + +// Named after classes referred to through the alias, not after the alias itself: no conflict. +message Field { + string a = 1; +} + +message Map { + map m = 1; +} + +message OneOf { + oneof value { + string s = 1; + int32 i = 2; + } +} + +message StringT { + string s = 1; +} + +// Refers to the messages above as field types, by fully qualified name. +message Holder { + Reflection reflection = 1; + Protobuf protobuf = 2; + Outer.Reflection nested = 3; + Field field = 4; + Map map = 5; + OneOf one_of = 6; + StringT string_t = 7; + repeated Reflection list = 8; + map by_name = 9; +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/DescriptorRegistry.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/DescriptorRegistry.php new file mode 100644 index 0000000..4f73d5f --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/DescriptorRegistry.php @@ -0,0 +1,49 @@ +add(Registry\Descriptor::base64(self::RUNTIME_IMPORT_ALIAS_DESCRIPTOR_BUFFER), new File( + name: 'runtime_import_alias.proto', + messages: [ + new File\MessageDescriptor('runtime.alias.Item', \Runtime\Alias\Item::class), + new File\MessageDescriptor('runtime.alias.Reflection', \Runtime\Alias\Reflection::class), + new File\MessageDescriptor('runtime.alias.Protobuf', \Runtime\Alias\Protobuf::class), + new File\MessageDescriptor('runtime.alias.Outer', \Runtime\Alias\Outer::class), + new File\MessageDescriptor('runtime.alias.Outer.Reflection', \Runtime\Alias\Outer\Reflection::class), + new File\MessageDescriptor('runtime.alias.Variants', \Runtime\Alias\Variants::class), + new File\MessageDescriptor('runtime.alias.Field', \Runtime\Alias\Field::class), + new File\MessageDescriptor('runtime.alias.Map', \Runtime\Alias\Map::class), + new File\MessageDescriptor('runtime.alias.OneOf', \Runtime\Alias\OneOf::class), + new File\MessageDescriptor('runtime.alias.StringT', \Runtime\Alias\StringT::class), + new File\MessageDescriptor('runtime.alias.Holder', \Runtime\Alias\Holder::class), + ], + enums: [ + new File\EnumDescriptor('runtime.alias.Kind', \Runtime\Alias\Kind::class), + ], + )); + } +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/Field.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/Field.php new file mode 100644 index 0000000..9dff944 --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/Field.php @@ -0,0 +1,28 @@ + $list + * @param Protobuf\Map $byName + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ObjectT(\Runtime\Alias\Reflection::class))] + public ?\Runtime\Alias\Reflection $reflection = null, + #[Reflection\Field(2, new Reflection\ObjectT(\Runtime\Alias\Protobuf::class))] + public ?\Runtime\Alias\Protobuf $protobuf = null, + #[Reflection\Field(3, new Reflection\ObjectT(\Runtime\Alias\Outer\Reflection::class))] + public ?\Runtime\Alias\Outer\Reflection $nested = null, + #[Reflection\Field(4, new Reflection\ObjectT(\Runtime\Alias\Field::class))] + public ?\Runtime\Alias\Field $field = null, + #[Reflection\Field(5, new Reflection\ObjectT(\Runtime\Alias\Map::class))] + public ?\Runtime\Alias\Map $map = null, + #[Reflection\Field(6, new Reflection\ObjectT(\Runtime\Alias\OneOf::class))] + public ?\Runtime\Alias\OneOf $oneOf = null, + #[Reflection\Field(7, new Reflection\ObjectT(\Runtime\Alias\StringT::class))] + public ?\Runtime\Alias\StringT $stringT = null, + #[Reflection\Field(8, new Reflection\ListT(new Reflection\ObjectT(\Runtime\Alias\Reflection::class)))] + public array $list = [], + #[Reflection\Field(9, new Reflection\MapT(Reflection\StringT::T, new Reflection\ObjectT(\Runtime\Alias\Reflection::class)))] + public Protobuf\Map $byName = new Protobuf\Map(), + ) {} +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/Item.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/Item.php new file mode 100644 index 0000000..0cc01e4 --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/Item.php @@ -0,0 +1,26 @@ + $m + */ + public function __construct( + #[Reflection\Field(1, new Reflection\MapT(Reflection\StringT::T, Reflection\StringT::T))] + public Protobuf\Map $m = new Protobuf\Map(), + ) {} +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/OneOf.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/OneOf.php new file mode 100644 index 0000000..511f806 --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/OneOf.php @@ -0,0 +1,26 @@ + $m + */ + public function __construct( + #[Reflection1\Field(1, Reflection1\StringT::T)] + public string $a = '', + #[Reflection1\Field(2, new Reflection1\MapT(Reflection1\Int32T::T, Reflection1\StringT::T))] + public Protobuf\Map $m = new Protobuf\Map(), + ) {} +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/Protobuf.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/Protobuf.php new file mode 100644 index 0000000..048d97a --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/Protobuf.php @@ -0,0 +1,34 @@ + $counts + */ + public function __construct( + #[Reflection\Field(1, new Reflection\MapT(Reflection\StringT::T, Reflection\Int32T::T))] + public Protobuf1\Map $counts = new Protobuf1\Map(), + #[Reflection\Field(2, Reflection\StringT::T)] + public string $name = '', + ) {} +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection.php new file mode 100644 index 0000000..e1e873a --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection.php @@ -0,0 +1,43 @@ + $numbers + * @param Protobuf\Map $items + */ + public function __construct( + #[Reflection1\Field(1, Reflection1\StringT::T)] + public string $scalar = '', + #[Reflection1\Field(2, new Reflection1\ListT(Reflection1\Int32T::T))] + public array $numbers = [], + #[Reflection1\Field(3, new Reflection1\EnumT(\Runtime\Alias\Kind::class))] + public \Runtime\Alias\Kind $kind = \Runtime\Alias\Kind::KIND_UNSPECIFIED, + #[Reflection1\Field(4, new Reflection1\ObjectT(\Runtime\Alias\Item::class))] + public ?\Runtime\Alias\Item $item = null, + #[Reflection1\Field(5, new Reflection1\MapT(Reflection1\StringT::T, new Reflection1\ObjectT(\Runtime\Alias\Item::class)))] + public Protobuf\Map $items = new Protobuf\Map(), + #[Reflection1\OneOf([\Runtime\Alias\Reflection\ChoiceText::class, \Runtime\Alias\Reflection\ChoiceObject::class])] + public ?\Runtime\Alias\Reflection\Choice $choice = null, + ) {} +} diff --git a/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection/Choice.php b/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection/Choice.php new file mode 100644 index 0000000..3cfd093 --- /dev/null +++ b/tests/snapshots/runtime_import_alias/Runtime/Alias/Reflection/Choice.php @@ -0,0 +1,22 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Runtime\Alias\DescriptorRegistry()), +);