From 55a7f6649102af3081eca66a9f2a7d47b351503a Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 4 Feb 2013 12:04:12 +0100 Subject: [PATCH] add parent recursion support; package.xml --- config.m4 | 1 + package.xml | 62 +++++++++++++++++++++++++ php_propro.c | 125 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 150 insertions(+), 38 deletions(-) create mode 100644 package.xml diff --git a/config.m4 b/config.m4 index f419731..925eebb 100644 --- a/config.m4 +++ b/config.m4 @@ -2,5 +2,6 @@ PHP_ARG_ENABLE(propro, whether to enable property proxy support, [ --enable-propro Enable property proxy support]) if test "$PHP_PROPRO" != "no"; then + PHP_INSTALL_HEADERS(ext/propro, php_propro.h) PHP_NEW_EXTENSION(propro, php_propro.c, $ext_shared) fi diff --git a/package.xml b/package.xml new file mode 100644 index 0000000..94b7ba2 --- /dev/null +++ b/package.xml @@ -0,0 +1,62 @@ + + + propro + pecl.php.net + Property proxy + A reusable split-off of pecl_http's property proxy API. + + Michael Wallner + mike + mike@php.net + yes + + 2013-02-03 + + 1.0.0 + 1.0.0 + + + stable + stable + + BSD, revised + + + + + + + + + + + + + + + + + 5.4 + + + 1.4.0 + + + + propro + + + + + diff --git a/php_propro.c b/php_propro.c index 07272b2..1b58fc7 100644 --- a/php_propro.c +++ b/php_propro.c @@ -91,18 +91,48 @@ PHP_PROPRO_API zend_object_value php_property_proxy_object_new_ex(zend_class_ent return o->zv; } +static zval *get_parent_proxied_value(zval *object TSRMLS_DC); static zval *get_proxied_value(zval *object TSRMLS_DC); static zval *read_dimension(zval *object, zval *offset, int type TSRMLS_DC); static STATUS cast_proxied_value(zval *object, zval *return_value, int type TSRMLS_DC); static void write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC); static void set_proxied_value(zval **object, zval *value TSRMLS_DC); +static zval *get_parent_proxied_value(zval *object TSRMLS_DC) +{ + zval *value = NULL; + php_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + + if (obj->proxy) { + if (obj->parent) { + zval *parent; + + MAKE_STD_ZVAL(parent); + parent->type = IS_OBJECT; + parent->value.obj = obj->parent->zv; + zend_objects_store_add_ref_by_handle(obj->parent->zv.handle TSRMLS_CC); + value = get_proxied_value(parent TSRMLS_CC); + zval_ptr_dtor(&parent); + } + } + + return value; +} + static zval *get_proxied_value(zval *object TSRMLS_DC) { zval **hash_value, *value = NULL; php_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); if (obj->proxy) { + if (obj->parent) { + zval *parent_value = get_parent_proxied_value(object TSRMLS_CC); + + Z_ADDREF_P(parent_value); + zval_ptr_dtor(&obj->proxy->container); + obj->proxy->container = parent_value; + } + switch (Z_TYPE_P(obj->proxy->container)) { case IS_OBJECT: value = zend_read_property(Z_OBJCE_P(obj->proxy->container), obj->proxy->container, obj->proxy->member_str, obj->proxy->member_len, 0 TSRMLS_CC); @@ -139,6 +169,14 @@ static void set_proxied_value(zval **object, zval *value TSRMLS_DC) php_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC); if (obj->proxy) { + if (obj->parent) { + zval *parent_value = get_parent_proxied_value(*object TSRMLS_CC); + + Z_ADDREF_P(parent_value); + zval_ptr_dtor(&obj->proxy->container); + obj->proxy->container = parent_value; + } + switch (Z_TYPE_P(obj->proxy->container)) { case IS_OBJECT: zend_update_property(Z_OBJCE_P(obj->proxy->container), obj->proxy->container, obj->proxy->member_str, obj->proxy->member_len, value TSRMLS_CC); @@ -155,7 +193,9 @@ static void set_proxied_value(zval **object, zval *value TSRMLS_DC) MAKE_STD_ZVAL(zparent); zparent->type = IS_OBJECT; zparent->value.obj = obj->parent->zv; + zend_objects_store_add_ref_by_handle(obj->parent->zv.handle TSRMLS_CC); set_proxied_value(&zparent, obj->proxy->container TSRMLS_CC); + zval_ptr_dtor(&zparent); } } } @@ -163,13 +203,12 @@ static void set_proxied_value(zval **object, zval *value TSRMLS_DC) static zval *read_dimension(zval *object, zval *offset, int type TSRMLS_DC) { zval *value = NULL; - zval *proxied_value; + zval *proxied_value = get_proxied_value(object TSRMLS_CC); + zval *o = offset; - if ((proxied_value = get_proxied_value(object TSRMLS_CC))) { - zval *o = offset; - - convert_to_string_ex(&o); + convert_to_string_ex(&o); + if (BP_VAR_R == type && proxied_value) { if (Z_TYPE_P(proxied_value) == IS_ARRAY) { zval **hash_value; if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(proxied_value), Z_STRVAL_P(o), Z_STRLEN_P(o), (void *) &hash_value)) { @@ -177,25 +216,32 @@ static zval *read_dimension(zval *object, zval *offset, int type TSRMLS_DC) value = *hash_value; } } + } else { + php_property_proxy_t *proxy; + php_property_proxy_object_t *proxy_obj; - if (!value) { - zval **hash_value; - - SEPARATE_ZVAL(&proxied_value); + if (proxied_value) { convert_to_array(proxied_value); - - MAKE_STD_ZVAL(value); - ZVAL_NULL(value); - zend_symtable_update(Z_ARRVAL_P(proxied_value), Z_STRVAL_P(o), Z_STRLEN_P(o) + 1, (void *) &value, sizeof(zval *), (void *) &hash_value); - value = *hash_value; - Z_SET_ISREF_P(value); - + Z_ADDREF_P(proxied_value); + } else { + MAKE_STD_ZVAL(proxied_value); + array_init(proxied_value); set_proxied_value(&object, proxied_value TSRMLS_CC); } - if (o != offset) { - zval_ptr_dtor(&o); - } + add_assoc_null_ex(proxied_value, Z_STRVAL_P(o), Z_STRLEN_P(o) + 1); + + proxy = php_property_proxy_init(proxied_value, Z_STRVAL_P(o), Z_STRLEN_P(o) TSRMLS_CC); + zval_ptr_dtor(&proxied_value); + MAKE_STD_ZVAL(value); + Z_SET_REFCOUNT_P(value, 0); + value->type = IS_OBJECT; + value->value.obj = php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy, &proxy_obj TSRMLS_CC); + proxy_obj->parent = zend_object_store_get_object(object TSRMLS_CC); + zend_objects_store_add_ref_by_handle(proxy_obj->parent->zv.handle TSRMLS_CC); + } + if (o && o != offset) { + zval_ptr_dtor(&o); } return value; @@ -203,32 +249,35 @@ static zval *read_dimension(zval *object, zval *offset, int type TSRMLS_DC) static void write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) { - zval *proxied_value; + zval *proxied_value, *o = offset; + php_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); if ((proxied_value = get_proxied_value(object TSRMLS_CC))) { - zval *o = offset; - - SEPARATE_ZVAL(&proxied_value); convert_to_array(proxied_value); + Z_ADDREF_P(proxied_value); + } else { + MAKE_STD_ZVAL(proxied_value); + array_init(proxied_value); + } - if (Z_REFCOUNT_P(value) > 1) { - SEPARATE_ZVAL(&value); - } - Z_ADDREF_P(value); - - if (o) { - convert_to_string_ex(&o); - zend_symtable_update(Z_ARRVAL_P(proxied_value), Z_STRVAL_P(o), Z_STRLEN_P(o) + 1, (void *) &value, sizeof(zval *), NULL); - } else { - zend_hash_next_index_insert(Z_ARRVAL_P(proxied_value), (void *) &value, sizeof(zval *), NULL); - } + if (Z_REFCOUNT_P(value) > 1) { + SEPARATE_ZVAL(&value); + } + Z_ADDREF_P(value); - if (o != offset) { - zval_ptr_dtor(&o); - } + if (o) { + convert_to_string_ex(&o); + zend_symtable_update(Z_ARRVAL_P(proxied_value), Z_STRVAL_P(o), Z_STRLEN_P(o) + 1, (void *) &value, sizeof(zval *), NULL); + } else { + zend_hash_next_index_insert(Z_ARRVAL_P(proxied_value), (void *) &value, sizeof(zval *), NULL); + } - set_proxied_value(&object, proxied_value TSRMLS_CC); + if (o && o != offset) { + zval_ptr_dtor(&o); } + + set_proxied_value(&object, proxied_value TSRMLS_CC); + zval_ptr_dtor(&proxied_value); } ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2) -- 2.30.2