From 6f325805c0bc956f927b0e2dbfb4dd8133b4ed69 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 5 Jun 2017 00:10:18 -0700 Subject: Add new file option php_namespace. (#3162) * Add new file option php_namespace. Use this option to change the namespace of php generated classes. Default is empty. When this option is empty, the package name will be used for determining the namespace. * Uncomment commented tests * Revert gdb test change * Update csharp descriptor. * Add test for empty php_namespace. --- src/google/protobuf/compiler/php/php_generator.cc | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/google/protobuf/compiler/php') diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index cbddee9e..78252817 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -145,6 +145,15 @@ std::string FullClassName(const DescriptorType* desc, bool is_descriptor) { } classname = ClassNamePrefix(classname, desc) + classname; + if (desc->file()->options().has_php_namespace()) { + const string& php_namespace = desc->file()->options().php_namespace(); + if (php_namespace != "") { + return php_namespace + '\\' + classname; + } else { + return classname; + } + } + if (desc->file()->package() == "") { return classname; } else { @@ -822,7 +831,14 @@ void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, std::string fullname = FilenameToClassname(filename); int lastindex = fullname.find_last_of("\\"); - if (!file->package().empty()) { + if (file->options().has_php_namespace()) { + const string& php_namespace = file->options().php_namespace(); + if (!php_namespace.empty()) { + printer.Print( + "namespace ^name^;\n\n", + "name", php_namespace); + } + } else if (!file->package().empty()) { printer.Print( "namespace ^name^;\n\n", "name", fullname.substr(0, lastindex)); @@ -874,7 +890,14 @@ void GenerateMessageFile(const FileDescriptor* file, const Descriptor* message, std::string fullname = FilenameToClassname(filename); int lastindex = fullname.find_last_of("\\"); - if (!file->package().empty()) { + if (file->options().has_php_namespace()) { + const string& php_namespace = file->options().php_namespace(); + if (!php_namespace.empty()) { + printer.Print( + "namespace ^name^;\n\n", + "name", php_namespace); + } + } else if (!file->package().empty()) { printer.Print( "namespace ^name^;\n\n", "name", fullname.substr(0, lastindex)); -- cgit v1.2.3