johannes                                 Sun, 30 May 2010 01:00:45 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=299946

Log:
- More trait reflection work
#- I'm not happy about ReflectionClass::getTraitAliases, yet

Changed paths:
    U   php/php-src/trunk/ext/reflection/php_reflection.c
    U   php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt
    A   php/php-src/trunk/ext/reflection/tests/traits004.phpt
    A   php/php-src/trunk/ext/reflection/tests/traits005.phpt

Modified: php/php-src/trunk/ext/reflection/php_reflection.c
===================================================================
--- php/php-src/trunk/ext/reflection/php_reflection.c   2010-05-30 00:51:55 UTC 
(rev 299945)
+++ php/php-src/trunk/ext/reflection/php_reflection.c   2010-05-30 01:00:45 UTC 
(rev 299946)
@@ -4254,6 +4254,80 @@
 }
 /* }}} */

+/* {{{ 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 @@
        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)

Modified: 
php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt
===================================================================
--- php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt    
2010-05-30 00:51:55 UTC (rev 299945)
+++ php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt    
2010-05-30 01:00:45 UTC (rev 299946)
@@ -34,7 +34,7 @@
     Property [ <default> public $name ]
   }

-  - Methods [44] {
+  - Methods [47] {
     Method [ <internal:Reflection> final private method __clone ] {

       - Parameters [0] {
@@ -188,6 +188,24 @@
       }
     }

+    Method [ <internal:Reflection> public method getTraits ] {
+
+      - Parameters [0] {
+      }
+    }
+
+    Method [ <internal:Reflection> public method getTraitNames ] {
+
+      - Parameters [0] {
+      }
+    }
+
+    Method [ <internal:Reflection> public method getTraitAliases ] {
+
+      - Parameters [0] {
+      }
+    }
+
     Method [ <internal:Reflection> public method isTrait ] {

       - Parameters [0] {

Added: php/php-src/trunk/ext/reflection/tests/traits004.phpt
===================================================================
--- php/php-src/trunk/ext/reflection/tests/traits004.phpt                       
        (rev 0)
+++ php/php-src/trunk/ext/reflection/tests/traits004.phpt       2010-05-30 
01:00:45 UTC (rev 299946)
@@ -0,0 +1,58 @@
+--TEST--
+ReflectionClass::getTraits() and ReflectionClass::getTraitNames
+--FILE--
+<?php
+trait T1 { }
+trait T2 { }
+
+class C1 { }
+class C2 { use T1; }
+class C3 { use T1; use T2; }
+
+for ($c = "C1"; $c <= "C3"; $c++) {
+    echo "class $c:\n";
+    $r = new ReflectionClass($c);
+    var_dump($r->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"
+  }
+}
+


Property changes on: php/php-src/trunk/ext/reflection/tests/traits004.phpt
___________________________________________________________________
Added: svn:keywords
   + Id Rev Revision
Added: svn:eol-style
   + native

Added: php/php-src/trunk/ext/reflection/tests/traits005.phpt
===================================================================
--- php/php-src/trunk/ext/reflection/tests/traits005.phpt                       
        (rev 0)
+++ php/php-src/trunk/ext/reflection/tests/traits005.phpt       2010-05-30 
01:00:45 UTC (rev 299946)
@@ -0,0 +1,41 @@
+--TEST--
+ReflectionClass::getTraitAlias
+--FILE--
+<?php
+trait T1 { function m1() { } function m2() { } }
+
+class C1 { }
+class C2 { use T1; }
+class C3 { use T1 { m1 as a1; } }
+class C4 { use T1 { m1 as a1; m2 as a2; } }
+
+for ($c = "C1"; $c <= "C4"; $c++) {
+    echo "class $c:\n";
+    $r = new ReflectionClass($c);
+    var_dump($r->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"
+}
+


Property changes on: php/php-src/trunk/ext/reflection/tests/traits005.phpt
___________________________________________________________________
Added: svn:keywords
   + Id Rev Revision
Added: svn:eol-style
   + native

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to