diff options
Diffstat (limited to 'grpc/src/compiler/php_plugin.cc')
-rw-r--r-- | grpc/src/compiler/php_plugin.cc | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/grpc/src/compiler/php_plugin.cc b/grpc/src/compiler/php_plugin.cc index c1be3876..7d4e4ce3 100644 --- a/grpc/src/compiler/php_plugin.cc +++ b/grpc/src/compiler/php_plugin.cc @@ -48,10 +48,13 @@ class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { std::vector<std::pair<std::string, std::string> > options; ParseGeneratorParameter(parameter, &options); + bool generate_server = false; std::string class_suffix; for (size_t i = 0; i < options.size(); ++i) { if (options[i].first == "class_suffix") { class_suffix = options[i].second; + } else if (options[i].first == "generate_server") { + generate_server = true; } else { *error = "unsupported options: " + options[i].first; return false; @@ -59,20 +62,32 @@ class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { } for (int i = 0; i < file->service_count(); i++) { - std::string code = GenerateFile(file, file->service(i), class_suffix); - - // Get output file name - std::string file_name = - GetPHPServiceFilename(file, file->service(i), class_suffix); - - std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( - context->Open(file_name)); - grpc::protobuf::io::CodedOutputStream coded_out(output.get()); - coded_out.WriteRaw(code.data(), code.size()); + GenerateService(file, file->service(i), class_suffix, false, context); + if (generate_server) { + GenerateService(file, file->service(i), class_suffix, true, context); + } } return true; } + + private: + void GenerateService( + const grpc::protobuf::FileDescriptor* file, + const grpc::protobuf::ServiceDescriptor* service, + const std::string& class_suffix, bool is_server, + grpc::protobuf::compiler::GeneratorContext* context) const { + std::string code = GenerateFile(file, service, class_suffix, is_server); + + // Get output file name + std::string file_name = + GetPHPServiceFilename(file, service, class_suffix, is_server); + + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( + context->Open(file_name)); + grpc::protobuf::io::CodedOutputStream coded_out(output.get()); + coded_out.WriteRaw(code.data(), code.size()); + } }; int main(int argc, char* argv[]) { |