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#include <map>
20
21#include <google/protobuf/compiler/php/php_generator.h>
22#include "src/compiler/config.h"
23#include "src/compiler/generator_helpers.h"
24#include "src/compiler/php_generator_helpers.h"
25
26using google::protobuf::compiler::php::GeneratedClassName;
27using grpc::protobuf::Descriptor;
28using grpc::protobuf::FileDescriptor;
29using grpc::protobuf::MethodDescriptor;
30using grpc::protobuf::ServiceDescriptor;
31using grpc::protobuf::io::Printer;
32using grpc::protobuf::io::StringOutputStream;
33using std::map;
34
35namespace grpc_php_generator {
36namespace {
37
38grpc::string ConvertToPhpNamespace(const grpc::string& name) {
39 std::vector<grpc::string> tokens = grpc_generator::tokenize(name, ".");
40 std::ostringstream oss;
41 for (unsigned int i = 0; i < tokens.size(); i++) {
42 oss << (i == 0 ? "" : "\\")
43 << grpc_generator::CapitalizeFirstLetter(tokens[i]);
44 }
45 return oss.str();
46}
47
48grpc::string PackageName(const FileDescriptor* file) {
49 if (file->options().has_php_namespace()) {
50 return file->options().php_namespace();
51 } else {
52 return ConvertToPhpNamespace(file->package());
53 }
54}
55
56grpc::string MessageIdentifierName(const grpc::string& name,
57 const FileDescriptor* file) {
58 std::vector<grpc::string> tokens = grpc_generator::tokenize(name, ".");
59 std::ostringstream oss;
60 if (PackageName(file) != "") {
61 oss << PackageName(file) << "\\";
62 }
63 oss << grpc_generator::CapitalizeFirstLetter(tokens[tokens.size() - 1]);
64 return oss.str();
65}
66
67void PrintMethod(const MethodDescriptor* method, Printer* out) {
68 const Descriptor* input_type = method->input_type();
69 const Descriptor* output_type = method->output_type();
70 map<grpc::string, grpc::string> vars;
71 vars["service_name"] = method->service()->full_name();
72 vars["name"] = method->name();
73 vars["input_type_id"] =
74 MessageIdentifierName(GeneratedClassName(input_type), input_type->file());
75 vars["output_type_id"] = MessageIdentifierName(
76 GeneratedClassName(output_type), output_type->file());
77
78 out->Print("/**\n");
79 out->Print(GetPHPComments(method, " *").c_str());
80 if (method->client_streaming()) {
81 out->Print(vars,
82 " * @param array $$metadata metadata\n"
83 " * @param array $$options call options\n */\n"
84 "public function $name$($$metadata = [], "
85 "$$options = []) {\n");
86 out->Indent();
87 out->Indent();
88 if (method->server_streaming()) {
89 out->Print("return $$this->_bidiRequest(");
90 } else {
91 out->Print("return $$this->_clientStreamRequest(");
92 }
93 out->Print(vars,
94 "'/$service_name$/$name$',\n"
95 "['\\$output_type_id$','decode'],\n"
96 "$$metadata, $$options);\n");
97 } else {
98 out->Print(vars,
99 " * @param \\$input_type_id$ $$argument input argument\n"
100 " * @param array $$metadata metadata\n"
101 " * @param array $$options call options\n */\n"
102 "public function $name$(\\$input_type_id$ $$argument,\n"
103 " $$metadata = [], $$options = []) {\n");
104 out->Indent();
105 out->Indent();
106 if (method->server_streaming()) {
107 out->Print("return $$this->_serverStreamRequest(");
108 } else {
109 out->Print("return $$this->_simpleRequest(");
110 }
111 out->Print(vars,
112 "'/$service_name$/$name$',\n"
113 "$$argument,\n"
114 "['\\$output_type_id$', 'decode'],\n"
115 "$$metadata, $$options);\n");
116 }
117 out->Outdent();
118 out->Outdent();
119 out->Print("}\n\n");
120}
121
122// Prints out the service descriptor object
123void PrintService(const ServiceDescriptor* service,
124 const grpc::string& class_suffix, Printer* out) {
125 map<grpc::string, grpc::string> vars;
126 out->Print("/**\n");
127 out->Print(GetPHPComments(service, " *").c_str());
128 out->Print(" */\n");
129 vars["name"] = GetPHPServiceClassname(service, class_suffix);
130 out->Print(vars, "class $name$ extends \\Grpc\\BaseStub {\n\n");
131 out->Indent();
132 out->Indent();
133 out->Print(
134 "/**\n * @param string $$hostname hostname\n"
135 " * @param array $$opts channel options\n"
136 " * @param \\Grpc\\Channel $$channel (optional) re-use channel "
137 "object\n */\n"
138 "public function __construct($$hostname, $$opts, "
139 "$$channel = null) {\n");
140 out->Indent();
141 out->Indent();
142 out->Print("parent::__construct($$hostname, $$opts, $$channel);\n");
143 out->Outdent();
144 out->Outdent();
145 out->Print("}\n\n");
146 for (int i = 0; i < service->method_count(); i++) {
147 grpc::string method_name =
148 grpc_generator::LowercaseFirstLetter(service->method(i)->name());
149 PrintMethod(service->method(i), out);
150 }
151 out->Outdent();
152 out->Outdent();
153 out->Print("}\n");
154}
155} // namespace
156
157grpc::string GenerateFile(const FileDescriptor* file,
158 const ServiceDescriptor* service,
159 const grpc::string& class_suffix) {
160 grpc::string output;
161 {
162 StringOutputStream output_stream(&output);
163 Printer out(&output_stream, '$');
164
165 out.Print("<?php\n");
166 out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
167
168 grpc::string leading_comments = GetPHPComments(file, "//");
169 if (!leading_comments.empty()) {
170 out.Print("// Original file comments:\n");
171 out.PrintRaw(leading_comments.c_str());
172 }
173
174 map<grpc::string, grpc::string> vars;
175 grpc::string php_namespace = PackageName(file);
176 vars["package"] = php_namespace;
177 out.Print(vars, "namespace $package$;\n\n");
178
179 PrintService(service, class_suffix, &out);
180 }
181 return output;
182}
183
184} // namespace grpc_php_generator
185