diff options
Diffstat (limited to 'plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php')
4 files changed, 0 insertions, 402 deletions
diff --git a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go deleted file mode 100644 index 03c48ac8..00000000 --- a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/generate.go +++ /dev/null @@ -1,57 +0,0 @@ -// MIT License -// -// Copyright (c) 2018 SpiralScout -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -package php - -import ( - desc "google.golang.org/protobuf/types/descriptorpb" - plugin "google.golang.org/protobuf/types/pluginpb" -) - -// Generate generates needed service classes -func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse { - resp := &plugin.CodeGeneratorResponse{} - - for _, file := range req.ProtoFile { - for _, service := range file.Service { - resp.File = append(resp.File, generate(req, file, service)) - } - } - - return resp -} - -func generate( - req *plugin.CodeGeneratorRequest, - file *desc.FileDescriptorProto, - service *desc.ServiceDescriptorProto, -) *plugin.CodeGeneratorResponse_File { - return &plugin.CodeGeneratorResponse_File{ - Name: str(filename(file, service.Name)), - Content: str(body(req, file, service)), - } -} - -// helper to convert string into string pointer -func str(str string) *string { - return &str -} diff --git a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go deleted file mode 100644 index 32579e33..00000000 --- a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/keywords.go +++ /dev/null @@ -1,139 +0,0 @@ -// MIT License -// -// Copyright (c) 2018 SpiralScout -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -package php - -import ( - "bytes" - "strings" - "unicode" -) - -// @see https://github.com/protocolbuffers/protobuf/blob/master/php/ext/google/protobuf/protobuf.c#L168 -var reservedKeywords = []string{ - "abstract", "and", "array", "as", "break", - "callable", "case", "catch", "class", "clone", - "const", "continue", "declare", "default", "die", - "do", "echo", "else", "elseif", "empty", - "enddeclare", "endfor", "endforeach", "endif", "endswitch", - "endwhile", "eval", "exit", "extends", "final", - "for", "foreach", "function", "global", "goto", - "if", "implements", "include", "include_once", "instanceof", - "insteadof", "interface", "isset", "list", "namespace", - "new", "or", "print", "private", "protected", - "public", "require", "require_once", "return", "static", - "switch", "throw", "trait", "try", "unset", - "use", "var", "while", "xor", "int", - "float", "bool", "string", "true", "false", - "null", "void", "iterable", -} - -// Check if given name/keyword is reserved by php. -func isReserved(name string) bool { - name = strings.ToLower(name) - for _, k := range reservedKeywords { - if name == k { - return true - } - } - - return false -} - -// generate php namespace or path -func namespace(pkg *string, sep string) string { - if pkg == nil { - return "" - } - - result := bytes.NewBuffer(nil) - for _, p := range strings.Split(*pkg, ".") { - result.WriteString(identifier(p, "")) - result.WriteString(sep) - } - - return strings.Trim(result.String(), sep) -} - -// create php identifier for class or message -func identifier(name string, suffix string) string { - name = Camelize(name) - if suffix != "" { - return name + Camelize(suffix) - } - - return name -} - -func resolveReserved(identifier string, pkg string) string { - if isReserved(strings.ToLower(identifier)) { - if pkg == ".google.protobuf" { - return "GPB" + identifier - } - return "PB" + identifier - } - - return identifier -} - -// Camelize "dino_party" -> "DinoParty" -func Camelize(word string) string { - words := splitAtCaseChangeWithTitlecase(word) - return strings.Join(words, "") -} - -func splitAtCaseChangeWithTitlecase(s string) []string { - words := make([]string, 0) - word := make([]rune, 0) - for _, c := range s { - spacer := isSpacerChar(c) - if len(word) > 0 { - if unicode.IsUpper(c) || spacer { - words = append(words, string(word)) - word = make([]rune, 0) - } - } - if !spacer { - if len(word) > 0 { - word = append(word, unicode.ToLower(c)) - } else { - word = append(word, unicode.ToUpper(c)) - } - } - } - words = append(words, string(word)) - return words -} - -func isSpacerChar(c rune) bool { - switch { - case c == rune("_"[0]): - return true - case c == rune(" "[0]): - return true - case c == rune(":"[0]): - return true - case c == rune("-"[0]): - return true - } - return false -} diff --git a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go deleted file mode 100644 index c1dc3898..00000000 --- a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/ns.go +++ /dev/null @@ -1,103 +0,0 @@ -package php - -import ( - "bytes" - "fmt" - "strings" - - desc "google.golang.org/protobuf/types/descriptorpb" - plugin "google.golang.org/protobuf/types/pluginpb" -) - -// manages internal name representation of the package -type ns struct { - // Package defines file package. - Package string - - // Root namespace of the package - Namespace string - - // Import declares what namespaces to be imported - Import map[string]string -} - -// newNamespace creates new work namespace. -func newNamespace(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, service *desc.ServiceDescriptorProto) *ns { - ns := &ns{ - Package: *file.Package, - Namespace: namespace(file.Package, "\\"), - Import: make(map[string]string), - } - - if file.Options != nil && file.Options.PhpNamespace != nil { - ns.Namespace = *file.Options.PhpNamespace - } - - for k := range service.Method { - ns.importMessage(req, service.Method[k].InputType) - ns.importMessage(req, service.Method[k].OutputType) - } - - return ns -} - -// importMessage registers new import message namespace (only the namespace). -func (ns *ns) importMessage(req *plugin.CodeGeneratorRequest, msg *string) { - if msg == nil { - return - } - - chunks := strings.Split(*msg, ".") - pkg := strings.Join(chunks[:len(chunks)-1], ".") - - result := bytes.NewBuffer(nil) - for _, p := range chunks[:len(chunks)-1] { - result.WriteString(identifier(p, "")) - result.WriteString(`\`) - } - - if pkg == "."+ns.Package { - // root package - return - } - - for _, f := range req.ProtoFile { - if pkg == "."+*f.Package { - if f.Options != nil && f.Options.PhpNamespace != nil { - // custom imported namespace - ns.Import[pkg] = *f.Options.PhpNamespace - return - } - } - } - - ns.Import[pkg] = strings.Trim(result.String(), `\`) -} - -// resolve message alias -func (ns *ns) resolve(msg *string) string { - chunks := strings.Split(*msg, ".") - pkg := strings.Join(chunks[:len(chunks)-1], ".") - - if pkg == "."+ns.Package { - // root message - return identifier(chunks[len(chunks)-1], "") - } - - for iPkg, ns := range ns.Import { - if pkg == iPkg { - // use last namespace chunk - nsChunks := strings.Split(ns, `\`) - identifier := identifier(chunks[len(chunks)-1], "") - - return fmt.Sprintf( - `%s\%s`, - nsChunks[len(nsChunks)-1], - resolveReserved(identifier, pkg), - ) - } - } - - // fully clarified name (fallback) - return "\\" + namespace(msg, "\\") -} diff --git a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go b/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go deleted file mode 100644 index e00c6fdd..00000000 --- a/plugins/grpc/protoc_plugins/protoc-gen-php-grpc/php/template.go +++ /dev/null @@ -1,103 +0,0 @@ -// MIT License -// -// Copyright (c) 2018 SpiralScout -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -package php - -import ( - "bytes" - "fmt" - "strings" - "text/template" - - desc "google.golang.org/protobuf/types/descriptorpb" - plugin "google.golang.org/protobuf/types/pluginpb" -) - -const phpBody = `<?php -# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT! -# source: {{ .File.Name }} -{{ $ns := .Namespace -}} -{{if $ns.Namespace}} -namespace {{ $ns.Namespace }}; -{{end}} -use Spiral\GRPC; -{{- range $n := $ns.Import}} -use {{ $n }}; -{{- end}} - -interface {{ .Service.Name | interface }} extends GRPC\ServiceInterface -{ - // GRPC specific service name. - public const NAME = "{{ .File.Package }}.{{ .Service.Name }}";{{ "\n" }} -{{- range $m := .Service.Method}} - /** - * @param GRPC\ContextInterface $ctx - * @param {{ name $ns $m.InputType }} $in - * @return {{ name $ns $m.OutputType }} - * - * @throws GRPC\Exception\InvokeException - */ - public function {{ $m.Name }}(GRPC\ContextInterface $ctx, {{ name $ns $m.InputType }} $in): {{ name $ns $m.OutputType }}; -{{end -}} -} -` - -// generate php filename -func filename(file *desc.FileDescriptorProto, name *string) string { - ns := namespace(file.Package, "/") - if file.Options != nil && file.Options.PhpNamespace != nil { - ns = strings.ReplaceAll(*file.Options.PhpNamespace, `\`, `/`) - } - - return fmt.Sprintf("%s/%s.php", ns, identifier(*name, "interface")) -} - -// generate php file body -func body(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, service *desc.ServiceDescriptorProto) string { - out := bytes.NewBuffer(nil) - - data := struct { - Namespace *ns - File *desc.FileDescriptorProto - Service *desc.ServiceDescriptorProto - }{ - Namespace: newNamespace(req, file, service), - File: file, - Service: service, - } - - tpl := template.Must(template.New("phpBody").Funcs(template.FuncMap{ - "interface": func(name *string) string { - return identifier(*name, "interface") - }, - "name": func(ns *ns, name *string) string { - return ns.resolve(name) - }, - }).Parse(phpBody)) - - err := tpl.Execute(out, data) - if err != nil { - panic(err) - } - - return out.String() -} |