diff options
author | Paul Yang <TeBoring@users.noreply.github.com> | 2016-12-08 11:16:49 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-08 11:16:49 -0800 |
commit | 46ae90dc5e145b12fffa7e053a908a9f3e066286 (patch) | |
tree | e15e50f9a986bd64cd4413b135382a305701437f /php/ext | |
parent | 631f4618a7b84cdc91949229a11a8abe5dfc4a34 (diff) | |
download | protobuf-46ae90dc5e145b12fffa7e053a908a9f3e066286.tar.gz protobuf-46ae90dc5e145b12fffa7e053a908a9f3e066286.tar.bz2 protobuf-46ae90dc5e145b12fffa7e053a908a9f3e066286.zip |
Make php generated code conform to PSR-4. (#2435)
1. Generate single file for each message.
2. Lazily initiate metadata.
Diffstat (limited to 'php/ext')
-rw-r--r-- | php/ext/google/protobuf/message.c | 12 | ||||
-rw-r--r-- | php/ext/google/protobuf/protobuf.h | 5 | ||||
-rw-r--r-- | php/ext/google/protobuf/storage.c | 8 |
3 files changed, 19 insertions, 6 deletions
diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index d8fbbe11..16e397f5 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -41,6 +41,7 @@ static zend_function_entry message_methods[] = { PHP_ME(Message, decode, NULL, ZEND_ACC_PUBLIC) PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED) PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED) + PHP_ME(Message, __construct, NULL, ZEND_ACC_PROTECTED) {NULL, NULL, NULL} }; @@ -210,6 +211,17 @@ void build_class_from_descriptor(zval* php_descriptor TSRMLS_DC) { // PHP Methods // ----------------------------------------------------------------------------- +// At the first time the message is created, the class entry hasn't been +// modified. As a result, the first created instance will be a normal zend +// object. Here, we manually modify it to our message in such a case. +PHP_METHOD(Message, __construct) { + if (Z_OBJVAL_P(getThis()).handlers != message_handlers) { + zend_class_entry* ce = Z_OBJCE_P(getThis()); + zval_dtor(getThis()); + Z_OBJVAL_P(getThis()) = message_create(ce TSRMLS_CC); + } +} + PHP_METHOD(Message, readOneof) { long index; diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index fb5879dc..8022a9aa 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -231,8 +231,8 @@ struct MessageHeader { }; MessageLayout* create_layout(const upb_msgdef* msgdef); -void layout_init(MessageLayout* layout, void* storage, zval** properties_table - TSRMLS_DC); +void layout_init(MessageLayout* layout, void* storage, + zval** properties_table TSRMLS_DC); zval* layout_get(MessageLayout* layout, const void* storage, const upb_fielddef* field, zval** cache TSRMLS_DC); void layout_set(MessageLayout* layout, MessageHeader* header, @@ -241,6 +241,7 @@ void free_layout(MessageLayout* layout); PHP_METHOD(Message, readOneof); PHP_METHOD(Message, writeOneof); +PHP_METHOD(Message, __construct); // ----------------------------------------------------------------------------- // Encode / Decode. diff --git a/php/ext/google/protobuf/storage.c b/php/ext/google/protobuf/storage.c index 1d25a91b..8a2b3a22 100644 --- a/php/ext/google/protobuf/storage.c +++ b/php/ext/google/protobuf/storage.c @@ -458,8 +458,8 @@ void free_layout(MessageLayout* layout) { FREE(layout); } -void layout_init(MessageLayout* layout, void* storage, zval** properties_table - TSRMLS_DC) { +void layout_init(MessageLayout* layout, void* storage, + zval** properties_table TSRMLS_DC) { int i; upb_msg_field_iter it; for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it); @@ -479,8 +479,8 @@ void layout_init(MessageLayout* layout, void* storage, zval** properties_table DEREF(memory, zval**) = property_ptr; } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { zval_ptr_dtor(property_ptr); - repeated_field_create_with_type(repeated_field_type, field, property_ptr - TSRMLS_CC); + repeated_field_create_with_type(repeated_field_type, field, + property_ptr TSRMLS_CC); DEREF(memory, zval**) = property_ptr; } else { native_slot_init(upb_fielddef_type(field), memory, property_ptr); |