php_plugin.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Generates PHP gRPC service interface out of Protobuf IDL.
  19. #include <memory>
  20. #include "src/compiler/config.h"
  21. #include "src/compiler/php_generator.h"
  22. #include "src/compiler/php_generator_helpers.h"
  23. using grpc_php_generator::GenerateFile;
  24. using grpc_php_generator::GetPHPServiceFilename;
  25. class PHPGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
  26. public:
  27. PHPGrpcGenerator() {}
  28. ~PHPGrpcGenerator() {}
  29. bool Generate(const grpc::protobuf::FileDescriptor *file,
  30. const grpc::string &parameter,
  31. grpc::protobuf::compiler::GeneratorContext *context,
  32. grpc::string *error) const {
  33. if (file->service_count() == 0) {
  34. return true;
  35. }
  36. for (int i = 0; i < file->service_count(); i++) {
  37. grpc::string code = GenerateFile(file, file->service(i), parameter);
  38. // Get output file name
  39. grpc::string file_name =
  40. GetPHPServiceFilename(file, file->service(i), parameter);
  41. std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
  42. context->Open(file_name));
  43. grpc::protobuf::io::CodedOutputStream coded_out(output.get());
  44. coded_out.WriteRaw(code.data(), code.size());
  45. }
  46. return true;
  47. }
  48. };
  49. int main(int argc, char *argv[]) {
  50. PHPGrpcGenerator generator;
  51. return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
  52. }