php_generator.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include <map>
  19. #include <google/protobuf/compiler/php/php_generator.h>
  20. #include "src/compiler/config.h"
  21. #include "src/compiler/generator_helpers.h"
  22. #include "src/compiler/php_generator_helpers.h"
  23. using google::protobuf::compiler::php::GeneratedClassName;
  24. using grpc::protobuf::Descriptor;
  25. using grpc::protobuf::FileDescriptor;
  26. using grpc::protobuf::MethodDescriptor;
  27. using grpc::protobuf::ServiceDescriptor;
  28. using grpc::protobuf::io::Printer;
  29. using grpc::protobuf::io::StringOutputStream;
  30. using std::map;
  31. namespace grpc_php_generator {
  32. namespace {
  33. std::string ConvertToPhpNamespace(const std::string& name) {
  34. std::vector<std::string> tokens = grpc_generator::tokenize(name, ".");
  35. std::ostringstream oss;
  36. for (unsigned int i = 0; i < tokens.size(); i++) {
  37. oss << (i == 0 ? "" : "\\")
  38. << grpc_generator::CapitalizeFirstLetter(tokens[i]);
  39. }
  40. return oss.str();
  41. }
  42. std::string PackageName(const FileDescriptor* file) {
  43. if (file->options().has_php_namespace()) {
  44. return file->options().php_namespace();
  45. } else {
  46. return ConvertToPhpNamespace(file->package());
  47. }
  48. }
  49. std::string MessageIdentifierName(const std::string& name,
  50. const FileDescriptor* file) {
  51. std::vector<std::string> tokens = grpc_generator::tokenize(name, ".");
  52. std::ostringstream oss;
  53. if (PackageName(file) != "") {
  54. oss << PackageName(file) << "\\";
  55. }
  56. oss << grpc_generator::CapitalizeFirstLetter(tokens[tokens.size() - 1]);
  57. return oss.str();
  58. }
  59. void PrintMethod(const MethodDescriptor* method, Printer* out) {
  60. const Descriptor* input_type = method->input_type();
  61. const Descriptor* output_type = method->output_type();
  62. map<std::string, std::string> vars;
  63. vars["service_name"] = method->service()->full_name();
  64. vars["name"] = method->name();
  65. vars["input_type_id"] =
  66. MessageIdentifierName(GeneratedClassName(input_type), input_type->file());
  67. vars["output_type_id"] = MessageIdentifierName(
  68. GeneratedClassName(output_type), output_type->file());
  69. out->Print("/**\n");
  70. out->Print(GetPHPComments(method, " *").c_str());
  71. if (method->client_streaming()) {
  72. if (method->server_streaming()) {
  73. vars["return_type_id"] = "\\Grpc\\BidiStreamingCall";
  74. } else {
  75. vars["return_type_id"] = "\\Grpc\\ClientStreamingCall";
  76. }
  77. out->Print(vars,
  78. " * @param array $$metadata metadata\n"
  79. " * @param array $$options call options\n"
  80. " * @return $return_type_id$\n */\n"
  81. "public function $name$($$metadata = [], "
  82. "$$options = []) {\n");
  83. out->Indent();
  84. out->Indent();
  85. if (method->server_streaming()) {
  86. out->Print("return $$this->_bidiRequest(");
  87. } else {
  88. out->Print("return $$this->_clientStreamRequest(");
  89. }
  90. out->Print(vars,
  91. "'/$service_name$/$name$',\n"
  92. "['\\$output_type_id$','decode'],\n"
  93. "$$metadata, $$options);\n");
  94. } else {
  95. if (method->server_streaming()) {
  96. vars["return_type_id"] = "\\Grpc\\ServerStreamingCall";
  97. } else {
  98. vars["return_type_id"] = "\\Grpc\\UnaryCall";
  99. }
  100. out->Print(vars,
  101. " * @param \\$input_type_id$ $$argument input argument\n"
  102. " * @param array $$metadata metadata\n"
  103. " * @param array $$options call options\n"
  104. " * @return $return_type_id$\n */\n"
  105. "public function $name$(\\$input_type_id$ $$argument,\n"
  106. " $$metadata = [], $$options = []) {\n");
  107. out->Indent();
  108. out->Indent();
  109. if (method->server_streaming()) {
  110. out->Print("return $$this->_serverStreamRequest(");
  111. } else {
  112. out->Print("return $$this->_simpleRequest(");
  113. }
  114. out->Print(vars,
  115. "'/$service_name$/$name$',\n"
  116. "$$argument,\n"
  117. "['\\$output_type_id$', 'decode'],\n"
  118. "$$metadata, $$options);\n");
  119. }
  120. out->Outdent();
  121. out->Outdent();
  122. out->Print("}\n\n");
  123. }
  124. // Prints out the service descriptor object
  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. std::string GenerateFile(const FileDescriptor* file,
  159. const ServiceDescriptor* service,
  160. const std::string& class_suffix) {
  161. std::string output;
  162. {
  163. StringOutputStream output_stream(&output);
  164. Printer out(&output_stream, '$');
  165. out.Print("<?php\n");
  166. out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
  167. std::string leading_comments = GetPHPComments(file, "//");
  168. if (!leading_comments.empty()) {
  169. out.Print("// Original file comments:\n");
  170. out.PrintRaw(leading_comments.c_str());
  171. }
  172. map<std::string, std::string> vars;
  173. std::string php_namespace = PackageName(file);
  174. vars["package"] = php_namespace;
  175. out.Print(vars, "namespace $package$;\n\n");
  176. PrintService(service, class_suffix, &out);
  177. }
  178. return output;
  179. }
  180. } // namespace grpc_php_generator