• 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     if (method->server_streaming()) {
82       vars["return_type_id"] = "\\Grpc\\BidiStreamingCall";
83     } else {
84       vars["return_type_id"] = "\\Grpc\\ClientStreamingCall";
85     }
86     out->Print(vars,
87                " * @param array $$metadata metadata\n"
88                " * @param array $$options call options\n"
89                " * @return $return_type_id$\n */\n"
90                "public function $name$($$metadata = [], "
91                "$$options = []) {\n");
92     out->Indent();
93     out->Indent();
94     if (method->server_streaming()) {
95       out->Print("return $$this->_bidiRequest(");
96     } else {
97       out->Print("return $$this->_clientStreamRequest(");
98     }
99     out->Print(vars,
100                "'/$service_name$/$name$',\n"
101                "['\\$output_type_id$','decode'],\n"
102                "$$metadata, $$options);\n");
103   } else {
104     if (method->server_streaming()) {
105       vars["return_type_id"] = "\\Grpc\\ServerStreamingCall";
106     } else {
107       vars["return_type_id"] = "\\Grpc\\UnaryCall";
108     }
109     out->Print(vars,
110                " * @param \\$input_type_id$ $$argument input argument\n"
111                " * @param array $$metadata metadata\n"
112                " * @param array $$options call options\n"
113                " * @return $return_type_id$\n */\n"
114                "public function $name$(\\$input_type_id$ $$argument,\n"
115                "  $$metadata = [], $$options = []) {\n");
116     out->Indent();
117     out->Indent();
118     if (method->server_streaming()) {
119       out->Print("return $$this->_serverStreamRequest(");
120     } else {
121       out->Print("return $$this->_simpleRequest(");
122     }
123     out->Print(vars,
124                "'/$service_name$/$name$',\n"
125                "$$argument,\n"
126                "['\\$output_type_id$', 'decode'],\n"
127                "$$metadata, $$options);\n");
128   }
129   out->Outdent();
130   out->Outdent();
131   out->Print("}\n\n");
132 }
133 
134 // Prints out the service descriptor object
PrintService(const ServiceDescriptor * service,const std::string & class_suffix,Printer * out)135 void PrintService(const ServiceDescriptor* service,
136                   const std::string& class_suffix, Printer* out) {
137   map<std::string, std::string> vars;
138   out->Print("/**\n");
139   out->Print(GetPHPComments(service, " *").c_str());
140   out->Print(" */\n");
141   vars["name"] = GetPHPServiceClassname(service, class_suffix);
142   out->Print(vars, "class $name$ extends \\Grpc\\BaseStub {\n\n");
143   out->Indent();
144   out->Indent();
145   out->Print(
146       "/**\n * @param string $$hostname hostname\n"
147       " * @param array $$opts channel options\n"
148       " * @param \\Grpc\\Channel $$channel (optional) re-use channel "
149       "object\n */\n"
150       "public function __construct($$hostname, $$opts, "
151       "$$channel = null) {\n");
152   out->Indent();
153   out->Indent();
154   out->Print("parent::__construct($$hostname, $$opts, $$channel);\n");
155   out->Outdent();
156   out->Outdent();
157   out->Print("}\n\n");
158   for (int i = 0; i < service->method_count(); i++) {
159     std::string method_name =
160         grpc_generator::LowercaseFirstLetter(service->method(i)->name());
161     PrintMethod(service->method(i), out);
162   }
163   out->Outdent();
164   out->Outdent();
165   out->Print("}\n");
166 }
167 }  // namespace
168 
GenerateFile(const FileDescriptor * file,const ServiceDescriptor * service,const std::string & class_suffix)169 std::string GenerateFile(const FileDescriptor* file,
170                          const ServiceDescriptor* service,
171                          const std::string& class_suffix) {
172   std::string output;
173   {
174     StringOutputStream output_stream(&output);
175     Printer out(&output_stream, '$');
176 
177     out.Print("<?php\n");
178     out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
179 
180     std::string leading_comments = GetPHPComments(file, "//");
181     if (!leading_comments.empty()) {
182       out.Print("// Original file comments:\n");
183       out.PrintRaw(leading_comments.c_str());
184     }
185 
186     map<std::string, std::string> vars;
187     std::string php_namespace = PackageName(file);
188     vars["package"] = php_namespace;
189     out.Print(vars, "namespace $package$;\n\n");
190 
191     PrintService(service, class_suffix, &out);
192   }
193   return output;
194 }
195 
196 }  // namespace grpc_php_generator
197