php_generator_helpers.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef GRPC_INTERNAL_COMPILER_PHP_GENERATOR_HELPERS_H
  19. #define GRPC_INTERNAL_COMPILER_PHP_GENERATOR_HELPERS_H
  20. #include <algorithm>
  21. #include "src/compiler/config.h"
  22. #include "src/compiler/generator_helpers.h"
  23. namespace grpc_php_generator {
  24. inline TString GetPHPServiceClassname(
  25. const grpc::protobuf::ServiceDescriptor* service,
  26. const TString& class_suffix, bool is_server) {
  27. return service->name() +
  28. (class_suffix == "" ? (is_server ? "" : "Client") : class_suffix) +
  29. (is_server ? "Stub" : "");
  30. }
  31. // ReplaceAll replaces all instances of search with replace in s.
  32. inline TString ReplaceAll(TString s, const TString& search,
  33. const TString& replace) {
  34. size_t pos = 0;
  35. while ((pos = s.find(search, pos)) != TString::npos) {
  36. s.replace(pos, search.length(), replace);
  37. pos += replace.length();
  38. }
  39. return s;
  40. }
  41. inline TString GetPHPServiceFilename(
  42. const grpc::protobuf::FileDescriptor* file,
  43. const grpc::protobuf::ServiceDescriptor* service,
  44. const TString& class_suffix, bool is_server) {
  45. std::ostringstream oss;
  46. if (file->options().has_php_namespace()) {
  47. oss << ReplaceAll(file->options().php_namespace(), "\\", "/");
  48. } else {
  49. std::vector<TString> tokens =
  50. grpc_generator::tokenize(file->package(), ".");
  51. for (unsigned int i = 0; i < tokens.size(); i++) {
  52. oss << (i == 0 ? "" : "/")
  53. << grpc_generator::CapitalizeFirstLetter(tokens[i]);
  54. }
  55. }
  56. return oss.str() + "/" +
  57. GetPHPServiceClassname(service, class_suffix, is_server) + ".php";
  58. }
  59. // Get leading or trailing comments in a string. Comment lines start with "// ".
  60. // Leading detached comments are put in front of leading comments.
  61. template <typename DescriptorType>
  62. inline TString GetPHPComments(const DescriptorType* desc,
  63. TString prefix) {
  64. return ReplaceAll(grpc_generator::GetPrefixedComments(desc, true, prefix),
  65. "*/", "&#42;/");
  66. }
  67. } // namespace grpc_php_generator
  68. #endif // GRPC_INTERNAL_COMPILER_PHP_GENERATOR_HELPERS_H