| 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 |
|
| 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. |
| 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 |
|
| 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 |
return oss.str() + "/" + |
| 64 |
GetPHPServiceClassname(service, class_suffix, is_server) + ".php"; |
| 65 |
} |
| 66 |
|
| 67 |
// Get leading or trailing comments in a string. Comment lines start with "// ". |
| 68 |
// Leading detached comments are put in front of leading comments. |
| 69 |
template <typename DescriptorType> |
| 70 |
inline std::string GetPHPComments(const DescriptorType* desc, |
| 71 |
std::string prefix) { |
| 72 |
return ReplaceAll(grpc_generator::GetPrefixedComments(desc, true, prefix), |
| 73 |
"*/", "*/"); |
| 74 |
} |
| 75 |
|
| 76 |
} // namespace grpc_php_generator |
| 77 |
|
| 78 |
#endif // GRPC_INTERNAL_COMPILER_PHP_GENERATOR_HELPERS_H |