21 #include <google/protobuf/compiler/php/php_generator.h>
41 std::ostringstream oss;
42 for (
unsigned int i = 0;
i < tokens.size();
i++) {
43 oss << (
i == 0 ?
"" :
"\\")
50 if (
file->options().has_php_namespace()) {
51 return file->options().php_namespace();
53 return ConvertToPhpNamespace(
file->package());
60 std::ostringstream oss;
61 if (PackageName(
file) !=
"") {
62 oss << PackageName(
file) <<
"\\";
71 map<std::string, std::string> vars;
72 vars[
"service_name"] =
method->service()->full_name();
73 vars[
"name"] =
method->name();
74 vars[
"input_type_id"] =
76 vars[
"output_type_id"] = MessageIdentifierName(
81 if (
method->client_streaming()) {
82 if (
method->server_streaming()) {
83 vars[
"return_type_id"] =
"\\Grpc\\BidiStreamingCall";
85 vars[
"return_type_id"] =
"\\Grpc\\ClientStreamingCall";
88 " * @param array $$metadata metadata\n"
89 " * @param array $$options call options\n"
90 " * @return $return_type_id$\n */\n"
91 "public function $name$($$metadata = [], "
92 "$$options = []) {\n");
95 if (
method->server_streaming()) {
96 out->Print(
"return $$this->_bidiRequest(");
98 out->Print(
"return $$this->_clientStreamRequest(");
101 "'/$service_name$/$name$',\n"
102 "['\\$output_type_id$','decode'],\n"
103 "$$metadata, $$options);\n");
105 if (
method->server_streaming()) {
106 vars[
"return_type_id"] =
"\\Grpc\\ServerStreamingCall";
108 vars[
"return_type_id"] =
"\\Grpc\\UnaryCall";
111 " * @param \\$input_type_id$ $$argument input argument\n"
112 " * @param array $$metadata metadata\n"
113 " * @param array $$options call options\n"
114 " * @return $return_type_id$\n */\n"
115 "public function $name$(\\$input_type_id$ $$argument,\n"
116 " $$metadata = [], $$options = []) {\n");
119 if (
method->server_streaming()) {
120 out->Print(
"return $$this->_serverStreamRequest(");
122 out->Print(
"return $$this->_simpleRequest(");
125 "'/$service_name$/$name$',\n"
127 "['\\$output_type_id$', 'decode'],\n"
128 "$$metadata, $$options);\n");
136 map<std::string, std::string> vars;
139 vars[
"service_name"] =
method->service()->full_name();
140 vars[
"method_name"] =
method->name();
141 vars[
"input_type_id"] =
143 vars[
"output_type_id"] = MessageIdentifierName(
149 const char* method_template;
150 if (
method->client_streaming() &&
method->server_streaming()) {
152 " * @param \\Grpc\\ServerCallReader $$reader read client request data "
153 "of \\$input_type_id$\n"
154 " * @param \\Grpc\\ServerCallWriter $$writer write response data of "
155 "\\$output_type_id$\n"
156 " * @param \\Grpc\\ServerContext $$context server request context\n"
159 "public function $method_name$(\n"
160 " \\Grpc\\ServerCallReader $$reader,\n"
161 " \\Grpc\\ServerCallWriter $$writer,\n"
162 " \\Grpc\\ServerContext $$context\n"
164 " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
165 " $$writer->finish();\n"
167 }
else if (
method->client_streaming()) {
169 " * @param \\Grpc\\ServerCallReader $$reader read client request data "
170 "of \\$input_type_id$\n"
171 " * @param \\Grpc\\ServerContext $$context server request context\n"
172 " * @return \\$output_type_id$ for response data, null if if error "
174 " * initial metadata (if any) and status (if not ok) should be set "
177 "public function $method_name$(\n"
178 " \\Grpc\\ServerCallReader $$reader,\n"
179 " \\Grpc\\ServerContext $$context\n"
180 "): ?\\$output_type_id$ {\n"
181 " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
184 }
else if (
method->server_streaming()) {
186 " * @param \\$input_type_id$ $$request client request\n"
187 " * @param \\Grpc\\ServerCallWriter $$writer write response data of "
188 "\\$output_type_id$\n"
189 " * @param \\Grpc\\ServerContext $$context server request context\n"
192 "public function $method_name$(\n"
193 " \\$input_type_id$ $$request,\n"
194 " \\Grpc\\ServerCallWriter $$writer,\n"
195 " \\Grpc\\ServerContext $$context\n"
197 " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
198 " $$writer->finish();\n"
202 " * @param \\$input_type_id$ $$request client request\n"
203 " * @param \\Grpc\\ServerContext $$context server request context\n"
204 " * @return \\$output_type_id$ for response data, null if if error "
206 " * initial metadata (if any) and status (if not ok) should be set "
209 "public function $method_name$(\n"
210 " \\$input_type_id$ $$request,\n"
211 " \\Grpc\\ServerContext $$context\n"
212 "): ?\\$output_type_id$ {\n"
213 " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
217 out->Print(vars, method_template);
222 map<std::string, std::string> vars;
223 vars[
"service_name"] =
service->full_name();
227 " * Get the method descriptors of the service for server registration\n"
229 " * @return array of \\Grpc\\MethodDescriptor for the service methods\n"
231 "public final function getMethodDescriptors(): array\n{\n");
234 out->Print(
"return [\n");
237 for (
int i = 0;
i <
service->method_count();
i++) {
239 auto input_type =
method->input_type();
240 vars[
"method_name"] =
method->name();
241 vars[
"input_type_id"] = MessageIdentifierName(
243 if (
method->client_streaming() &&
method->server_streaming()) {
244 vars[
"call_type"] =
"BIDI_STREAMING_CALL";
245 }
else if (
method->client_streaming()) {
246 vars[
"call_type"] =
"CLIENT_STREAMING_CALL";
247 }
else if (
method->server_streaming()) {
248 vars[
"call_type"] =
"SERVER_STREAMING_CALL";
250 vars[
"call_type"] =
"UNARY_CALL";
254 "'/$service_name$/$method_name$' => new \\Grpc\\MethodDescriptor(\n"
256 " '$method_name$',\n"
257 " '\\$input_type_id$',\n"
258 " \\Grpc\\MethodDescriptor::$call_type$\n"
273 map<std::string, std::string> vars;
278 vars[
"extends"] = is_server ?
"" :
"extends \\Grpc\\BaseStub ";
279 out->Print(vars,
"class $name$ $extends${\n\n");
284 "/**\n * @param string $$hostname hostname\n"
285 " * @param array $$opts channel options\n"
286 " * @param \\Grpc\\Channel $$channel (optional) re-use channel object\n"
288 "public function __construct($$hostname, $$opts, "
289 "$$channel = null) {\n");
292 out->Print(
"parent::__construct($$hostname, $$opts, $$channel);\n");
297 for (
int i = 0;
i <
service->method_count();
i++) {
321 out.Print(
"<?php\n");
322 out.Print(
"// GENERATED CODE -- DO NOT EDIT!\n\n");
325 if (!leading_comments.empty()) {
326 out.Print(
"// Original file comments:\n");
327 out.PrintRaw(leading_comments.c_str());
330 map<std::string, std::string> vars;
332 vars[
"package"] = php_namespace;
333 out.Print(vars,
"namespace $package$;\n\n");
335 PrintService(
service, class_suffix, is_server, &
out);