Commit 6f325805 authored by Paul Yang's avatar Paul Yang Committed by GitHub

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.
parent fbaad361
......@@ -654,11 +654,13 @@ php_EXTRA_DIST= \
php/tests/map_field_test.php \
php/tests/memory_leak_test.php \
php/tests/php_implementation_test.php \
php/tests/proto/test_empty_php_namespace.proto \
php/tests/proto/test_import_descriptor_proto.proto \
php/tests/proto/test_include.proto \
php/tests/proto/test.proto \
php/tests/proto/test_prefix.proto \
php/tests/proto/test_no_namespace.proto \
php/tests/proto/test_php_namespace.proto \
php/tests/proto/test_prefix.proto \
php/tests/test.sh \
php/tests/test_base.php \
php/tests/test_util.php \
......
......@@ -405,23 +405,34 @@ static const char *classname_prefix(const char *classname,
}
static void convert_to_class_name_inplace(const char *package,
const char *namespace_given,
const char *prefix, char *classname) {
size_t package_len = package == NULL ? 0 : strlen(package);
size_t prefix_len = prefix == NULL ? 0 : strlen(prefix);
size_t classname_len = strlen(classname);
int i = 0, j;
bool first_char = true;
int offset = package_len != 0 ? 2 : 0;
size_t package_len = package == NULL ? 0 : strlen(package);
size_t namespace_given_len =
namespace_given == NULL ? 0 : strlen(namespace_given);
bool use_namespace_given = namespace_given != NULL;
size_t namespace_len =
use_namespace_given ? namespace_given_len : package_len;
int offset = namespace_len != 0 ? 2 : 0;
for (j = 0; j < classname_len; j++) {
classname[package_len + prefix_len + classname_len + offset - 1 - j] =
classname[namespace_len + prefix_len + classname_len + offset - 1 - j] =
classname[classname_len - j - 1];
}
if (package_len != 0) {
if (namespace_len != 0) {
classname[i++] = '\\';
for (j = 0; j < package_len; j++) {
for (j = 0; j < namespace_len; j++) {
if (use_namespace_given) {
classname[i++] = namespace_given[j];
continue;
}
// php packages are divided by '\'.
if (package[j] == '.') {
classname[i++] = '\\';
......@@ -490,16 +501,20 @@ PHP_METHOD(DescriptorPool, internalAddGeneratedFile) {
* bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if \
* given message is google.protobuf.Empty.*/ \
const char *fullname = upb_##def_type_lower##_fullname(def_type_lower); \
const char *php_namespace = upb_filedef_phpnamespace(files[0]); \
const char *prefix_given = upb_filedef_phpprefix(files[0]); \
size_t classname_len = strlen(fullname) + 5; \
if (prefix_given != NULL) { \
classname_len += strlen(prefix_given); \
} \
if (php_namespace != NULL) { \
classname_len += strlen(php_namespace); \
} \
char *classname = ecalloc(sizeof(char), classname_len); \
const char *package = upb_filedef_package(files[0]); \
classname_no_prefix(fullname, package, classname); \
const char *prefix = classname_prefix(classname, prefix_given, package); \
convert_to_class_name_inplace(package, prefix, classname); \
convert_to_class_name_inplace(package, php_namespace, prefix, classname); \
PHP_PROTO_CE_DECLARE pce; \
if (php_proto_zend_lookup_class(classname, strlen(classname), &pce) == \
FAILURE) { \
......
This diff is collapsed.
......@@ -2973,10 +2973,16 @@ class upb::FileDef {
bool set_package(const char* package, Status* s);
/* Sets the php class prefix which is prepended to all php generated classes
/ from this .proto. Default is empty. */
* from this .proto. Default is empty. */
const char* phpprefix() const;
bool set_phpprefix(const char* phpprefix, Status* s);
/* 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. */
const char* phpnamespace() const;
bool set_phpnamespace(const char* phpnamespace, Status* s);
/* Syntax for the file. Defaults to proto2. */
upb_syntax_t syntax() const;
void set_syntax(upb_syntax_t syntax);
......@@ -3031,6 +3037,7 @@ UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast)
const char *upb_filedef_name(const upb_filedef *f);
const char *upb_filedef_package(const upb_filedef *f);
const char *upb_filedef_phpprefix(const upb_filedef *f);
const char *upb_filedef_phpnamespace(const upb_filedef *f);
upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
size_t upb_filedef_defcount(const upb_filedef *f);
size_t upb_filedef_depcount(const upb_filedef *f);
......@@ -3042,6 +3049,8 @@ bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s);
bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s);
bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix,
upb_status *s);
bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace,
upb_status *s);
bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s);
bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
......@@ -3806,6 +3815,12 @@ inline const char* FileDef::phpprefix() const {
inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) {
return upb_filedef_setphpprefix(this, phpprefix, s);
}
inline const char* FileDef::phpnamespace() const {
return upb_filedef_phpnamespace(this);
}
inline bool FileDef::set_phpnamespace(const char* phpnamespace, Status* s) {
return upb_filedef_setphpnamespace(this, phpnamespace, s);
}
inline int FileDef::def_count() const {
return upb_filedef_defcount(this);
}
......@@ -4021,6 +4036,7 @@ struct upb_filedef {
const char *name;
const char *package;
const char *phpprefix;
const char *phpnamespace;
upb_syntax_t syntax;
upb_inttable defs;
......@@ -7228,6 +7244,7 @@ UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_us
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 40); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 41); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); }
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); }
......@@ -8326,16 +8343,8 @@ UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
return ret;
}
/* Four functions for decoding a varint of at most eight bytes. They are all
* functionally identical, but are implemented in different ways and likely have
* different performance profiles. We keep them around for performance testing.
*
* Note that these functions may not read byte-by-byte, so they must not be used
* unless there are at least eight bytes left in the buffer! */
upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
upb_decoderet upb_vdecode_max8_wright(upb_decoderet r);
upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r);
/* Template for a function that checks the first two bytes with branching
* and dispatches 2-10 bytes with a separate function. Note that this may read
......@@ -8360,8 +8369,6 @@ UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright)
UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino)
#undef UPB_VARINT_DECODER_CHECK2
/* Our canonical functions for decoding varints, based on the currently
......@@ -8373,10 +8380,6 @@ UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
return upb_vdecode_check2_branch32(p);
}
UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) {
return upb_vdecode_max8_massimino(r);
}
/* Encoding *******************************************************************/
......
......@@ -145,6 +145,7 @@ class Descriptor
->optional('csharp_namespace', \Google\Protobuf\Internal\GPBType::STRING, 37)
->optional('swift_prefix', \Google\Protobuf\Internal\GPBType::STRING, 39)
->optional('php_class_prefix', \Google\Protobuf\Internal\GPBType::STRING, 40)
->optional('php_namespace', \Google\Protobuf\Internal\GPBType::STRING, 41)
->repeated('uninterpreted_option', \Google\Protobuf\Internal\GPBType::MESSAGE, 999, 'google.protobuf.internal.UninterpretedOption')
->finalizeToPool();
......
......@@ -97,6 +97,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -120,6 +122,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->field = $arr;
$this->has_field = true;
return $this;
}
public function hasField()
......@@ -143,6 +147,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $arr;
$this->has_extension = true;
return $this;
}
public function hasExtension()
......@@ -166,6 +172,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->nested_type = $arr;
$this->has_nested_type = true;
return $this;
}
public function hasNestedType()
......@@ -189,6 +197,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $arr;
$this->has_enum_type = true;
return $this;
}
public function hasEnumType()
......@@ -212,6 +222,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ExtensionRange::class);
$this->extension_range = $arr;
$this->has_extension_range = true;
return $this;
}
public function hasExtensionRange()
......@@ -235,6 +247,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\OneofDescriptorProto::class);
$this->oneof_decl = $arr;
$this->has_oneof_decl = true;
return $this;
}
public function hasOneofDecl()
......@@ -258,6 +272,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MessageOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......@@ -281,6 +297,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ReservedRange::class);
$this->reserved_range = $arr;
$this->has_reserved_range = true;
return $this;
}
public function hasReservedRange()
......@@ -314,6 +332,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->reserved_name = $arr;
$this->has_reserved_name = true;
return $this;
}
public function hasReservedName()
......
......@@ -48,6 +48,8 @@ class DescriptorProto_ExtensionRange extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->start = $var;
$this->has_start = true;
return $this;
}
public function hasStart()
......@@ -71,6 +73,8 @@ class DescriptorProto_ExtensionRange extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->end = $var;
$this->has_end = true;
return $this;
}
public function hasEnd()
......
......@@ -70,6 +70,8 @@ class DescriptorProto_ReservedRange extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->start = $var;
$this->has_start = true;
return $this;
}
public function hasStart()
......@@ -101,6 +103,8 @@ class DescriptorProto_ReservedRange extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->end = $var;
$this->has_end = true;
return $this;
}
public function hasEnd()
......
......@@ -57,6 +57,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -80,6 +82,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumValueDescriptorProto::class);
$this->value = $arr;
$this->has_value = true;
return $this;
}
public function hasValue()
......@@ -103,6 +107,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......
......@@ -79,6 +79,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->allow_alias = $var;
$this->has_allow_alias = true;
return $this;
}
public function hasAllowAlias()
......@@ -116,6 +118,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -147,6 +151,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -57,6 +57,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -80,6 +82,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->number = $var;
$this->has_number = true;
return $this;
}
public function hasNumber()
......@@ -103,6 +107,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumValueOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......
......@@ -73,6 +73,8 @@ class EnumValueOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -104,6 +106,8 @@ class EnumValueOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -130,6 +130,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -153,6 +155,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->number = $var;
$this->has_number = true;
return $this;
}
public function hasNumber()
......@@ -176,6 +180,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldDescriptorProto_Label::class);
$this->label = $var;
$this->has_label = true;
return $this;
}
public function hasLabel()
......@@ -209,6 +215,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldDescriptorProto_Type::class);
$this->type = $var;
$this->has_type = true;
return $this;
}
public function hasType()
......@@ -248,6 +256,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->type_name = $var;
$this->has_type_name = true;
return $this;
}
public function hasTypeName()
......@@ -281,6 +291,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->extendee = $var;
$this->has_extendee = true;
return $this;
}
public function hasExtendee()
......@@ -320,6 +332,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->default_value = $var;
$this->has_default_value = true;
return $this;
}
public function hasDefaultValue()
......@@ -353,6 +367,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->oneof_index = $var;
$this->has_oneof_index = true;
return $this;
}
public function hasOneofIndex()
......@@ -390,6 +406,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->json_name = $var;
$this->has_json_name = true;
return $this;
}
public function hasJsonName()
......@@ -413,6 +431,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FieldOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......
......@@ -156,6 +156,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldOptions_CType::class);
$this->ctype = $var;
$this->has_ctype = true;
return $this;
}
public function hasCtype()
......@@ -195,6 +197,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->packed = $var;
$this->has_packed = true;
return $this;
}
public function hasPacked()
......@@ -242,6 +246,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldOptions_JSType::class);
$this->jstype = $var;
$this->has_jstype = true;
return $this;
}
public function hasJstype()
......@@ -319,6 +325,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->lazy = $var;
$this->has_lazy = true;
return $this;
}
public function hasLazy()
......@@ -356,6 +364,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -387,6 +397,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->weak = $var;
$this->has_weak = true;
return $this;
}
public function hasWeak()
......@@ -418,6 +430,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -147,6 +147,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -178,6 +180,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->package = $var;
$this->has_package = true;
return $this;
}
public function hasPackage()
......@@ -209,6 +213,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->dependency = $arr;
$this->has_dependency = true;
return $this;
}
public function hasDependency()
......@@ -240,6 +246,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->public_dependency = $arr;
$this->has_public_dependency = true;
return $this;
}
public function hasPublicDependency()
......@@ -273,6 +281,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->weak_dependency = $arr;
$this->has_weak_dependency = true;
return $this;
}
public function hasWeakDependency()
......@@ -304,6 +314,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->message_type = $arr;
$this->has_message_type = true;
return $this;
}
public function hasMessageType()
......@@ -327,6 +339,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $arr;
$this->has_enum_type = true;
return $this;
}
public function hasEnumType()
......@@ -350,6 +364,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\ServiceDescriptorProto::class);
$this->service = $arr;
$this->has_service = true;
return $this;
}
public function hasService()
......@@ -373,6 +389,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $arr;
$this->has_extension = true;
return $this;
}
public function hasExtension()
......@@ -396,6 +414,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FileOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......@@ -433,6 +453,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\SourceCodeInfo::class);
$this->source_code_info = $var;
$this->has_source_code_info = true;
return $this;
}
public function hasSourceCodeInfo()
......@@ -466,6 +488,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->syntax = $var;
$this->has_syntax = true;
return $this;
}
public function hasSyntax()
......
......@@ -48,6 +48,8 @@ class FileDescriptorSet extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FileDescriptorProto::class);
$this->file = $arr;
$this->has_file = true;
return $this;
}
public function hasFile()
......
......@@ -186,6 +186,17 @@ class FileOptions extends \Google\Protobuf\Internal\Message
*/
private $php_class_prefix = '';
private $has_php_class_prefix = false;
/**
* <pre>
* 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.
* </pre>
*
* <code>optional string php_namespace = 41;</code>
*/
private $php_namespace = '';
private $has_php_namespace = false;
/**
* <pre>
* The parser stores options it doesn't recognize here. See above.
......@@ -231,6 +242,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->java_package = $var;
$this->has_java_package = true;
return $this;
}
public function hasJavaPackage()
......@@ -270,6 +283,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->java_outer_classname = $var;
$this->has_java_outer_classname = true;
return $this;
}
public function hasJavaOuterClassname()
......@@ -311,6 +326,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->java_multiple_files = $var;
$this->has_java_multiple_files = true;
return $this;
}
public function hasJavaMultipleFiles()
......@@ -342,6 +359,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->java_generate_equals_and_hash = $var;
$this->has_java_generate_equals_and_hash = true;
return $this;
}
public function hasJavaGenerateEqualsAndHash()
......@@ -383,6 +402,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->java_string_check_utf8 = $var;
$this->has_java_string_check_utf8 = true;
return $this;
}
public function hasJavaStringCheckUtf8()
......@@ -406,6 +427,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FileOptions_OptimizeMode::class);
$this->optimize_for = $var;
$this->has_optimize_for = true;
return $this;
}
public function hasOptimizeFor()
......@@ -445,6 +468,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->go_package = $var;
$this->has_go_package = true;
return $this;
}
public function hasGoPackage()
......@@ -492,6 +517,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->cc_generic_services = $var;
$this->has_cc_generic_services = true;
return $this;
}
public function hasCcGenericServices()
......@@ -515,6 +542,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->java_generic_services = $var;
$this->has_java_generic_services = true;
return $this;
}
public function hasJavaGenericServices()
......@@ -538,6 +567,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->py_generic_services = $var;
$this->has_py_generic_services = true;
return $this;
}
public function hasPyGenericServices()
......@@ -575,6 +606,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -608,6 +641,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->cc_enable_arenas = $var;
$this->has_cc_enable_arenas = true;
return $this;
}
public function hasCcEnableArenas()
......@@ -641,6 +676,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->objc_class_prefix = $var;
$this->has_objc_class_prefix = true;
return $this;
}
public function hasObjcClassPrefix()
......@@ -672,6 +709,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->csharp_namespace = $var;
$this->has_csharp_namespace = true;
return $this;
}
public function hasCsharpNamespace()
......@@ -709,6 +748,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->swift_prefix = $var;
$this->has_swift_prefix = true;
return $this;
}
public function hasSwiftPrefix()
......@@ -742,6 +783,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->php_class_prefix = $var;
$this->has_php_class_prefix = true;
return $this;
}
public function hasPhpClassPrefix()
......@@ -749,6 +792,43 @@ class FileOptions extends \Google\Protobuf\Internal\Message
return $this->has_php_class_prefix;
}
/**
* <pre>
* 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.
* </pre>
*
* <code>optional string php_namespace = 41;</code>
*/
public function getPhpNamespace()
{
return $this->php_namespace;
}
/**
* <pre>
* 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.
* </pre>
*
* <code>optional string php_namespace = 41;</code>
*/
public function setPhpNamespace($var)
{
GPBUtil::checkString($var, True);
$this->php_namespace = $var;
$this->has_php_namespace = true;
return $this;
}
public function hasPhpNamespace()
{
return $this->has_php_namespace;
}
/**
* <pre>
* The parser stores options it doesn't recognize here. See above.
......@@ -773,6 +853,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -64,6 +64,8 @@ class GeneratedCodeInfo extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\GeneratedCodeInfo_Annotation::class);
$this->annotation = $arr;
$this->has_annotation = true;
return $this;
}
public function hasAnnotation()
......
......@@ -88,6 +88,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $arr;
$this->has_path = true;
return $this;
}
public function hasPath()
......@@ -119,6 +121,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->source_file = $var;
$this->has_source_file = true;
return $this;
}
public function hasSourceFile()
......@@ -152,6 +156,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->begin = $var;
$this->has_begin = true;
return $this;
}
public function hasBegin()
......@@ -187,6 +193,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt32($var);
$this->end = $var;
$this->has_end = true;
return $this;
}
public function hasEnd()
......
......@@ -155,6 +155,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->message_set_wire_format = $var;
$this->has_message_set_wire_format = true;
return $this;
}
public function hasMessageSetWireFormat()
......@@ -190,6 +192,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->no_standard_descriptor_accessor = $var;
$this->has_no_standard_descriptor_accessor = true;
return $this;
}
public function hasNoStandardDescriptorAccessor()
......@@ -227,6 +231,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -292,6 +298,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->map_entry = $var;
$this->has_map_entry = true;
return $this;
}
public function hasMapEntry()
......@@ -323,6 +331,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -85,6 +85,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -118,6 +120,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->input_type = $var;
$this->has_input_type = true;
return $this;
}
public function hasInputType()
......@@ -141,6 +145,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->output_type = $var;
$this->has_output_type = true;
return $this;
}
public function hasOutputType()
......@@ -164,6 +170,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MethodOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......@@ -195,6 +203,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->client_streaming = $var;
$this->has_client_streaming = true;
return $this;
}
public function hasClientStreaming()
......@@ -226,6 +236,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->server_streaming = $var;
$this->has_server_streaming = true;
return $this;
}
public function hasServerStreaming()
......
......@@ -78,6 +78,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -101,6 +103,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkEnum($var, \Google\Protobuf\Internal\MethodOptions_IdempotencyLevel::class);
$this->idempotency_level = $var;
$this->has_idempotency_level = true;
return $this;
}
public function hasIdempotencyLevel()
......@@ -132,6 +136,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -52,6 +52,8 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -75,6 +77,8 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\OneofOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......
......@@ -55,6 +55,8 @@ class OneofOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -57,6 +57,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name = $var;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -80,6 +82,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\MethodDescriptorProto::class);
$this->method = $arr;
$this->has_method = true;
return $this;
}
public function hasMethod()
......@@ -103,6 +107,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message
GPBUtil::checkMessage($var, \Google\Protobuf\Internal\ServiceOptions::class);
$this->options = $var;
$this->has_options = true;
return $this;
}
public function hasOptions()
......
......@@ -73,6 +73,8 @@ class ServiceOptions extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->deprecated = $var;
$this->has_deprecated = true;
return $this;
}
public function hasDeprecated()
......@@ -104,6 +106,8 @@ class ServiceOptions extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
return $this;
}
public function hasUninterpretedOption()
......
......@@ -180,6 +180,8 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\SourceCodeInfo_Location::class);
$this->location = $arr;
$this->has_location = true;
return $this;
}
public function hasLocation()
......
......@@ -182,6 +182,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $arr;
$this->has_path = true;
return $this;
}
public function hasPath()
......@@ -221,6 +223,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->span = $arr;
$this->has_span = true;
return $this;
}
public function hasSpan()
......@@ -322,6 +326,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->leading_comments = $var;
$this->has_leading_comments = true;
return $this;
}
public function hasLeadingComments()
......@@ -345,6 +351,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->trailing_comments = $var;
$this->has_trailing_comments = true;
return $this;
}
public function hasTrailingComments()
......@@ -368,6 +376,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->leading_detached_comments = $arr;
$this->has_leading_detached_comments = true;
return $this;
}
public function hasLeadingDetachedComments()
......
......@@ -87,6 +87,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption_NamePart::class);
$this->name = $arr;
$this->has_name = true;
return $this;
}
public function hasName()
......@@ -120,6 +122,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->identifier_value = $var;
$this->has_identifier_value = true;
return $this;
}
public function hasIdentifierValue()
......@@ -143,6 +147,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkUint64($var);
$this->positive_int_value = $var;
$this->has_positive_int_value = true;
return $this;
}
public function hasPositiveIntValue()
......@@ -166,6 +172,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkInt64($var);
$this->negative_int_value = $var;
$this->has_negative_int_value = true;
return $this;
}
public function hasNegativeIntValue()
......@@ -189,6 +197,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkDouble($var);
$this->double_value = $var;
$this->has_double_value = true;
return $this;
}
public function hasDoubleValue()
......@@ -212,6 +222,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, False);
$this->string_value = $var;
$this->has_string_value = true;
return $this;
}
public function hasStringValue()
......@@ -235,6 +247,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->aggregate_value = $var;
$this->has_aggregate_value = true;
return $this;
}
public function hasAggregateValue()
......
......@@ -56,6 +56,8 @@ class UninterpretedOption_NamePart extends \Google\Protobuf\Internal\Message
GPBUtil::checkString($var, True);
$this->name_part = $var;
$this->has_name_part = true;
return $this;
}
public function hasNamePart()
......@@ -79,6 +81,8 @@ class UninterpretedOption_NamePart extends \Google\Protobuf\Internal\Message
GPBUtil::checkBool($var);
$this->is_extension = $var;
$this->has_is_extension = true;
return $this;
}
public function hasIsExtension()
......
......@@ -282,6 +282,19 @@ function getFullClassName(
$class_name_without_package =
getClassNameWithoutPackage($message_name_without_package, $file_proto);
$option = $file_proto->getOptions();
if (!is_null($option) && $option->hasPhpNamespace()) {
$namespace = $option->getPhpNamespace();
if ($namespace !== "") {
$classname = $namespace . "\\" . $class_name_without_package;
return;
} else {
$classname = $class_name_without_package;
return;
}
}
if ($package === "") {
$classname = $class_name_without_package;
} else {
......
......@@ -9,9 +9,11 @@ use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\MapField;
use Google\Protobuf\Internal\GPBType;
use Foo\TestEnum;
use Foo\TestIncludeNamespaceMessage;
use Foo\TestIncludePrefixMessage;
use Foo\TestMessage;
use Foo\TestMessage_Sub;
use Php\Test\TestNamespace;
class GeneratedClassTest extends TestBase
{
......@@ -867,6 +869,25 @@ class GeneratedClassTest extends TestBase
$this->assertSame(1, $m->getPrefixMessage()->getA());
}
#########################################################
# Test message with given namespace.
#########################################################
public function testNamespaceMessage()
{
$m = new TestIncludeNamespaceMessage();
$n = new TestNamespace();
$n->setA(1);
$m->setNamespaceMessage($n);
$this->assertSame(1, $m->getNamespaceMessage()->getA());
$n = new TestEmptyNamespace();
$n->setA(1);
$m->setEmptyNamespaceMessage($n);
$this->assertSame(1, $m->getEmptyNamespaceMessage()->getA());
}
#########################################################
# Test prefix for reserved words.
#########################################################
......
......@@ -7,10 +7,12 @@ require_once('generated/NoNamespaceMessage.php');
require_once('generated/NoNamespaceMessage_NestedEnum.php');
require_once('generated/PrefixEmpty.php');
require_once('generated/PrefixTestPrefix.php');
require_once('generated/TestEmptyNamespace.php');
require_once('generated/Bar/TestInclude.php');
require_once('generated/Foo/PBARRAY.php');
require_once('generated/Foo/PBEmpty.php');
require_once('generated/Foo/TestEnum.php');
require_once('generated/Foo/TestIncludeNamespaceMessage.php');
require_once('generated/Foo/TestIncludePrefixMessage.php');
require_once('generated/Foo/TestMessage.php');
require_once('generated/Foo/TestMessage_Empty.php');
......@@ -20,9 +22,12 @@ require_once('generated/Foo/TestPackedMessage.php');
require_once('generated/Foo/TestPhpDoc.php');
require_once('generated/Foo/TestUnpackedMessage.php');
require_once('generated/GPBMetadata/Proto/Test.php');
require_once('generated/GPBMetadata/Proto/TestEmptyPhpNamespace.php');
require_once('generated/GPBMetadata/Proto/TestInclude.php');
require_once('generated/GPBMetadata/Proto/TestNoNamespace.php');
require_once('generated/GPBMetadata/Proto/TestPhpNamespace.php');
require_once('generated/GPBMetadata/Proto/TestPrefix.php');
require_once('generated/Php/Test/TestNamespace.php');
require_once('test_util.php');
use Google\Protobuf\Internal\RepeatedField;
......
......@@ -2,6 +2,8 @@ syntax = "proto3";
import 'proto/test_include.proto';
import 'proto/test_no_namespace.proto';
import 'proto/test_php_namespace.proto';
import 'proto/test_empty_php_namespace.proto';
import 'proto/test_prefix.proto';
package foo;
......@@ -174,3 +176,8 @@ message TestPhpDoc {
message TestIncludePrefixMessage {
TestPrefix prefix_message = 1;
}
message TestIncludeNamespaceMessage {
TestNamespace namespace_message = 1;
TestEmptyNamespace empty_namespace_message = 2;
}
syntax = "proto3";
package foo;
option php_namespace = "";
message TestEmptyNamespace {
int32 a = 1;
}
syntax = "proto3";
package foo;
option php_namespace = "Php\\Test";
message TestNamespace {
int32 a = 1;
}
......@@ -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));
......
This diff is collapsed.
This diff is collapsed.
......@@ -380,6 +380,11 @@ message FileOptions {
// from this .proto. Default is empty.
optional string php_class_prefix = 40;
// 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.
optional string php_namespace = 41;
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
......
......@@ -346,7 +346,7 @@ generate_php_test_proto() {
# Generate test file
rm -rf generated
mkdir generated
../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto
../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto proto/test_php_namespace.proto proto/test_empty_php_namespace.proto
pushd ../../src
./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment