mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Improved ClassInfo::ancestry() performance through in-memory caching and removal of unnecessary is_object() check - get_class() will complain if its not passed an object already)
This commit is contained in:
parent
72694d8349
commit
4056b94f75
@ -27,6 +27,11 @@ class ClassInfo {
|
||||
*/
|
||||
private static $_cache_all_tables = null;
|
||||
|
||||
/**
|
||||
* @var Array Cache for {@link ancestry()}.
|
||||
*/
|
||||
private static $_cache_ancestry = array();
|
||||
|
||||
/**
|
||||
* @todo Move this to SS_Database or DB
|
||||
*/
|
||||
@ -46,6 +51,7 @@ class ClassInfo {
|
||||
|
||||
static function reset_db_cache() {
|
||||
self::$_cache_all_tables = null;
|
||||
self::$_cache_ancestry = array();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,23 +149,21 @@ class ClassInfo {
|
||||
* @return array
|
||||
*/
|
||||
public static function ancestry($class, $tablesOnly = false) {
|
||||
$ancestry = array();
|
||||
if (!is_string($class)) $class = get_class($class);
|
||||
|
||||
if (is_object($class)) {
|
||||
$class = get_class($class);
|
||||
} elseif (!is_string($class)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid class value %s, must be an object or string', var_export($class, true)
|
||||
));
|
||||
$cacheKey = $class . '_' . (string)$tablesOnly;
|
||||
$parent = $class;
|
||||
if(!isset(self::$_cache_ancestry[$cacheKey])) {
|
||||
$ancestry = array();
|
||||
do {
|
||||
if (!$tablesOnly || DataObject::has_own_table($parent)) {
|
||||
$ancestry[$parent] = $parent;
|
||||
}
|
||||
} while ($parent = get_parent_class($parent));
|
||||
self::$_cache_ancestry[$cacheKey] = array_reverse($ancestry);
|
||||
}
|
||||
|
||||
do {
|
||||
if (!$tablesOnly || DataObject::has_own_table($class)) {
|
||||
$ancestry[$class] = $class;
|
||||
}
|
||||
} while ($class = get_parent_class($class));
|
||||
|
||||
return array_reverse($ancestry);
|
||||
return self::$_cache_ancestry[$cacheKey];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,9 +70,6 @@ class ClassInfoTest extends SapphireTest {
|
||||
$this->assertEquals(array('ClassInfoTest_BaseClass' => 'ClassInfoTest_BaseClass'), $ancestry,
|
||||
'$tablesOnly option excludes memory-only inheritance classes'
|
||||
);
|
||||
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
ClassInfo::ancestry(42);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user