From: Etienne Kneuss Date: Wed, 9 Jul 2008 21:27:28 +0000 (+0000) Subject: MFH: Move SXI::count to SXE::count and make it user-friendly X-Git-Tag: php-5.3.0alpha1~414 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=003841e312c00966846c636a3d9b2f41915ae297;p=php MFH: Move SXI::count to SXE::count and make it user-friendly --- diff --git a/ext/simplexml/php_simplexml.h b/ext/simplexml/php_simplexml.h index 095074c972..309f0186c5 100644 --- a/ext/simplexml/php_simplexml.h +++ b/ext/simplexml/php_simplexml.h @@ -68,6 +68,7 @@ typedef struct { zval *data; } iter; zval *tmp; + zend_function *fptr_count; } php_sxe_object; #ifdef ZTS diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index 4e330c9bd7..91d2a7ad90 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -1811,14 +1811,12 @@ SXE_METHOD(__toString) } /* }}} */ -static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */ +static int php_sxe_count_elements_helper(php_sxe_object *sxe, long *count TSRMLS_DC) /* {{{ */ { - php_sxe_object *sxe; xmlNodePtr node; zval *data; *count = 0; - sxe = php_sxe_fetch_object(object TSRMLS_CC); data = sxe->iter.data; sxe->iter.data = NULL; @@ -1840,6 +1838,42 @@ static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */ } /* }}} */ +static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */ +{ + php_sxe_object *intern; + intern = php_sxe_fetch_object(object TSRMLS_CC); + if (intern->fptr_count) { + zval *rv; + zend_call_method_with_0_params(&object, intern->zo.ce, &intern->fptr_count, "count", &rv); + if (rv) { + if (intern->tmp) { + zval_ptr_dtor(&intern->tmp); + } + MAKE_STD_ZVAL(intern->tmp); + ZVAL_ZVAL(intern->tmp, rv, 1, 1); + convert_to_long(intern->tmp); + *count = (long) Z_LVAL_P(intern->tmp); + return SUCCESS; + } + return FAILURE; + } + return php_sxe_count_elements_helper(intern, count TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto int SimpleXMLIterator::count() + Get number of child elements */ +SXE_METHOD(count) +{ + long count = 0; + php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + + php_sxe_count_elements_helper(sxe, &count TSRMLS_CC); + + RETURN_LONG(count); +} +/* }}} */ + static zval *sxe_get_value(zval *z TSRMLS_DC) /* {{{ */ { zval *retval; @@ -1990,12 +2024,15 @@ static void sxe_object_free_storage(void *object TSRMLS_DC) static php_sxe_object* php_sxe_object_new(zend_class_entry *ce TSRMLS_DC) { php_sxe_object *intern; + zend_class_entry *parent = ce; + int inherited = 0; intern = ecalloc(1, sizeof(php_sxe_object)); intern->iter.type = SXE_ITER_NONE; intern->iter.nsprefix = NULL; intern->iter.name = NULL; + intern->fptr_count = NULL; #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5) zend_object_std_init(&intern->zo, ce TSRMLS_CC); @@ -2007,6 +2044,22 @@ static php_sxe_object* php_sxe_object_new(zend_class_entry *ce TSRMLS_DC) intern->zo.guards = NULL; #endif + while (parent) { + if (parent == sxe_class_entry) { + break; + } + + parent = parent->parent; + inherited = 1; + } + + if (inherited) { + zend_hash_find(&ce->function_table, "count", sizeof("count"),(void **) &intern->fptr_count); + if (intern->fptr_count->common.scope == parent) { + intern->fptr_count = NULL; + } + } + return intern; } /* }}} */ @@ -2456,6 +2509,7 @@ static const zend_function_entry sxe_functions[] = { /* {{{ */ SXE_ME(addChild, NULL, ZEND_ACC_PUBLIC) SXE_ME(addAttribute, NULL, ZEND_ACC_PUBLIC) SXE_ME(__toString, NULL, ZEND_ACC_PUBLIC) + SXE_ME(count, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/ext/simplexml/tests/036.phpt b/ext/simplexml/tests/036.phpt new file mode 100644 index 0000000000..24bfe60e7d --- /dev/null +++ b/ext/simplexml/tests/036.phpt @@ -0,0 +1,22 @@ +--TEST-- +SimpleXML: overriden count() method +--SKIPIF-- + +--FILE-- +asdfghjk'; +$sxe = new SXE($str); +var_dump(count($sxe)); +?> +==Done== +--EXPECT-- +Called Count! +int(2) +==Done== diff --git a/ext/spl/spl_sxe.c b/ext/spl/spl_sxe.c index 70ca8f2061..5670ab6138 100755 --- a/ext/spl/spl_sxe.c +++ b/ext/spl/spl_sxe.c @@ -145,17 +145,6 @@ SPL_METHOD(SimpleXMLIterator, getChildren) RETURN_ZVAL(sxe->iter.data, 1, 0); } -/* {{{ proto int SimpleXMLIterator::count() - Get number of child elements */ -SPL_METHOD(SimpleXMLIterator, count) -{ - long count = 0; - - Z_OBJ_HANDLER_P(getThis(), count_elements)(getThis(), &count TSRMLS_CC); - - RETURN_LONG(count); -} - static const zend_function_entry spl_funcs_SimpleXMLIterator[] = { SPL_ME(SimpleXMLIterator, rewind, NULL, ZEND_ACC_PUBLIC) SPL_ME(SimpleXMLIterator, valid, NULL, ZEND_ACC_PUBLIC) @@ -164,7 +153,6 @@ static const zend_function_entry spl_funcs_SimpleXMLIterator[] = { SPL_ME(SimpleXMLIterator, next, NULL, ZEND_ACC_PUBLIC) SPL_ME(SimpleXMLIterator, hasChildren, NULL, ZEND_ACC_PUBLIC) SPL_ME(SimpleXMLIterator, getChildren, NULL, ZEND_ACC_PUBLIC) - SPL_ME(SimpleXMLIterator, count, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */