|
1 | 1 | package php |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "slices" |
4 | 5 | "strings" |
5 | 6 |
|
6 | 7 | "google.golang.org/protobuf/compiler/protogen" |
7 | 8 | "google.golang.org/protobuf/reflect/protoreflect" |
8 | 9 | "google.golang.org/protobuf/types/descriptorpb" |
9 | 10 | ) |
10 | 11 |
|
| 12 | +// Code in this file is based on the original protoc compiler code for PHP: |
| 13 | +// https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/compiler/php/names.cc |
| 14 | + |
11 | 15 | // Reserved PHP keywords that must be prefixed with something. |
12 | 16 | var reservedNames = []string{ |
13 | 17 | "abstract", "and", "array", "as", "break", |
@@ -43,16 +47,7 @@ func classNamePrefix(className string, file protoreflect.FileDescriptor) string |
43 | 47 | } |
44 | 48 |
|
45 | 49 | // Check if the class name matches a reserved name |
46 | | - isReserved := false |
47 | | - for _, name := range reservedNames { |
48 | | - if name == strings.ToLower(className) { |
49 | | - isReserved = true |
50 | | - |
51 | | - break |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - if isReserved { |
| 50 | + if slices.Contains(reservedNames, strings.ToLower(className)) { |
56 | 51 | // Google internal classes receive a different prefix |
57 | 52 | if file.Package() == "google.protobuf" { |
58 | 53 | return "GPB" |
@@ -85,12 +80,12 @@ func namespace(file protoreflect.FileDescriptor) string { |
85 | 80 |
|
86 | 81 | // Path guesses the path of the file based on the (internally calculated) namespace. |
87 | 82 | func Path(file *protogen.File) string { |
88 | | - return strings.Replace(Namespace(file), "\\", "/", -1) |
| 83 | + return strings.ReplaceAll(Namespace(file), "\\", "/") |
89 | 84 | } |
90 | 85 |
|
91 | 86 | // PathFromNamespace guesses the path of the file based on the namespace. |
92 | 87 | func PathFromNamespace(ns string) string { |
93 | | - return strings.Replace(ns, "\\", "/", -1) |
| 88 | + return strings.ReplaceAll(ns, "\\", "/") |
94 | 89 | } |
95 | 90 |
|
96 | 91 | // Name generates a name from a proto reference. |
@@ -130,8 +125,12 @@ func ServiceName(file *protogen.File, svc *protogen.Service) string { |
130 | 125 |
|
131 | 126 | // MessageName transforms a message name into a PHP compatible one. |
132 | 127 | func MessageName(file *protogen.File, message *protogen.Message) string { |
133 | | - className := string(message.Desc.Name()) |
134 | 128 | parentFile := message.Desc.ParentFile() |
| 129 | + className := classNamePrefix(string(message.Desc.Name()), parentFile) + string(message.Desc.Name()) |
| 130 | + |
| 131 | + for parent, ok := message.Desc.Parent().(protoreflect.MessageDescriptor); ok && parent != nil; parent, ok = parent.Parent().(protoreflect.MessageDescriptor) { |
| 132 | + className = classNamePrefix(string(parent.Name()), parentFile) + string(parent.Name()) + "\\" + className |
| 133 | + } |
135 | 134 |
|
136 | | - return "\\" + namespacedName(classNamePrefix(className, parentFile)+className, parentFile) |
| 135 | + return "\\" + namespacedName(className, parentFile) |
137 | 136 | } |
0 commit comments