xref: /aosp_15_r20/external/grpc-grpc/src/compiler/php_plugin.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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"
24 #include "src/compiler/php_generator.h"
25 #include "src/compiler/php_generator_helpers.h"
26 
27 using google::protobuf::compiler::ParseGeneratorParameter;
28 using grpc_php_generator::GenerateFile;
29 using grpc_php_generator::GetPHPServiceFilename;
30 
31 class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
32  public:
PHPGrpcGenerator()33   PHPGrpcGenerator() {}
~PHPGrpcGenerator()34   ~PHPGrpcGenerator() {}
35 
GetSupportedFeatures() const36   uint64_t GetSupportedFeatures() const override {
37     return FEATURE_PROTO3_OPTIONAL
38 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
39            | FEATURE_SUPPORTS_EDITIONS
40 #endif
41         ;
42   }
43 
44 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
GetMinimumEdition() const45   grpc::protobuf::Edition GetMinimumEdition() const override {
46     return grpc::protobuf::Edition::EDITION_PROTO2;
47   }
GetMaximumEdition() const48   grpc::protobuf::Edition GetMaximumEdition() const override {
49     return grpc::protobuf::Edition::EDITION_2023;
50   }
51 #endif
52 
Generate(const grpc::protobuf::FileDescriptor * file,const std::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,std::string * error) const53   bool Generate(const grpc::protobuf::FileDescriptor* file,
54                 const std::string& parameter,
55                 grpc::protobuf::compiler::GeneratorContext* context,
56                 std::string* error) const override {
57     if (file->service_count() == 0) {
58       return true;
59     }
60 
61     std::vector<std::pair<std::string, std::string> > options;
62     ParseGeneratorParameter(parameter, &options);
63 
64     bool generate_server = false;
65     std::string class_suffix;
66     for (size_t i = 0; i < options.size(); ++i) {
67       if (options[i].first == "class_suffix") {
68         class_suffix = options[i].second;
69       } else if (options[i].first == "generate_server") {
70         generate_server = true;
71       } else {
72         *error = "unsupported options: " + options[i].first;
73         return false;
74       }
75     }
76 
77     for (int i = 0; i < file->service_count(); i++) {
78       GenerateService(file, file->service(i), class_suffix, false, context);
79       if (generate_server) {
80         GenerateService(file, file->service(i), class_suffix, true, context);
81       }
82     }
83 
84     return true;
85   }
86 
87  private:
GenerateService(const grpc::protobuf::FileDescriptor * file,const grpc::protobuf::ServiceDescriptor * service,const std::string & class_suffix,bool is_server,grpc::protobuf::compiler::GeneratorContext * context) const88   void GenerateService(
89       const grpc::protobuf::FileDescriptor* file,
90       const grpc::protobuf::ServiceDescriptor* service,
91       const std::string& class_suffix, bool is_server,
92       grpc::protobuf::compiler::GeneratorContext* context) const {
93     std::string code = GenerateFile(file, service, class_suffix, is_server);
94 
95     // Get output file name
96     std::string file_name =
97         GetPHPServiceFilename(file, service, class_suffix, is_server);
98 
99     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
100         context->Open(file_name));
101     grpc::protobuf::io::CodedOutputStream coded_out(output.get());
102     coded_out.WriteRaw(code.data(), code.size());
103   }
104 };
105 
main(int argc,char * argv[])106 int main(int argc, char* argv[]) {
107   PHPGrpcGenerator generator;
108   return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
109 }
110