PHP: update codegen plugin to support php_namespace option

pull/11886/head
Stanley Cheung 8 years ago
parent faef8bcc65
commit 5fd85b58e6
  1. 16
      src/compiler/php_generator.cc
  2. 2
      src/compiler/php_generator.h
  3. 43
      src/compiler/php_generator_helpers.h
  4. 18
      src/compiler/php_plugin.cc
  5. 2
      third_party/protobuf
  6. 2
      tools/run_tests/sanity/check_submodules.sh

@ -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;
}

@ -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

@ -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>

@ -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));

@ -1 +1 @@
Subproject commit a6189acd18b00611c1dc7042299ad75486f08a1a
Subproject commit 942a29cecd36f2a4b22fdd2179635cd548e6bd27

@ -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

Loading…
Cancel
Save