API CHANGE: Refactored ClassInfo::subclassesFor() to traverse the child tree, rather than needing to store a list of every classes descendants.

API CHANGE: Updated ClassInfo::subclassesFor() so all the array keys are consistently the same as the values.
This commit is contained in:
ajshort 2011-02-21 16:16:52 +11:00
parent f55cc7ec67
commit 803e67b87d
2 changed files with 23 additions and 13 deletions

View File

@ -125,21 +125,31 @@ class ClassInfo {
* @param mixed $class string of the classname or instance of the class * @param mixed $class string of the classname or instance of the class
* @return array Names of all subclasses as an associative array. * @return array Names of all subclasses as an associative array.
*/ */
static function subclassesFor($class){ public static function subclassesFor($class) {
global $_ALL_CLASSES; global $_ALL_CLASSES;
if (is_object($class)) $class = get_class($class);
// get all classes from the manifest
$subclasses = isset($_ALL_CLASSES['children'][$class]) ? $_ALL_CLASSES['children'][$class] : null;
// add the base class to the array if (is_object($class)) {
if(isset($subclasses)) { $class = get_class($class);
array_unshift($subclasses, $class);
} else {
$subclasses[$class] = $class;
} }
return $subclasses; $parents = array($class);
$classes = array($class => $class);
if (!isset($_ALL_CLASSES['children'][$class])) {
return $classes;
}
while ($parent = array_shift($parents)) {
foreach ($_ALL_CLASSES['children'][$parent] as $class) {
$classes[$class] = $class;
if (isset($_ALL_CLASSES['children'][$class])) {
$parents[] = $class;
}
}
}
return $classes;
} }
/** /**

View File

@ -9,7 +9,7 @@ class ClassInfoTest extends SapphireTest {
$this->assertEquals( $this->assertEquals(
ClassInfo::subclassesFor('ClassInfoTest_BaseClass'), ClassInfo::subclassesFor('ClassInfoTest_BaseClass'),
array( array(
0 => 'ClassInfoTest_BaseClass', 'ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass',
'ClassInfoTest_ChildClass' => 'ClassInfoTest_ChildClass', 'ClassInfoTest_ChildClass' => 'ClassInfoTest_ChildClass',
'ClassInfoTest_GrandChildClass' => 'ClassInfoTest_GrandChildClass' 'ClassInfoTest_GrandChildClass' => 'ClassInfoTest_GrandChildClass'
), ),