dmitry Wed Jun 25 12:34:14 2008 UTC Modified files: /php-src/ext/reflection php_reflection.c /php-src/ext/reflection/tests ReflectionFunction_getNamespaceName.phpt reflectionClass_getNamespaceName.phpt Log: Added support for namespaces
http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/php_reflection.c?r1=1.296&r2=1.297&diff_format=u Index: php-src/ext/reflection/php_reflection.c diff -u php-src/ext/reflection/php_reflection.c:1.296 php-src/ext/reflection/php_reflection.c:1.297 --- php-src/ext/reflection/php_reflection.c:1.296 Sun Jun 1 03:13:54 2008 +++ php-src/ext/reflection/php_reflection.c Wed Jun 25 12:34:14 2008 @@ -20,7 +20,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_reflection.c,v 1.296 2008/06/01 03:13:54 felipe Exp $ */ +/* $Id: php_reflection.c,v 1.297 2008/06/25 12:34:14 dmitry Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -2596,6 +2596,78 @@ } /* }}} */ +/* {{{ proto public bool ReflectionFunction::inNamespace() + Returns whether this function is defined in namespace */ +ZEND_METHOD(reflection_function, inNamespace) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_TRUE; + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_TRUE; + } + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto public string ReflectionFunction::getNamespaceName() + Returns the name of namespace where this function is defined */ +ZEND_METHOD(reflection_function, getNamespaceName) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_STRINGL(Z_STRVAL_PP(name), colon.s - Z_STRVAL_PP(name) - 1, 1); + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_UNICODEL(Z_USTRVAL_PP(name), colon.u - Z_USTRVAL_PP(name) - 1, 1); + } + RETURN_EMPTY_TEXT(); +} +/* }}} */ + +/* {{{ proto public string ReflectionFunction::getShortName() + Returns the short name of the function (without namespace part) */ +ZEND_METHOD(reflection_function, getShortName) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_STRINGL(colon.s + 1, Z_STRLEN_PP(name) - (colon.s - Z_STRVAL_PP(name) + 1), 1); + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_UNICODEL(colon.u + 1, Z_USTRLEN_PP(name) - (colon.u - Z_USTRVAL_PP(name) + 1), 1); + } + RETURN_ZVAL(*name, 1, 0); +} +/* }}} */ + /* {{{ proto public bool ReflectionMethod::isConstructor() U Returns whether this method is the constructor */ ZEND_METHOD(reflection_method, isConstructor) @@ -3842,6 +3914,78 @@ } /* }}} */ +/* {{{ proto public bool ReflectionClass::inNamespace() + Returns whether this class is defined in namespace */ +ZEND_METHOD(reflection_class, inNamespace) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_TRUE; + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_TRUE; + } + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto public string ReflectionClass::getNamespaceName() + Returns the name of namespace where this class is defined */ +ZEND_METHOD(reflection_class, getNamespaceName) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_STRINGL(Z_STRVAL_PP(name), colon.s - Z_STRVAL_PP(name) - 1, 1); + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_UNICODEL(Z_USTRVAL_PP(name), colon.u - Z_USTRVAL_PP(name) - 1, 1); + } + RETURN_EMPTY_TEXT(); +} +/* }}} */ + +/* {{{ proto public string ReflectionClass::getShortName() + Returns the short name of the class (without namespace part) */ +ZEND_METHOD(reflection_class, getShortName) +{ + zval **name; + zstr colon; + + METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0); + if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) { + RETURN_FALSE; + } + if (Z_TYPE_PP(name) == IS_STRING && + (colon.s = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) && + colon.s > Z_STRVAL_PP(name) && *(colon.s-1) == ':') { + RETURN_STRINGL(colon.s + 1, Z_STRLEN_PP(name) - (colon.s - Z_STRVAL_PP(name) + 1), 1); + } else if (Z_TYPE_PP(name) == IS_UNICODE && + (colon.u = u_memrchr(Z_USTRVAL_PP(name), ':', Z_USTRLEN_PP(name))) && + colon.u > Z_USTRVAL_PP(name) && *(colon.u-1) == ':') { + RETURN_UNICODEL(colon.u + 1, Z_USTRLEN_PP(name) - (colon.u - Z_USTRVAL_PP(name) + 1), 1); + } + RETURN_ZVAL(*name, 1, 0); +} +/* }}} */ + /* {{{ proto public static mixed ReflectionObject::export(mixed argument [, bool return]) throws ReflectionException U Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */ ZEND_METHOD(reflection_object, export) @@ -4626,6 +4770,9 @@ ZEND_ME(reflection_function, getExtension, NULL, 0) ZEND_ME(reflection_function, getExtensionName, NULL, 0) ZEND_ME(reflection_function, isDeprecated, NULL, 0) + ZEND_ME(reflection_function, inNamespace, NULL, 0) + ZEND_ME(reflection_function, getNamespaceName, NULL, 0) + ZEND_ME(reflection_function, getShortName, NULL, 0) {NULL, NULL, NULL} }; @@ -4815,6 +4962,9 @@ ZEND_ME(reflection_class, implementsInterface, arginfo_reflection_class_implementsInterface, 0) ZEND_ME(reflection_class, getExtension, NULL, 0) ZEND_ME(reflection_class, getExtensionName, NULL, 0) + ZEND_ME(reflection_class, inNamespace, NULL, 0) + ZEND_ME(reflection_class, getNamespaceName, NULL, 0) + ZEND_ME(reflection_class, getShortName, NULL, 0) {NULL, NULL, NULL} }; @@ -5060,7 +5210,7 @@ php_info_print_table_start(); php_info_print_table_header(2, "Reflection", "enabled"); - php_info_print_table_row(2, "Version", "$Id: php_reflection.c,v 1.296 2008/06/01 03:13:54 felipe Exp $"); + php_info_print_table_row(2, "Version", "$Id: php_reflection.c,v 1.297 2008/06/25 12:34:14 dmitry Exp $"); php_info_print_table_end(); } /* }}} */ http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt diff -u /dev/null php-src/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt:1.2 --- /dev/null Wed Jun 25 12:34:14 2008 +++ php-src/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt Wed Jun 25 12:34:14 2008 @@ -0,0 +1,29 @@ +--TEST-- +ReflectionFunction::getNamespaceName() +--FILE-- +<?php +namespace A::B; +function foo() {} + +$function = new ReflectionFunction('sort'); +var_dump($function->inNamespace()); +var_dump($function->getName()); +var_dump($function->getNamespaceName()); +var_dump($function->getShortName()); + +$function = new ReflectionFunction('A::B::foo'); +var_dump($function->inNamespace()); +var_dump($function->getName()); +var_dump($function->getNamespaceName()); +var_dump($function->getShortName()); +?> +--EXPECT-- +bool(false) +unicode(4) "sort" +unicode(0) "" +unicode(4) "sort" +bool(true) +unicode(9) "A::B::foo" +unicode(4) "A::B" +unicode(3) "foo" + http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/tests/reflectionClass_getNamespaceName.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/reflection/tests/reflectionClass_getNamespaceName.phpt diff -u /dev/null php-src/ext/reflection/tests/reflectionClass_getNamespaceName.phpt:1.2 --- /dev/null Wed Jun 25 12:34:14 2008 +++ php-src/ext/reflection/tests/reflectionClass_getNamespaceName.phpt Wed Jun 25 12:34:14 2008 @@ -0,0 +1,30 @@ +--TEST-- +ReflectionClass::getNamespaceName() +--FILE-- +<?php +namespace A::B; +class Foo { +} + +$function = new ReflectionClass('stdClass'); +var_dump($function->inNamespace()); +var_dump($function->getName()); +var_dump($function->getNamespaceName()); +var_dump($function->getShortName()); + +$function = new ReflectionClass('A::B::Foo'); +var_dump($function->inNamespace()); +var_dump($function->getName()); +var_dump($function->getNamespaceName()); +var_dump($function->getShortName()); +?> +--EXPECT-- +bool(false) +unicode(8) "stdClass" +unicode(0) "" +unicode(8) "stdClass" +bool(true) +unicode(9) "A::B::Foo" +unicode(4) "A::B" +unicode(3) "Foo" +
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php