diff options
author | Stanley Cheung <stanleycheung@google.com> | 2017-07-20 00:59:03 -0700 |
---|---|---|
committer | Stanley Cheung <stanleycheung@google.com> | 2017-08-10 11:01:35 -0700 |
commit | 5fd85b58e6412a28e640d63d362c4b626a9d3e41 (patch) | |
tree | 6cef0a3c64fa4e8b66d391ee29a362e4dc9bf19e | |
parent | faef8bcc6533ac5fbc6a8efa4ff964fe897f2b5c (diff) |
PHP: update codegen plugin to support php_namespace option
-rw-r--r-- | src/compiler/php_generator.cc | 16 | ||||
-rw-r--r-- | src/compiler/php_generator.h | 2 | ||||
-rw-r--r-- | src/compiler/php_generator_helpers.h | 43 | ||||
-rw-r--r-- | src/compiler/php_plugin.cc | 18 | ||||
m--------- | third_party/protobuf | 0 | ||||
-rwxr-xr-x | tools/run_tests/sanity/check_submodules.sh | 2 |
6 files changed, 50 insertions, 31 deletions
diff --git a/src/compiler/php_generator.cc b/src/compiler/php_generator.cc index 38ec46e656..67967d0bd7 100644 --- a/src/compiler/php_generator.cc +++ b/src/compiler/php_generator.cc @@ -98,12 +98,12 @@ void PrintMethod(const MethodDescriptor *method, Printer *out) { // Prints out the service descriptor object void PrintService(const ServiceDescriptor *service, - const grpc::string ¶meter, Printer *out) { + const grpc::string &class_suffix, Printer *out) { map<grpc::string, grpc::string> vars; out->Print("/**\n"); out->Print(GetPHPComments(service, " *").c_str()); out->Print(" */\n"); - vars["name"] = GetPHPServiceClassname(service, parameter); + vars["name"] = GetPHPServiceClassname(service, class_suffix); out->Print(vars, "class $name$ extends \\Grpc\\BaseStub {\n\n"); out->Indent(); out->Indent(); @@ -133,7 +133,7 @@ void PrintService(const ServiceDescriptor *service, grpc::string GenerateFile(const FileDescriptor *file, const ServiceDescriptor *service, - const grpc::string ¶meter) { + const grpc::string &class_suffix) { grpc::string output; { StringOutputStream output_stream(&output); @@ -149,10 +149,16 @@ grpc::string GenerateFile(const FileDescriptor *file, } map<grpc::string, grpc::string> vars; - vars["package"] = MessageIdentifierName(file->package()); + grpc::string php_namespace; + if (file->options().has_php_namespace()) { + php_namespace = file->options().php_namespace(); + } else { + php_namespace = MessageIdentifierName(file->package()); + } + vars["package"] = php_namespace; out.Print(vars, "namespace $package$;\n\n"); - PrintService(service, parameter, &out); + PrintService(service, class_suffix, &out); } return output; } diff --git a/src/compiler/php_generator.h b/src/compiler/php_generator.h index 9a04bd33d7..5412d774a9 100644 --- a/src/compiler/php_generator.h +++ b/src/compiler/php_generator.h @@ -25,7 +25,7 @@ namespace grpc_php_generator { grpc::string GenerateFile(const grpc::protobuf::FileDescriptor *file, const grpc::protobuf::ServiceDescriptor *service, - const grpc::string ¶meter); + const grpc::string &class_suffix); } // namespace grpc_php_generator diff --git a/src/compiler/php_generator_helpers.h b/src/compiler/php_generator_helpers.h index 5edebf6290..3c886794b8 100644 --- a/src/compiler/php_generator_helpers.h +++ b/src/compiler/php_generator_helpers.h @@ -28,28 +28,8 @@ namespace grpc_php_generator { inline grpc::string GetPHPServiceClassname( const grpc::protobuf::ServiceDescriptor *service, - const grpc::string ¶meter) { - grpc::string suffix; - if (parameter == "") { - suffix = "Client"; - } else { - suffix = parameter; - } - return service->name() + suffix; -} - -inline grpc::string GetPHPServiceFilename( - const grpc::protobuf::FileDescriptor *file, - const grpc::protobuf::ServiceDescriptor *service, - const grpc::string ¶meter) { - std::vector<grpc::string> tokens = - grpc_generator::tokenize(file->package(), "."); - std::ostringstream oss; - for (unsigned int i = 0; i < tokens.size(); i++) { - oss << (i == 0 ? "" : "/") - << grpc_generator::CapitalizeFirstLetter(tokens[i]); - } - return oss.str() + "/" + GetPHPServiceClassname(service, parameter) + ".php"; + const grpc::string &class_suffix) { + return service->name() + (class_suffix == "" ? "Client" : class_suffix); } // ReplaceAll replaces all instances of search with replace in s. @@ -63,6 +43,25 @@ inline grpc::string ReplaceAll(grpc::string s, const grpc::string &search, return s; } +inline grpc::string GetPHPServiceFilename( + const grpc::protobuf::FileDescriptor *file, + const grpc::protobuf::ServiceDescriptor *service, + const grpc::string &class_suffix) { + std::ostringstream oss; + if (file->options().has_php_namespace()) { + oss << ReplaceAll(file->options().php_namespace(), "\\", "/"); + } else { + std::vector<grpc::string> tokens = + grpc_generator::tokenize(file->package(), "."); + for (unsigned int i = 0; i < tokens.size(); i++) { + oss << (i == 0 ? "" : "/") + << grpc_generator::CapitalizeFirstLetter(tokens[i]); + } + } + return oss.str() + "/" + GetPHPServiceClassname(service, class_suffix) + + ".php"; +} + // Get leading or trailing comments in a string. Comment lines start with "// ". // Leading detached comments are put in in front of leading comments. template <typename DescriptorType> diff --git a/src/compiler/php_plugin.cc b/src/compiler/php_plugin.cc index bbe91656d5..7b0da87eb2 100644 --- a/src/compiler/php_plugin.cc +++ b/src/compiler/php_plugin.cc @@ -26,6 +26,7 @@ using grpc_php_generator::GenerateFile; using grpc_php_generator::GetPHPServiceFilename; +using google::protobuf::compiler::ParseGeneratorParameter; class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { public: @@ -40,12 +41,25 @@ class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { return true; } + std::vector<std::pair<grpc::string, grpc::string> > options; + ParseGeneratorParameter(parameter, &options); + + grpc::string class_suffix; + for (size_t i = 0; i < options.size(); ++i) { + if (options[i].first == "class_suffix") { + class_suffix = options[i].second; + } else { + *error = "unsupported options: " + options[i].first; + return false; + } + } + for (int i = 0; i < file->service_count(); i++) { - grpc::string code = GenerateFile(file, file->service(i), parameter); + grpc::string code = GenerateFile(file, file->service(i), class_suffix); // Get output file name grpc::string file_name = - GetPHPServiceFilename(file, file->service(i), parameter); + GetPHPServiceFilename(file, file->service(i), class_suffix); std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( context->Open(file_name)); diff --git a/third_party/protobuf b/third_party/protobuf -Subproject a6189acd18b00611c1dc7042299ad75486f08a1 +Subproject 942a29cecd36f2a4b22fdd2179635cd548e6bd2 diff --git a/tools/run_tests/sanity/check_submodules.sh b/tools/run_tests/sanity/check_submodules.sh index d856e22176..f6812f1afb 100755 --- a/tools/run_tests/sanity/check_submodules.sh +++ b/tools/run_tests/sanity/check_submodules.sh @@ -31,7 +31,7 @@ cat << EOF | awk '{ print $1 }' | sort > $want_submodules 886e7d75368e3f4fab3f4d0d3584e4abfc557755 third_party/boringssl-with-bazel (version_for_cocoapods_7.0-857-g886e7d7) 30dbc81fb5ffdc98ea9b14b1918bfe4e8779b26e third_party/gflags (v2.2.0) ec44c6c1675c25b9827aacd08c02433cccde7780 third_party/googletest (release-1.8.0) - a6189acd18b00611c1dc7042299ad75486f08a1a third_party/protobuf (v3.3.0) + 942a29cecd36f2a4b22fdd2179635cd548e6bd27 third_party/protobuf (3.4.x) cacf7f1d4e3d44d871b605da3b647f07d718623f third_party/zlib (v1.2.11) 7691f773af79bf75a62d1863fd0f13ebf9dc51b1 third_party/cares/cares (1.12.0) EOF |