From 46ae90dc5e145b12fffa7e053a908a9f3e066286 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 8 Dec 2016 11:16:49 -0800 Subject: Make php generated code conform to PSR-4. (#2435) 1. Generate single file for each message. 2. Lazily initiate metadata. --- php/ext/google/protobuf/message.c | 12 ++++++++++++ php/ext/google/protobuf/protobuf.h | 5 +++-- php/ext/google/protobuf/storage.c | 8 ++++---- 3 files changed, 19 insertions(+), 6 deletions(-) (limited to 'php/ext/google') 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); -- cgit v1.2.3