mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
f55cc7ec67
commit
803e67b87d
@ -125,21 +125,31 @@ class ClassInfo {
|
||||
* @param mixed $class string of the classname or instance of the class
|
||||
* @return array Names of all subclasses as an associative array.
|
||||
*/
|
||||
static function subclassesFor($class){
|
||||
public static function subclassesFor($class) {
|
||||
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(isset($subclasses)) {
|
||||
array_unshift($subclasses, $class);
|
||||
} else {
|
||||
$subclasses[$class] = $class;
|
||||
if (is_object($class)) {
|
||||
$class = get_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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,7 +9,7 @@ class ClassInfoTest extends SapphireTest {
|
||||
$this->assertEquals(
|
||||
ClassInfo::subclassesFor('ClassInfoTest_BaseClass'),
|
||||
array(
|
||||
0 => 'ClassInfoTest_BaseClass',
|
||||
'ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass',
|
||||
'ClassInfoTest_ChildClass' => 'ClassInfoTest_ChildClass',
|
||||
'ClassInfoTest_GrandChildClass' => 'ClassInfoTest_GrandChildClass'
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user