aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler
diff options
context:
space:
mode:
authorGravatar Stanley Cheung <stanleycheung@google.com>2017-07-20 00:59:03 -0700
committerGravatar Stanley Cheung <stanleycheung@google.com>2017-08-10 11:01:35 -0700
commit5fd85b58e6412a28e640d63d362c4b626a9d3e41 (patch)
tree6cef0a3c64fa4e8b66d391ee29a362e4dc9bf19e /src/compiler
parentfaef8bcc6533ac5fbc6a8efa4ff964fe897f2b5c (diff)
PHP: update codegen plugin to support php_namespace option
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/php_generator.cc16
-rw-r--r--src/compiler/php_generator.h2
-rw-r--r--src/compiler/php_generator_helpers.h43
-rw-r--r--src/compiler/php_plugin.cc18
4 files changed, 49 insertions, 30 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 &parameter, 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 &parameter) {
+ 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 &parameter);
+ 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 &parameter) {
- 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 &parameter) {
- 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));