Stanley Cheung | 53d219c | 2016-09-27 15:45:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <map> |
| 35 | |
| 36 | #include "src/compiler/config.h" |
| 37 | #include "src/compiler/generator_helpers.h" |
| 38 | #include "src/compiler/php_generator_helpers.h" |
| 39 | |
| 40 | using grpc::protobuf::FileDescriptor; |
| 41 | using grpc::protobuf::ServiceDescriptor; |
| 42 | using grpc::protobuf::MethodDescriptor; |
| 43 | using grpc::protobuf::Descriptor; |
| 44 | using grpc::protobuf::io::Printer; |
| 45 | using grpc::protobuf::io::StringOutputStream; |
| 46 | using std::map; |
| 47 | |
| 48 | namespace grpc_php_generator { |
| 49 | namespace { |
| 50 | |
| 51 | grpc::string MessageIdentifierName(const grpc::string &name) { |
| 52 | std::vector<grpc::string> tokens = grpc_generator::tokenize(name, "."); |
| 53 | std::ostringstream oss; |
| 54 | for (unsigned int i = 0; i < tokens.size(); i++) { |
| 55 | oss << (i == 0 ? "" : "\\") |
| 56 | << grpc_generator::CapitalizeFirstLetter(tokens[i]); |
| 57 | } |
| 58 | return oss.str(); |
| 59 | } |
| 60 | |
| 61 | void PrintMethod(const MethodDescriptor *method, Printer *out) { |
| 62 | const Descriptor *input_type = method->input_type(); |
| 63 | const Descriptor *output_type = method->output_type(); |
| 64 | map<grpc::string, grpc::string> vars; |
| 65 | vars["service_name"] = method->service()->full_name(); |
| 66 | vars["name"] = method->name(); |
| 67 | vars["input_type_id"] = MessageIdentifierName(input_type->full_name()); |
| 68 | vars["output_type_id"] = MessageIdentifierName(output_type->full_name()); |
| 69 | |
| 70 | out->Print("/**\n"); |
| 71 | out->Print(GetPHPComments(method, " *").c_str()); |
| 72 | if (method->client_streaming()) { |
| 73 | out->Print(vars, |
| 74 | " * @param array $$metadata metadata\n" |
| 75 | " * @param array $$options call options\n */\n" |
| 76 | "public function $name$($$metadata = [], " |
| 77 | "$$options = []) {\n"); |
| 78 | out->Indent(); |
| 79 | if (method->server_streaming()) { |
| 80 | out->Print("return $$this->_bidiRequest("); |
| 81 | } else { |
| 82 | out->Print("return $$this->_clientStreamRequest("); |
| 83 | } |
| 84 | out->Print(vars, |
| 85 | "'/$service_name$/$name$',\n" |
| 86 | "['\\$output_type_id$','decode'],\n" |
| 87 | "$$metadata, $$options);\n"); |
| 88 | } else { |
| 89 | out->Print(vars, |
| 90 | " * @param \\$input_type_id$ $$argument input argument\n" |
| 91 | " * @param array $$metadata metadata\n" |
| 92 | " * @param array $$options call options\n */\n" |
| 93 | "public function $name$(\\$input_type_id$ $$argument,\n" |
| 94 | " $$metadata = [], $$options = []) {\n"); |
| 95 | out->Indent(); |
| 96 | if (method->server_streaming()) { |
| 97 | out->Print("return $$this->_serverStreamRequest("); |
| 98 | } else { |
| 99 | out->Print("return $$this->_simpleRequest("); |
| 100 | } |
| 101 | out->Print(vars, |
| 102 | "'/$service_name$/$name$',\n" |
| 103 | "$$argument,\n" |
| 104 | "['\\$output_type_id$', 'decode'],\n" |
| 105 | "$$metadata, $$options);\n"); |
| 106 | } |
| 107 | out->Outdent(); |
| 108 | out->Print("}\n\n"); |
| 109 | } |
| 110 | |
| 111 | // Prints out the service descriptor object |
| 112 | void PrintService(const ServiceDescriptor *service, Printer *out) { |
| 113 | map<grpc::string, grpc::string> vars; |
| 114 | out->Print(GetPHPComments(service, "//").c_str()); |
| 115 | vars["name"] = service->name(); |
| 116 | out->Print(vars, "class $name$Client extends \\Grpc\\BaseStub {\n\n"); |
| 117 | out->Indent(); |
| 118 | out->Print( |
| 119 | "/**\n * @param string $$hostname hostname\n" |
| 120 | " * @param array $$opts channel options\n" |
| 121 | " * @param Grpc\\Channel $$channel (optional) re-use channel " |
| 122 | "object\n */\n" |
| 123 | "public function __construct($$hostname, $$opts, " |
| 124 | "$$channel = null) {\n"); |
| 125 | out->Indent(); |
| 126 | out->Print("parent::__construct($$hostname, $$opts, $$channel);\n"); |
| 127 | out->Outdent(); |
| 128 | out->Print("}\n\n"); |
| 129 | for (int i = 0; i < service->method_count(); i++) { |
| 130 | grpc::string method_name = |
| 131 | grpc_generator::LowercaseFirstLetter(service->method(i)->name()); |
| 132 | PrintMethod(service->method(i), out); |
| 133 | } |
| 134 | out->Outdent(); |
| 135 | out->Print("}\n\n"); |
| 136 | } |
| 137 | |
| 138 | void PrintServices(const FileDescriptor *file, Printer *out) { |
| 139 | map<grpc::string, grpc::string> vars; |
| 140 | vars["package"] = MessageIdentifierName(file->package()); |
| 141 | out->Print(vars, "namespace $package$ {\n\n"); |
| 142 | out->Indent(); |
| 143 | for (int i = 0; i < file->service_count(); i++) { |
| 144 | PrintService(file->service(i), out); |
| 145 | } |
| 146 | out->Outdent(); |
| 147 | out->Print("}\n"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | grpc::string GenerateFile(const FileDescriptor *file) { |
| 152 | grpc::string output; |
| 153 | { |
| 154 | StringOutputStream output_stream(&output); |
| 155 | Printer out(&output_stream, '$'); |
| 156 | |
| 157 | if (file->service_count() == 0) { |
| 158 | return output; |
| 159 | } |
| 160 | out.Print("<?php\n"); |
| 161 | out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n"); |
| 162 | |
| 163 | grpc::string leading_comments = GetPHPComments(file, "//"); |
| 164 | if (!leading_comments.empty()) { |
| 165 | out.Print("// Original file comments:\n"); |
| 166 | out.Print(leading_comments.c_str()); |
| 167 | } |
| 168 | |
| 169 | PrintServices(file, &out); |
| 170 | } |
| 171 | return output; |
| 172 | } |
| 173 | |
| 174 | } // namespace grpc_php_generator |