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