From 4158f7e1221ef5b69e8ba0aadd547a6ee2baa827 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Schl=C3=BCter?= Date: Sun, 30 May 2010 01:00:45 +0000 Subject: [PATCH] - More trait reflection work #- I'm not happy about ReflectionClass::getTraitAliases, yet --- ext/reflection/php_reflection.c | 77 +++++++++++++++++++ .../tests/ReflectionClass_toString_001.phpt | 20 ++++- ext/reflection/tests/traits004.phpt | 58 ++++++++++++++ ext/reflection/tests/traits005.phpt | 41 ++++++++++ 4 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/traits004.phpt create mode 100644 ext/reflection/tests/traits005.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a590bf541d..a3aaf3eea1 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4254,6 +4254,80 @@ ZEND_METHOD(reflection_class, getInterfaceNames) } /* }}} */ +/* {{{ proto public ReflectionClass[] ReflectionClass::getTraits() + Returns an array of traits used by this class */ +ZEND_METHOD(reflection_class, getTraits) +{ + reflection_object *intern; + zend_class_entry *ce; + zend_uint i; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + GET_REFLECTION_OBJECT_PTR(ce); + + array_init(return_value); + + for (i=0; i < ce->num_traits; i++) { + zval *trait; + ALLOC_ZVAL(trait); + zend_reflection_class_factory(ce->traits[i], trait TSRMLS_CC); + add_assoc_zval_ex(return_value, ce->traits[i]->name, ce->traits[i]->name_length + 1, trait); + } +} +/* }}} */ + +/* {{{ proto public String[] ReflectionClass::getTraitNames() + Returns an array of names of traits used by this class */ +ZEND_METHOD(reflection_class, getTraitNames) +{ + reflection_object *intern; + zend_class_entry *ce; + zend_uint i; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + GET_REFLECTION_OBJECT_PTR(ce); + + array_init(return_value); + + for (i=0; i < ce->num_traits; i++) { + add_next_index_stringl(return_value, ce->traits[i]->name, ce->traits[i]->name_length, 1); + } +} +/* }}} */ + +/* {{{ proto public arra ReflectionClass::getTraitaliases() + Returns an array of trait aliases */ +ZEND_METHOD(reflection_class, getTraitAliases) +{ + reflection_object *intern; + zend_class_entry *ce; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + GET_REFLECTION_OBJECT_PTR(ce); + + array_init(return_value); + + if (ce->trait_aliases) { + zend_uint i = 0; + while (ce->trait_aliases[i]) { + char *method_name; + int method_name_len; + zend_trait_method_reference *cur_ref = ce->trait_aliases[i]->trait_method; + + method_name_len = spprintf(&method_name, 0, "%s::%s", cur_ref->class_name, cur_ref->method_name); + add_assoc_stringl_ex(return_value, ce->trait_aliases[i]->alias, ce->trait_aliases[i]->alias_len + 1, method_name, method_name_len, 0); + i++; + } + } +} +/* }}} */ + /* {{{ proto public ReflectionClass ReflectionClass::getParentClass() Returns the class' parent class, or, if none exists, FALSE */ ZEND_METHOD(reflection_class, getParentClass) @@ -5636,6 +5710,9 @@ static const zend_function_entry reflection_class_functions[] = { ZEND_ME(reflection_class, getInterfaces, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getInterfaceNames, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isInterface, arginfo_reflection__void, 0) + ZEND_ME(reflection_class, getTraits, arginfo_reflection__void, 0) + ZEND_ME(reflection_class, getTraitNames, arginfo_reflection__void, 0) + ZEND_ME(reflection_class, getTraitAliases, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isTrait, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isAbstract, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isFinal, arginfo_reflection__void, 0) diff --git a/ext/reflection/tests/ReflectionClass_toString_001.phpt b/ext/reflection/tests/ReflectionClass_toString_001.phpt index 790ee4e5e3..a593147293 100644 --- a/ext/reflection/tests/ReflectionClass_toString_001.phpt +++ b/ext/reflection/tests/ReflectionClass_toString_001.phpt @@ -34,7 +34,7 @@ Class [ class ReflectionClass implements Reflector ] { Property [ public $name ] } - - Methods [44] { + - Methods [47] { Method [ final private method __clone ] { - Parameters [0] { @@ -188,6 +188,24 @@ Class [ class ReflectionClass implements Reflector ] { } } + Method [ public method getTraits ] { + + - Parameters [0] { + } + } + + Method [ public method getTraitNames ] { + + - Parameters [0] { + } + } + + Method [ public method getTraitAliases ] { + + - Parameters [0] { + } + } + Method [ public method isTrait ] { - Parameters [0] { diff --git a/ext/reflection/tests/traits004.phpt b/ext/reflection/tests/traits004.phpt new file mode 100644 index 0000000000..c9367c1f48 --- /dev/null +++ b/ext/reflection/tests/traits004.phpt @@ -0,0 +1,58 @@ +--TEST-- +ReflectionClass::getTraits() and ReflectionClass::getTraitNames +--FILE-- +getTraitNames()); + var_dump($r->getTraits()); + echo "\n"; +} +--EXPECT-- +class C1: +array(0) { +} +array(0) { +} + +class C2: +array(1) { + [0]=> + string(2) "T1" +} +array(1) { + ["T1"]=> + &object(ReflectionClass)#1 (1) { + ["name"]=> + string(2) "T1" + } +} + +class C3: +array(2) { + [0]=> + string(2) "T1" + [1]=> + string(2) "T2" +} +array(2) { + ["T1"]=> + &object(ReflectionClass)#2 (1) { + ["name"]=> + string(2) "T1" + } + ["T2"]=> + &object(ReflectionClass)#3 (1) { + ["name"]=> + string(2) "T2" + } +} + diff --git a/ext/reflection/tests/traits005.phpt b/ext/reflection/tests/traits005.phpt new file mode 100644 index 0000000000..1496a35ec9 --- /dev/null +++ b/ext/reflection/tests/traits005.phpt @@ -0,0 +1,41 @@ +--TEST-- +ReflectionClass::getTraitAlias +--FILE-- +getTraitAliases()); + echo "\n"; +} +?> +--EXPECT-- +class C1: +array(0) { +} + +class C2: +array(0) { +} + +class C3: +array(1) { + ["a1"]=> + string(10) "(null)::m1" +} + +class C4: +array(2) { + ["a1"]=> + string(10) "(null)::m1" + ["a2"]=> + string(10) "(null)::m2" +} + -- 2.40.0