php_plugin.cc
Go to the documentation of this file.
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 
19 // Generates PHP gRPC service interface out of Protobuf IDL.
20 
21 #include <memory>
22 
23 #include "src/compiler/config.h"
26 
30 
32  public:
35 
36  uint64_t GetSupportedFeatures() const override {
37  return FEATURE_PROTO3_OPTIONAL;
38  }
39 
41  const std::string& parameter,
43  std::string* error) const override {
44  if (file->service_count() == 0) {
45  return true;
46  }
47 
48  std::vector<std::pair<std::string, std::string> > options;
49  ParseGeneratorParameter(parameter, &options);
50 
51  bool generate_server = false;
52  std::string class_suffix;
53  for (size_t i = 0; i < options.size(); ++i) {
54  if (options[i].first == "class_suffix") {
55  class_suffix = options[i].second;
56  } else if (options[i].first == "generate_server") {
57  generate_server = true;
58  } else {
59  *error = "unsupported options: " + options[i].first;
60  return false;
61  }
62  }
63 
64  for (int i = 0; i < file->service_count(); i++) {
65  GenerateService(file, file->service(i), class_suffix, false, context);
66  if (generate_server) {
67  GenerateService(file, file->service(i), class_suffix, true, context);
68  }
69  }
70 
71  return true;
72  }
73 
74  private:
78  const std::string& class_suffix, bool is_server,
80  std::string code = GenerateFile(file, service, class_suffix, is_server);
81 
82  // Get output file name
83  std::string file_name =
84  GetPHPServiceFilename(file, service, class_suffix, is_server);
85 
86  std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
87  context->Open(file_name));
89  coded_out.WriteRaw(code.data(), code.size());
90  }
91 };
92 
93 int main(int argc, char* argv[]) {
94  PHPGrpcGenerator generator;
95  return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
96 }
grpc_node_generator::GenerateFile
std::string GenerateFile(const FileDescriptor *file, const Parameters &params)
Definition: node_generator.cc:246
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
PHPGrpcGenerator::Generate
bool Generate(const grpc::protobuf::FileDescriptor *file, const std::string &parameter, grpc::protobuf::compiler::GeneratorContext *context, std::string *error) const override
Definition: php_plugin.cc:40
options
double_dict options[]
Definition: capstone_test.c:55
config.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_php_generator::GenerateFile
std::string GenerateFile(const FileDescriptor *file, const ServiceDescriptor *service, const std::string &class_suffix, bool is_server)
Definition: src/compiler/php_generator.cc:313
grpc_php_generator::GetPHPServiceFilename
std::string GetPHPServiceFilename(const grpc::protobuf::FileDescriptor *file, const grpc::protobuf::ServiceDescriptor *service, const std::string &class_suffix, bool is_server)
Definition: php_generator_helpers.h:48
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
php_generator.h
main
int main(int argc, char *argv[])
Definition: php_plugin.cc:93
php_generator_helpers.h
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc::protobuf::compiler::ParseGeneratorParameter
static void ParseGeneratorParameter(const string &parameter, std::vector< std::pair< string, string > > *options)
Definition: src/compiler/config.h:47
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
PHPGrpcGenerator::GetSupportedFeatures
uint64_t GetSupportedFeatures() const override
Definition: php_plugin.cc:36
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
PHPGrpcGenerator::PHPGrpcGenerator
PHPGrpcGenerator()
Definition: php_plugin.cc:33
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
PHPGrpcGenerator::GenerateService
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
Definition: php_plugin.cc:75
grpc::protobuf::compiler::PluginMain
static int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: src/compiler/config.h:43
first
StrT first
Definition: cxa_demangle.cpp:4884
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:101
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
PHPGrpcGenerator
Definition: php_plugin.cc:31
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
PHPGrpcGenerator::~PHPGrpcGenerator
~PHPGrpcGenerator()
Definition: php_plugin.cc:34
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:43