• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <map>
20 
21 #include <google/protobuf/compiler/php/php_generator.h>
22 #include "src/compiler/config.h"
23 #include "src/compiler/generator_helpers.h"
24 #include "src/compiler/php_generator_helpers.h"
25 
26 using google::protobuf::compiler::php::GeneratedClassName;
27 using grpc::protobuf::Descriptor;
28 using grpc::protobuf::FileDescriptor;
29 using grpc::protobuf::MethodDescriptor;
30 using grpc::protobuf::ServiceDescriptor;
31 using grpc::protobuf::io::Printer;
32 using grpc::protobuf::io::StringOutputStream;
33 using std::map;
34 
35 namespace grpc_php_generator {
36 namespace {
37 
ConvertToPhpNamespace(const std::string & name)38 std::string ConvertToPhpNamespace(const std::string& name) {
39   std::vector<std::string> tokens = grpc_generator::tokenize(name, ".");
40   std::ostringstream oss;
41   for (unsigned int i = 0; i < tokens.size(); i++) {
42     oss << (i == 0 ? "" : "\\")
43         << grpc_generator::CapitalizeFirstLetter(tokens[i]);
44   }
45   return oss.str();
46 }
47 
PackageName(const FileDescriptor * file)48 std::string PackageName(const FileDescriptor* file) {
49   if (file->options().has_php_namespace()) {
50     return file->options().php_namespace();
51   } else {
52     return ConvertToPhpNamespace(file->package());
53   }
54 }
55 
MessageIdentifierName(const std::string & name,const FileDescriptor * file)56 std::string MessageIdentifierName(const std::string& name,
57                                   const FileDescriptor* file) {
58   std::vector<std::string> tokens = grpc_generator::tokenize(name, ".");
59   std::ostringstream oss;
60   if (PackageName(file) != "") {
61     oss << PackageName(file) << "\\";
62   }
63   oss << grpc_generator::CapitalizeFirstLetter(tokens[tokens.size() - 1]);
64   return oss.str();
65 }
66 
PrintMethod(const MethodDescriptor * method,Printer * out)67 void PrintMethod(const MethodDescriptor* method, Printer* out) {
68   const Descriptor* input_type = method->input_type();
69   const Descriptor* output_type = method->output_type();
70   map<std::string, std::string> vars;
71   vars["service_name"] = method->service()->full_name();
72   vars["name"] = method->name();
73   vars["input_type_id"] =
74       MessageIdentifierName(GeneratedClassName(input_type), input_type->file());
75   vars["output_type_id"] = MessageIdentifierName(
76       GeneratedClassName(output_type), output_type->file());
77 
78   out->Print("/**\n");
79   out->Print(GetPHPComments(method, " *").c_str());
80   if (method->client_streaming()) {
81     out->Print(vars,
82                " * @param array $$metadata metadata\n"
83                " * @param array $$options call options\n"
84                " * @return \\$output_type_id$\n */\n"
85                "public function $name$($$metadata = [], "
86                "$$options = []) {\n");
87     out->Indent();
88     out->Indent();
89     if (method->server_streaming()) {
90       out->Print("return $$this->_bidiRequest(");
91     } else {
92       out->Print("return $$this->_clientStreamRequest(");
93     }
94     out->Print(vars,
95                "'/$service_name$/$name$',\n"
96                "['\\$output_type_id$','decode'],\n"
97                "$$metadata, $$options);\n");
98   } else {
99     out->Print(vars,
100                " * @param \\$input_type_id$ $$argument input argument\n"
101                " * @param array $$metadata metadata\n"
102                " * @param array $$options call options\n"
103                " * @return \\$output_type_id$\n */\n"
104                "public function $name$(\\$input_type_id$ $$argument,\n"
105                "  $$metadata = [], $$options = []) {\n");
106     out->Indent();
107     out->Indent();
108     if (method->server_streaming()) {
109       out->Print("return $$this->_serverStreamRequest(");
110     } else {
111       out->Print("return $$this->_simpleRequest(");
112     }
113     out->Print(vars,
114                "'/$service_name$/$name$',\n"
115                "$$argument,\n"
116                "['\\$output_type_id$', 'decode'],\n"
117                "$$metadata, $$options);\n");
118   }
119   out->Outdent();
120   out->Outdent();
121   out->Print("}\n\n");
122 }
123 
124 // Prints out the service descriptor object
PrintService(const ServiceDescriptor * service,const std::string & class_suffix,Printer * out)125 void PrintService(const ServiceDescriptor* service,
126                   const std::string& class_suffix, Printer* out) {
127   map<std::string, std::string> vars;
128   out->Print("/**\n");
129   out->Print(GetPHPComments(service, " *").c_str());
130   out->Print(" */\n");
131   vars["name"] = GetPHPServiceClassname(service, class_suffix);
132   out->Print(vars, "class $name$ extends \\Grpc\\BaseStub {\n\n");
133   out->Indent();
134   out->Indent();
135   out->Print(
136       "/**\n * @param string $$hostname hostname\n"
137       " * @param array $$opts channel options\n"
138       " * @param \\Grpc\\Channel $$channel (optional) re-use channel "
139       "object\n */\n"
140       "public function __construct($$hostname, $$opts, "
141       "$$channel = null) {\n");
142   out->Indent();
143   out->Indent();
144   out->Print("parent::__construct($$hostname, $$opts, $$channel);\n");
145   out->Outdent();
146   out->Outdent();
147   out->Print("}\n\n");
148   for (int i = 0; i < service->method_count(); i++) {
149     std::string method_name =
150         grpc_generator::LowercaseFirstLetter(service->method(i)->name());
151     PrintMethod(service->method(i), out);
152   }
153   out->Outdent();
154   out->Outdent();
155   out->Print("}\n");
156 }
157 }  // namespace
158 
GenerateFile(const FileDescriptor * file,const ServiceDescriptor * service,const std::string & class_suffix)159 std::string GenerateFile(const FileDescriptor* file,
160                          const ServiceDescriptor* service,
161                          const std::string& class_suffix) {
162   std::string output;
163   {
164     StringOutputStream output_stream(&output);
165     Printer out(&output_stream, '$');
166 
167     out.Print("<?php\n");
168     out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
169 
170     std::string leading_comments = GetPHPComments(file, "//");
171     if (!leading_comments.empty()) {
172       out.Print("// Original file comments:\n");
173       out.PrintRaw(leading_comments.c_str());
174     }
175 
176     map<std::string, std::string> vars;
177     std::string php_namespace = PackageName(file);
178     vars["package"] = php_namespace;
179     out.Print(vars, "namespace $package$;\n\n");
180 
181     PrintService(service, class_suffix, &out);
182   }
183   return output;
184 }
185 
186 }  // namespace grpc_php_generator
187