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

Add file option php_class_prefix (#2849)

This option will be prepended to generated classes of all messages in
the containing file.
parent c0871aa4
......@@ -584,6 +584,21 @@ objectivec_EXTRA_DIST= \
Protobuf.podspec
php_EXTRA_DIST= \
php/ext/google/protobuf/utf8.h \
php/ext/google/protobuf/message.c \
php/ext/google/protobuf/utf8.c \
php/ext/google/protobuf/package.xml \
php/ext/google/protobuf/upb.h \
php/ext/google/protobuf/array.c \
php/ext/google/protobuf/encode_decode.c \
php/ext/google/protobuf/protobuf.h \
php/ext/google/protobuf/type_check.c \
php/ext/google/protobuf/def.c \
php/ext/google/protobuf/storage.c \
php/ext/google/protobuf/map.c \
php/ext/google/protobuf/config.m4 \
php/ext/google/protobuf/upb.c \
php/ext/google/protobuf/protobuf.c \
php/src/phpdoc.dist.xml \
php/src/Google/Protobuf/Internal/DescriptorPool.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \
......@@ -632,39 +647,26 @@ php_EXTRA_DIST= \
php/src/Google/Protobuf/Internal/FieldOptions_CType.php \
php/src/Google/Protobuf/descriptor.php \
php/src/GPBMetadata/Google/Protobuf/Internal/Descriptor.php \
php/tests/array_test.php \
php/tests/autoload.php \
php/tests/encode_decode_test.php \
php/tests/test.sh \
php/tests/gdb_test.sh \
php/tests/generated_class_test.php \
php/tests/array_test.php \
php/tests/map_field_test.php \
php/tests/memory_leak_test.php \
php/tests/php_implementation_test.php \
php/tests/proto/test_include.proto \
php/tests/map_field_test.php \
php/tests/test_base.php \
php/tests/proto/test.proto \
php/tests/proto/test_prefix.proto \
php/tests/proto/test_no_namespace.proto \
php/tests/test.sh \
php/tests/test_base.php \
php/tests/test_util.php \
php/tests/proto/test.proto \
php/tests/memory_leak_test.php \
php/tests/well_known_test.php \
php/README.md \
php/ext/google/protobuf/utf8.h \
php/ext/google/protobuf/message.c \
php/ext/google/protobuf/utf8.c \
php/ext/google/protobuf/package.xml \
php/ext/google/protobuf/upb.h \
php/ext/google/protobuf/array.c \
php/ext/google/protobuf/encode_decode.c \
php/ext/google/protobuf/protobuf.h \
php/ext/google/protobuf/type_check.c \
php/ext/google/protobuf/def.c \
php/ext/google/protobuf/storage.c \
php/ext/google/protobuf/map.c \
php/ext/google/protobuf/config.m4 \
php/ext/google/protobuf/upb.c \
php/ext/google/protobuf/protobuf.c \
php/phpunit.xml \
php/composer.json \
php/generate_descriptor_protos.sh \
composer.json
python_EXTRA_DIST= \
......
......@@ -104,3 +104,8 @@ if test -x csharp/generate_protos.sh; then
echo "Generating messages for C#."
csharp/generate_protos.sh $@
fi
if test -x php/generate_descriptor_protos.sh; then
echo "Generating messages for PHP."
php/generate_descriptor_protos.sh $@
fi
......@@ -18,7 +18,8 @@
"Google\\Protobuf\\": "tests/generated/Google/Protobuf",
"Google\\Protobuf\\Internal\\": "src/Google/Protobuf/Internal",
"GPBMetadata\\": "tests/generated/GPBMetadata",
"GPBMetadata\\Google\\Protobuf\\Internal\\": "src/GPBMetadata/Google/Protobuf/Internal"
"GPBMetadata\\Google\\Protobuf\\Internal\\": "src/GPBMetadata/Google/Protobuf/Internal",
"": "tests/generated"
},
"files": [
"src/Google/Protobuf/descriptor.php"
......
......@@ -252,39 +252,54 @@ PHP_METHOD(DescriptorPool, getGeneratedPool) {
static void convert_to_class_name_inplace(char *class_name,
const char* fullname,
const char* prefix,
const char* package_name) {
size_t i;
bool first_char = false;
size_t i = 0, j;
bool first_char = true;
size_t pkg_name_len = package_name == NULL ? 0 : strlen(package_name);
size_t prefix_len = prefix == NULL ? 0 : strlen(prefix);
size_t message_name_start = package_name == NULL ? 0 : pkg_name_len + 1;
size_t message_len = (strlen(fullname) - message_name_start);
// In php, class name cannot be Empty.
if (strcmp("google.protobuf.Empty", fullname) == 0) {
fullname = "google.protobuf.GPBEmpty";
strcpy(class_name, "\\Google\\Protobuf\\GPBEmpty");
return;
}
if (pkg_name_len == 0) {
strcpy(class_name, fullname);
} else {
class_name[0] = '.';
strcpy(&class_name[1], fullname);
for (i = 0; i <= pkg_name_len + 1; i++) {
// PHP package uses camel case.
if (!first_char && class_name[i] != '.') {
if (pkg_name_len != 0) {
class_name[i++] = '\\';
for (j = 0; j < pkg_name_len; j++) {
// php packages are divided by '\'.
if (package_name[j] == '.') {
class_name[i++] = '\\';
first_char = true;
class_name[i] += 'A' - 'a';
} else if (first_char) {
// PHP package uses camel case.
if (package_name[j] < 'A' || package_name[j] > 'Z') {
class_name[i++] = package_name[j] + 'A' - 'a';
} else {
class_name[i++] = package_name[j];
}
// php packages are divided by '\'.
if (class_name[i] == '.') {
first_char = false;
class_name[i] = '\\';
} else {
class_name[i++] = package_name[j];
}
}
class_name[i++] = '\\';
}
if (prefix_len > 0) {
strcpy(class_name + i, prefix);
i += prefix_len;
}
// Submessage is concatenated with its containing messages by '_'.
for (i = pkg_name_len; i < strlen(class_name); i++) {
if (class_name[i] == '.') {
class_name[i] = '_';
for (j = message_name_start; j < message_name_start + message_len; j++) {
if (fullname[j] == '.') {
class_name[i++] = '_';
} else {
class_name[i++] = fullname[j];
}
}
}
......@@ -339,8 +354,13 @@ 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); \
char *klass_name = ecalloc(sizeof(char), 5 + strlen(fullname)); \
convert_to_class_name_inplace(klass_name, fullname, \
const char *prefix = upb_filedef_phpprefix(files[0]); \
size_t klass_name_len = strlen(fullname) + 5; \
if (prefix != NULL) { \
klass_name_len += strlen(prefix); \
} \
char *klass_name = ecalloc(sizeof(char), klass_name_len); \
convert_to_class_name_inplace(klass_name, fullname, prefix, \
upb_filedef_package(files[0])); \
zend_class_entry **pce; \
if (zend_lookup_class(klass_name, strlen(klass_name), &pce TSRMLS_CC) == \
......
This diff is collapsed.
......@@ -2972,6 +2972,11 @@ class upb::FileDef {
const char* package() const;
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. */
const char* phpprefix() const;
bool set_phpprefix(const char* phpprefix, Status* s);
/* Syntax for the file. Defaults to proto2. */
upb_syntax_t syntax() const;
void set_syntax(upb_syntax_t syntax);
......@@ -3025,6 +3030,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);
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);
......@@ -3034,6 +3040,8 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i);
bool upb_filedef_freeze(upb_filedef *f, upb_status *s);
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_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,
......@@ -3792,6 +3800,12 @@ inline const char* FileDef::package() const {
inline bool FileDef::set_package(const char* package, Status* s) {
return upb_filedef_setpackage(this, package, s);
}
inline const char* FileDef::phpprefix() const {
return upb_filedef_phpprefix(this);
}
inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) {
return upb_filedef_setphpprefix(this, phpprefix, s);
}
inline int FileDef::def_count() const {
return upb_filedef_defcount(this);
}
......@@ -4006,6 +4020,7 @@ struct upb_filedef {
const char *name;
const char *package;
const char *phpprefix;
upb_syntax_t syntax;
upb_inttable defs;
......@@ -7212,6 +7227,7 @@ UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string
UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); }
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_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); }
......
#!/usr/bin/env bash
# Run this script to regenerate desriptor protos after the protocol compiler
# changes.
if test ! -e src/google/protobuf/stubs/common.h; then
cat >&2 << __EOF__
Could not find source code. Make sure you are running this script from the
root of the distribution tree.
__EOF__
exit 1
fi
pushd src
./protoc --php_out=internal:../php/src google/protobuf/descriptor.proto
popd
......@@ -144,6 +144,7 @@ class Descriptor
->optional('objc_class_prefix', \Google\Protobuf\Internal\GPBType::STRING, 36)
->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)
->repeated('uninterpreted_option', \Google\Protobuf\Internal\GPBType::MESSAGE, 999, 'google.protobuf.internal.UninterpretedOption')
->finalizeToPool();
......
......@@ -117,8 +117,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setField(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->field = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->field = $arr;
$this->has_field = true;
}
......@@ -140,8 +140,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setExtension(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $arr;
$this->has_extension = true;
}
......@@ -163,8 +163,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setNestedType(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->nested_type = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->nested_type = $arr;
$this->has_nested_type = true;
}
......@@ -186,8 +186,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setEnumType(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $arr;
$this->has_enum_type = true;
}
......@@ -209,8 +209,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setExtensionRange(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ExtensionRange::class);
$this->extension_range = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ExtensionRange::class);
$this->extension_range = $arr;
$this->has_extension_range = true;
}
......@@ -232,8 +232,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setOneofDecl(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\OneofDescriptorProto::class);
$this->oneof_decl = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\OneofDescriptorProto::class);
$this->oneof_decl = $arr;
$this->has_oneof_decl = true;
}
......@@ -278,8 +278,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setReservedRange(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ReservedRange::class);
$this->reserved_range = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ReservedRange::class);
$this->reserved_range = $arr;
$this->has_reserved_range = true;
}
......@@ -311,8 +311,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setReservedName(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->reserved_name = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->reserved_name = $arr;
$this->has_reserved_name = true;
}
......
......@@ -77,8 +77,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setValue(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumValueDescriptorProto::class);
$this->value = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumValueDescriptorProto::class);
$this->value = $arr;
$this->has_value = true;
}
......
......@@ -144,8 +144,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -101,8 +101,8 @@ class EnumValueOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -2,11 +2,11 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* Protobuf enum <code>google.protobuf.FieldDescriptorProto.Label</code>
*/
namespace Google\Protobuf\Internal;
class FieldDescriptorProto_Label
{
/**
......
......@@ -2,11 +2,11 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* Protobuf enum <code>google.protobuf.FieldDescriptorProto.Type</code>
*/
namespace Google\Protobuf\Internal;
class FieldDescriptorProto_Type
{
/**
......@@ -63,6 +63,9 @@ class FieldDescriptorProto_Type
/**
* <pre>
* Tag-delimited aggregate.
* Group type is deprecated and not supported in proto3. However, Proto3
* implementations should still be able to parse the group wire format and
* treat group fields as unknown fields.
* </pre>
*
* <code>TYPE_GROUP = 10;</code>
......
......@@ -415,8 +415,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -2,11 +2,11 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* Protobuf enum <code>google.protobuf.FieldOptions.CType</code>
*/
namespace Google\Protobuf\Internal;
class FieldOptions_CType
{
/**
......
......@@ -2,11 +2,11 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* Protobuf enum <code>google.protobuf.FieldOptions.JSType</code>
*/
namespace Google\Protobuf\Internal;
class FieldOptions_JSType
{
/**
......
......@@ -206,8 +206,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setDependency(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->dependency = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->dependency = $arr;
$this->has_dependency = true;
}
......@@ -237,8 +237,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setPublicDependency(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->public_dependency = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->public_dependency = $arr;
$this->has_public_dependency = true;
}
......@@ -270,8 +270,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setWeakDependency(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->weak_dependency = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->weak_dependency = $arr;
$this->has_weak_dependency = true;
}
......@@ -301,8 +301,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setMessageType(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->message_type = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class);
$this->message_type = $arr;
$this->has_message_type = true;
}
......@@ -324,8 +324,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setEnumType(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class);
$this->enum_type = $arr;
$this->has_enum_type = true;
}
......@@ -347,8 +347,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setService(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\ServiceDescriptorProto::class);
$this->service = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\ServiceDescriptorProto::class);
$this->service = $arr;
$this->has_service = true;
}
......@@ -370,8 +370,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setExtension(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class);
$this->extension = $arr;
$this->has_extension = true;
}
......
......@@ -45,8 +45,8 @@ class FileDescriptorSet extends \Google\Protobuf\Internal\Message
*/
public function setFile(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FileDescriptorProto::class);
$this->file = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FileDescriptorProto::class);
$this->file = $arr;
$this->has_file = true;
}
......
......@@ -176,6 +176,16 @@ class FileOptions extends \Google\Protobuf\Internal\Message
*/
private $swift_prefix = '';
private $has_swift_prefix = false;
/**
* <pre>
* Sets the php class prefix which is prepended to all php generated classes
* from this .proto. Default is empty.
* </pre>
*
* <code>optional string php_class_prefix = 40;</code>
*/
private $php_class_prefix = '';
private $has_php_class_prefix = false;
/**
* <pre>
* The parser stores options it doesn't recognize here. See above.
......@@ -706,6 +716,39 @@ class FileOptions extends \Google\Protobuf\Internal\Message
return $this->has_swift_prefix;
}
/**
* <pre>
* Sets the php class prefix which is prepended to all php generated classes
* from this .proto. Default is empty.
* </pre>
*
* <code>optional string php_class_prefix = 40;</code>
*/
public function getPhpClassPrefix()
{
return $this->php_class_prefix;
}
/**
* <pre>
* Sets the php class prefix which is prepended to all php generated classes
* from this .proto. Default is empty.
* </pre>
*
* <code>optional string php_class_prefix = 40;</code>
*/
public function setPhpClassPrefix($var)
{
GPBUtil::checkString($var, True);
$this->php_class_prefix = $var;
$this->has_php_class_prefix = true;
}
public function hasPhpClassPrefix()
{
return $this->has_php_class_prefix;
}
/**
* <pre>
* The parser stores options it doesn't recognize here. See above.
......@@ -727,8 +770,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -2,6 +2,8 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* <pre>
* Generated classes can be optimized for speed or code size.
......@@ -9,8 +11,6 @@
*
* Protobuf enum <code>google.protobuf.FileOptions.OptimizeMode</code>
*/
namespace Google\Protobuf\Internal;
class FileOptions_OptimizeMode
{
/**
......
......@@ -61,8 +61,8 @@ class GeneratedCodeInfo extends \Google\Protobuf\Internal\Message
*/
public function setAnnotation(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\GeneratedCodeInfo_Annotation::class);
$this->annotation = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\GeneratedCodeInfo_Annotation::class);
$this->annotation = $arr;
$this->has_annotation = true;
}
......
......@@ -85,8 +85,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message
*/
public function setPath(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $arr;
$this->has_path = true;
}
......
......@@ -320,8 +320,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -129,8 +129,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -2,6 +2,8 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: google/protobuf/descriptor.proto
namespace Google\Protobuf\Internal;
/**
* <pre>
* Is this method side-effect-free (or safe in HTTP parlance), or idempotent,
......@@ -11,8 +13,6 @@
*
* Protobuf enum <code>google.protobuf.MethodOptions.IdempotencyLevel</code>
*/
namespace Google\Protobuf\Internal;
class MethodOptions_IdempotencyLevel
{
/**
......
......@@ -52,8 +52,8 @@ class OneofOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -77,8 +77,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message
*/
public function setMethod(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\MethodDescriptorProto::class);
$this->method = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\MethodDescriptorProto::class);
$this->method = $arr;
$this->has_method = true;
}
......
......@@ -101,8 +101,8 @@ class ServiceOptions extends \Google\Protobuf\Internal\Message
*/
public function setUninterpretedOption(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class);
$this->uninterpreted_option = $arr;
$this->has_uninterpreted_option = true;
}
......
......@@ -177,8 +177,8 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message
*/
public function setLocation(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\SourceCodeInfo_Location::class);
$this->location = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\SourceCodeInfo_Location::class);
$this->location = $arr;
$this->has_location = true;
}
......
......@@ -179,8 +179,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
*/
public function setPath(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->path = $arr;
$this->has_path = true;
}
......@@ -218,8 +218,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
*/
public function setSpan(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->span = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32);
$this->span = $arr;
$this->has_span = true;
}
......@@ -365,8 +365,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message
*/
public function setLeadingDetachedComments(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->leading_detached_comments = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
$this->leading_detached_comments = $arr;
$this->has_leading_detached_comments = true;
}
......
......@@ -84,8 +84,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message
*/
public function setName(&$var)
{
GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption_NamePart::class);
$this->name = $var;
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption_NamePart::class);
$this->name = $arr;
$this->has_name = true;
}
......
......@@ -78,14 +78,14 @@ class FileDescriptor
$file->setPackage($proto->getPackage());
foreach ($proto->getMessageType() as $message_proto) {
$file->addMessageType(Descriptor::buildFromProto(
$message_proto, $file->getPackage(), ""));
$message_proto, $proto, ""));
}
foreach ($proto->getEnumType() as $enum_proto) {
$file->getEnumType()[] =
$file->addEnumType(
EnumDescriptor::buildFromProto(
$enum_proto,
$file->getPackage(),
$proto,
""));
}
return $file;
......@@ -182,7 +182,7 @@ class Descriptor
return $this->options;
}
public static function buildFromProto($proto, $package, $containing)
public static function buildFromProto($proto, $file_proto, $containing)
{
$desc = new Descriptor();
......@@ -192,7 +192,7 @@ class Descriptor
getFullClassName(
$proto,
$containing,
$package,
$file_proto,
$message_name_without_package,
$classname,
$fullname);
......@@ -207,7 +207,7 @@ class Descriptor
// Handle nested types.
foreach ($proto->getNestedType() as $nested_proto) {
$desc->addNestedType(Descriptor::buildFromProto(
$nested_proto, $package, $message_name_without_package));
$nested_proto, $file_proto, $message_name_without_package));
}
// Handle oneof fields.
......@@ -220,43 +220,46 @@ class Descriptor
}
}
function addPrefixIfSpecial(
function getClassNameWithoutPackage(
$name,
$package)
$file_proto)
{
if ($name === "Empty" && $package === "google.protobuf") {
if ($name === "Empty" && $file_proto->getPackage() === "google.protobuf") {
return "GPBEmpty";
} else {
return $name;
$option = $file_proto->getOptions();
$prefix = is_null($option) ? "" : $option->getPhpClassPrefix();
// Nested message class names are seperated by '_', and package names
// are seperated by '\'.
return $prefix . implode('_', array_map('ucwords',
explode('.', $name)));
}
}
function getFullClassName(
$proto,
$containing,
$package,
$file_proto,
&$message_name_without_package,
&$classname,
&$fullname)
{
// Full name needs to start with '.'.
$message_name_without_package =
addPrefixIfSpecial($proto->getName(), $package);
$message_name_without_package = $proto->getName();
if ($containing !== "") {
$message_name_without_package =
$containing . "." . $message_name_without_package;
}
$package = $file_proto->getPackage();
if ($package === "") {
$fullname = "." . $message_name_without_package;
} else {
$fullname = "." . $package . "." . $message_name_without_package;
}
// Nested message class names are seperated by '_', and package names are
// seperated by '\'.
$class_name_without_package =
implode('_', array_map('ucwords',
explode('.', $message_name_without_package)));
getClassNameWithoutPackage($message_name_without_package, $file_proto);
if ($package === "") {
$classname = $class_name_without_package;
} else {
......@@ -333,7 +336,7 @@ class EnumDescriptor
return $this->klass;
}
public static function buildFromProto($proto, $package, $containing)
public static function buildFromProto($proto, $file_proto, $containing)
{
$desc = new EnumDescriptor();
......@@ -343,7 +346,7 @@ class EnumDescriptor
getFullClassName(
$proto,
$containing,
$package,
$file_proto,
$enum_name_without_package,
$classname,
$fullname);
......
......@@ -194,7 +194,7 @@ class EncodeDecodeTest extends TestBase
{
$m = new TestMessage();
$m->setOptionalInt32(-1);
$data = $m->encode();
$data = $m->serializeToString();
$this->assertSame("08ffffffffffffffffff01", bin2hex($data));
}
......@@ -202,12 +202,12 @@ class EncodeDecodeTest extends TestBase
{
$m = new TestMessage();
$this->assertEquals(0, $m->getOptionalInt32());
$m->decode(hex2bin("08ffffffffffffffffff01"));
$m->mergeFromString(hex2bin("08ffffffffffffffffff01"));
$this->assertEquals(-1, $m->getOptionalInt32());
$m = new TestMessage();
$this->assertEquals(0, $m->getOptionalInt32());
$m->decode(hex2bin("08ffffffff0f"));
$m->mergeFromString(hex2bin("08ffffffff0f"));
$this->assertEquals(-1, $m->getOptionalInt32());
}
......
......@@ -3,7 +3,7 @@
# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which
# phpunit` --bootstrap autoload.php tmp_test.php
#
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php array_test.php
#
# # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so
# memory_leak_test.php
......
......@@ -9,6 +9,7 @@ use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\MapField;
use Google\Protobuf\Internal\GPBType;
use Foo\TestEnum;
use Foo\TestIncludePrefixMessage;
use Foo\TestMessage;
use Foo\TestMessage_Sub;
......@@ -838,4 +839,17 @@ class GeneratedClassTest extends TestBase
{
$m = new NoNameSpaceEnum();
}
#########################################################
# Test message with given prefix.
#########################################################
public function testPrefixMessage()
{
$m = new TestIncludePrefixMessage();
$n = new PrefixTestPrefix();
$n->setA(1);
$m->setPrefixMessage($n);
$this->assertSame(1, $m->getPrefixMessage()->getA());
}
}
......@@ -2,8 +2,10 @@
# phpunit has memory leak by itself. Thus, it cannot be used to test memory leak.
require_once('generated/PrefixTestPrefix.php');
require_once('generated/Bar/TestInclude.php');
require_once('generated/Foo/TestEnum.php');
require_once('generated/Foo/TestIncludePrefixMessage.php');
require_once('generated/Foo/TestMessage.php');
require_once('generated/Foo/TestMessage_Sub.php');
require_once('generated/Foo/TestPackedMessage.php');
......@@ -11,6 +13,7 @@ require_once('generated/Foo/TestPhpDoc.php');
require_once('generated/Foo/TestUnpackedMessage.php');
require_once('generated/GPBMetadata/Proto/Test.php');
require_once('generated/GPBMetadata/Proto/TestInclude.php');
require_once('generated/GPBMetadata/Proto/TestPrefix.php');
require_once('test_util.php');
use Google\Protobuf\Internal\RepeatedField;
......
syntax = "proto3";
import 'proto/test_include.proto';
import 'proto/test_prefix.proto';
package foo;
......@@ -142,3 +143,7 @@ message TestUnpackedMessage {
message TestPhpDoc {
int32 a = 1;
}
message TestIncludePrefixMessage {
TestPrefix prefix_message = 1;
}
syntax = "proto3";
option php_class_prefix = "Prefix";
message TestPrefix {
int32 a = 1;
}
......@@ -90,7 +90,7 @@ std::string MessagePrefix(const Descriptor* message) {
message->file()->package() == "google.protobuf") {
return "GPB";
} else {
return "";
return (message->file()->options()).php_class_prefix();
}
}
......@@ -103,8 +103,12 @@ std::string MessageName(const Descriptor* message, bool is_descriptor) {
}
message_name = MessagePrefix(message) + message_name;
if (message->file()->package() == "") {
return message_name;
} else {
return PhpName(message->file()->package(), is_descriptor) + '\\' +
message_name;
}
}
std::string MessageFullName(const Descriptor* message, bool is_descriptor) {
......
This diff is collapsed.
This diff is collapsed.
......@@ -376,6 +376,10 @@ message FileOptions {
// to prefix the types/symbols defined.
optional string swift_prefix = 39;
// Sets the php class prefix which is prepended to all php generated classes
// from this .proto. Default is empty.
optional string php_class_prefix = 40;
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
......
......@@ -358,7 +358,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
../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto
pushd ../../src
./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
popd
......
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