ENHANCEMENT: Improved performance of Object instantation.

API CHANGE: Extensions can no longer be extended with more Extensions (that would be a really bad idea, now it doesn't work at all, for performance)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@83439 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-07-31 05:40:31 +00:00
parent 3b7595e8c7
commit 93d45d9b4d

View File

@ -261,20 +261,26 @@ abstract class Object {
* @return mixed
*/
public static function uninherited_static($class, $name) {
$inherited = self::get_static($class, $name);
$parent = null;
if(isset(self::$cache_is_inherited[$class][$name])) {
return self::$cache_is_inherited[$class][$name] ? null : self::get_static($class, $name);
if($parentClass = get_parent_class($class)) {
$parent = self::get_static($parentClass, $name);
} else {
$inherited = self::get_static($class, $name);
$parent = null;
if($parentClass = get_parent_class($class)) {
$parent = self::get_static($parentClass, $name);
}
self::$cache_is_inherited[$class][$name] = ($inherited == $parent);
return ($inherited == $parent) ? null : $inherited;
}
if(is_array($inherited) && is_array($parent)) {
return array_diff_assoc($inherited, $parent);
}
return ($inherited != $parent) ? $inherited : null;
}
/**
* Cache the results of whether or not a given var is inherited.
*/
private static $cache_is_inherited = array();
/**
* Traverse down a class ancestry and attempt to merge all the uninherited static values for a particular static
* into a single variable
@ -484,11 +490,16 @@ abstract class Object {
public function __construct() {
$this->class = get_class($this);
// Don't do magic methods and/or extensions on extensions themselves.
if($this instanceof Extension) return;
if($extensionClasses = ClassInfo::ancestry($this->class)) foreach($extensionClasses as $class) {
if($extensions = self::uninherited_static($class, 'extensions')) foreach($extensions as $extension) {
// an $extension value can contain parameters as a string,
// e.g. "Versioned('Stage','Live')"
$instance = eval("return new $extension;");
if(strpos($extension,'(') === false) $instance = new $extension();
else $instance = eval("return new $extension;");
$instance->setOwner(null, $class);
$this->extension_instances[$instance->class] = $instance;
}