diff --git a/src/Plugin/Generator/PhpNamespacer.php b/src/Plugin/Generator/PhpNamespacer.php index 5a6812c..bcf5dec 100644 --- a/src/Plugin/Generator/PhpNamespacer.php +++ b/src/Plugin/Generator/PhpNamespacer.php @@ -29,13 +29,14 @@ public function fqcn(string $path): string $paths = explode('.', $path); $name = $paths[\count($paths) - 1]; - return Naming::joinNamespace([ - '', - $this->resolve($path), - $name, - ]); + return '\\' . $this->resolve($path) . '\\' . Naming::pascalCase($name); } + /** + * Only the protobuf segments of the type path are normalised. The namespace of the file + * is already a PHP namespace, possibly a user-defined one ("Foo_Bar\\baz"), so it is kept + * verbatim: nested types must land in the very namespace the top-level ones are declared in. + */ private function resolve(string $path): string { $rewritten = $this->rewrite($path); @@ -51,10 +52,7 @@ private function resolve(string $path): string $typeNamespace = \array_slice($paths, 0, \count($paths) - 1); if (\count($typeNamespace) > 0) { - return Naming::joinNamespace([ - $this->namespace, - ...$typeNamespace, - ]); + return $this->namespace . '\\' . Naming::joinNamespace($typeNamespace); } return $this->namespace; diff --git a/tests/GeneratedCode/samples/php_namespace_verbatim.php b/tests/GeneratedCode/samples/php_namespace_verbatim.php new file mode 100644 index 0000000..6ea6019 --- /dev/null +++ b/tests/GeneratedCode/samples/php_namespace_verbatim.php @@ -0,0 +1,38 @@ + new Outer( + inner: new Outer\Inner(new Outer\Inner\Deep('deep')), + kind: Outer\Kind::KIND_ONE, + choice: new Outer\ChoiceNested(new Outer\Inner(new Outer\Inner\Deep('variant'))), + ), + 'enum oneof variant' => new Outer(choice: new Outer\ChoiceKnown(Outer\Kind::KIND_ONE)), + 'scalar oneof variant' => new Outer(choice: new Outer\ChoiceText('text')), + 'nested types referenced from another message' => new Holder( + inner: new Outer\Inner(new Outer\Inner\Deep('a')), + deep: new Outer\Inner\Deep('b'), + kind: Outer\Kind::KIND_ONE, + list: [new Outer\Inner(), new Outer\Inner(new Outer\Inner\Deep('c'))], + byName: Map::fromArray(['d' => new Outer\Inner\Deep('d')]), + ), + 'lowercase php_namespace referencing nested types of another file' => new Envelope( + body: new Envelope\Body(new Outer\Inner(new Outer\Inner\Deep('body'))), + payload: new Envelope\PayloadInlineBody(new Envelope\Body()), + ), + 'lowercase php_namespace with an enum variant of another file' => new Envelope( + payload: new Envelope\PayloadKind(Outer\Kind::KIND_ONE), + ), +]; diff --git a/tests/Plugin/Generator/PhpNamespacerTest.php b/tests/Plugin/Generator/PhpNamespacerTest.php new file mode 100644 index 0000000..b7cf506 --- /dev/null +++ b/tests/Plugin/Generator/PhpNamespacerTest.php @@ -0,0 +1,30 @@ +create($path)->getName()); + self::assertSame($expectedFqcn, $namespacer->fqcn($path)); + } +} diff --git a/tests/fixtures/php_namespace_verbatim/lowercase.proto b/tests/fixtures/php_namespace_verbatim/lowercase.proto new file mode 100644 index 0000000..db6648a --- /dev/null +++ b/tests/fixtures/php_namespace_verbatim/lowercase.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +package php_namespace.verbatim.lowercase; + +import "underscore.proto"; + +// A php_namespace starting with a lowercase letter keeps its case for nested types as well. +option php_namespace = "verbatim\\Lower_Case"; + +message Envelope { + message Body { + php_namespace.verbatim.underscore.Outer.Inner inner = 1; + } + + Body body = 1; + + oneof payload { + Body inline_body = 2; + php_namespace.verbatim.underscore.Outer.Kind kind = 3; + } +} diff --git a/tests/fixtures/php_namespace_verbatim/underscore.proto b/tests/fixtures/php_namespace_verbatim/underscore.proto new file mode 100644 index 0000000..d82b76b --- /dev/null +++ b/tests/fixtures/php_namespace_verbatim/underscore.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +package php_namespace.verbatim.underscore; + +// A php_namespace is already a PHP namespace and is used as is: nested types, oneof interfaces and their +// variants are generated into Verbatim_Ns\under_score\…, next to the top-level types, not into VerbatimNs\… +option php_namespace = "Verbatim_Ns\\under_score"; + +message Outer { + message Inner { + message Deep { + string value = 1; + } + + Deep deep = 1; + } + + enum Kind { + KIND_UNSPECIFIED = 0; + KIND_ONE = 1; + } + + Inner inner = 1; + Kind kind = 2; + + oneof choice { + string text = 3; + Inner nested = 4; + Kind known = 5; + } +} + +message Holder { + Outer.Inner inner = 1; + Outer.Inner.Deep deep = 2; + Outer.Kind kind = 3; + repeated Outer.Inner list = 4; + map by_name = 5; +} + +service Lookup { + rpc Find(Outer.Inner) returns (Outer.Inner.Deep); +} diff --git a/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/DescriptorRegistry.php b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/DescriptorRegistry.php new file mode 100644 index 0000000..72120a9 --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/DescriptorRegistry.php @@ -0,0 +1,50 @@ +add(Registry\Descriptor::base64(self::UNDERSCORE_DESCRIPTOR_BUFFER), new File( + name: 'underscore.proto', + messages: [ + new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer', \Verbatim_Ns\under_score\Outer::class), + new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer.Inner', \Verbatim_Ns\under_score\Outer\Inner::class), + new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer.Inner.Deep', \Verbatim_Ns\under_score\Outer\Inner\Deep::class), + new File\MessageDescriptor('php_namespace.verbatim.underscore.Holder', \Verbatim_Ns\under_score\Holder::class), + ], + enums: [ + new File\EnumDescriptor('php_namespace.verbatim.underscore.Outer.Kind', \Verbatim_Ns\under_score\Outer\Kind::class), + ], + services: [ + new File\ServiceDescriptor( + name: 'php_namespace.verbatim.underscore.Lookup', + methods: [ + new File\MethodDescriptor('Find', false, false), + ], + ), + ], + )); + } +} diff --git a/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Holder.php b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Holder.php new file mode 100644 index 0000000..91e6b41 --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Holder.php @@ -0,0 +1,39 @@ + $list + * @param Protobuf\Map $byName + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner::class))] + public ?\Verbatim_Ns\under_score\Outer\Inner $inner = null, + #[Reflection\Field(2, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner\Deep::class))] + public ?\Verbatim_Ns\under_score\Outer\Inner\Deep $deep = null, + #[Reflection\Field(3, new Reflection\EnumT(\Verbatim_Ns\under_score\Outer\Kind::class))] + public \Verbatim_Ns\under_score\Outer\Kind $kind = \Verbatim_Ns\under_score\Outer\Kind::KIND_UNSPECIFIED, + #[Reflection\Field(4, new Reflection\ListT(new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner::class)))] + public array $list = [], + #[Reflection\Field(5, new Reflection\MapT(Reflection\StringT::T, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner\Deep::class)))] + public Protobuf\Map $byName = new Protobuf\Map(), + ) {} +} diff --git a/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupClient.php b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupClient.php new file mode 100644 index 0000000..3148739 --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupClient.php @@ -0,0 +1,55 @@ + $invoke */ + $invoke = new Client\Invoke( + method: '/php_namespace.verbatim.underscore.Lookup/Find', + output: \Verbatim_Ns\under_score\Outer\Inner\Deep::class, + type: Grpc\RpcType::Unary, + ); + + return $this->client->invoke( + request: $request, + invoke: $invoke, + md: $md, + cancellation: $cancellation, + ); + } +} diff --git a/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupServer.php b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupServer.php new file mode 100644 index 0000000..a51f70c --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/LookupServer.php @@ -0,0 +1,28 @@ +server->find(...)), + type: Grpc\RpcType::Unary, + ), + ]); + } +} diff --git a/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Outer.php b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Outer.php new file mode 100644 index 0000000..ae57f7d --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/Verbatim_Ns/under_score/Outer.php @@ -0,0 +1,34 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Verbatim_Ns\under_score\DescriptorRegistry()), +); diff --git a/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/DescriptorRegistry.php b/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/DescriptorRegistry.php new file mode 100644 index 0000000..f97889d --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/DescriptorRegistry.php @@ -0,0 +1,40 @@ +add(Registry\Descriptor::base64(self::LOWERCASE_DESCRIPTOR_BUFFER), new File( + name: 'lowercase.proto', + dependencies: [ + 'underscore.proto', + ], + messages: [ + new File\MessageDescriptor('php_namespace.verbatim.lowercase.Envelope', \verbatim\Lower_Case\Envelope::class), + new File\MessageDescriptor('php_namespace.verbatim.lowercase.Envelope.Body', \verbatim\Lower_Case\Envelope\Body::class), + ], + )); + } +} diff --git a/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/Envelope.php b/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/Envelope.php new file mode 100644 index 0000000..a65705e --- /dev/null +++ b/tests/snapshots/php_namespace_verbatim/verbatim/Lower_Case/Envelope.php @@ -0,0 +1,28 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \verbatim\Lower_Case\DescriptorRegistry()), +);