API CHANGE: Extension no longer inherits from Object.

ENHANCEMENT: Object::__construct() performance improved slightly.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@84160 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-08-11 08:35:14 +00:00
parent 81eeef25e8
commit d8113a667e
3 changed files with 35 additions and 12 deletions

View File

@ -11,7 +11,7 @@
* @package sapphire * @package sapphire
* @subpackage core * @subpackage core
*/ */
abstract class Extension extends Object { abstract class Extension {
/** /**
* This is used by extensions designed to be applied to controllers. * This is used by extensions designed to be applied to controllers.
* It works the same way as {@link Controller::$allowed_actions}. * It works the same way as {@link Controller::$allowed_actions}.
@ -35,6 +35,12 @@ abstract class Extension extends Object {
* been called as many times as setOwner() * been called as many times as setOwner()
*/ */
private $ownerRefs = 0; private $ownerRefs = 0;
public $class;
function __construct() {
$this->class = get_class($this);
}
/** /**
* Set the owner of this decorator. * Set the owner of this decorator.

View File

@ -506,15 +506,22 @@ abstract class Object {
// Don't do magic methods and/or extensions on extensions themselves. // Don't do magic methods and/or extensions on extensions themselves.
if($this instanceof Extension) return; if($this instanceof Extension) return;
// Don't bother checking some classes that should never be extended
static $notExtendable = array('Object', 'ViewableData', 'DataObject', 'RequestHandler');
if($extensionClasses = ClassInfo::ancestry($this->class)) foreach($extensionClasses as $class) { if($extensionClasses = ClassInfo::ancestry($this->class)) foreach($extensionClasses as $class) {
if($extensions = self::uninherited_static($class, 'extensions')) foreach($extensions as $extension) { if(in_array($class, $notExtendable)) continue;
// an $extension value can contain parameters as a string,
// e.g. "Versioned('Stage','Live')" if($extensions = self::uninherited_static($class, 'extensions')) {
if(strpos($extension,'(') === false) $instance = new $extension(); foreach($extensions as $extension) {
else $instance = eval("return new $extension;"); // an $extension value can contain parameters as a string,
// e.g. "Versioned('Stage','Live')"
if(strpos($extension,'(') === false) $instance = new $extension();
else $instance = eval("return new $extension;");
$instance->setOwner(null, $class); $instance->setOwner(null, $class);
$this->extension_instances[$instance->class] = $instance; $this->extension_instances[$instance->class] = $instance;
}
} }
} }
@ -655,13 +662,23 @@ abstract class Object {
} }
if(method_exists($extension, 'allMethodNames')) { if(method_exists($extension, 'allMethodNames')) {
$methods = $extension->allMethodNames(true);
} else {
if(!isset(self::$built_in_methods[$extension->class])) {
self::$built_in_methods[$extension->class] = array_map('strtolower', get_class_methods($extension));
}
$methods = self::$built_in_methods[$extension->class];
}
if($methods) {
$methodInfo = array( $methodInfo = array(
'property' => $property, 'property' => $property,
'index' => $index, 'index' => $index,
'callSetOwnerFirst' => $extension instanceof Extension, 'callSetOwnerFirst' => $extension instanceof Extension,
); );
$newMethods = array_fill_keys($extension->allMethodNames(true), $methodInfo); $newMethods = array_fill_keys($methods, $methodInfo);
if(isset(self::$extra_methods[$this->class])) { if(isset(self::$extra_methods[$this->class])) {
self::$extra_methods[$this->class] = self::$extra_methods[$this->class] =
@ -806,7 +823,7 @@ abstract class Object {
$values = array(); $values = array();
if($this->extension_instances) foreach($this->extension_instances as $instance) { if($this->extension_instances) foreach($this->extension_instances as $instance) {
if($instance->hasMethod($method)) { if(method_exists($instance, $method)) {
$instance->setOwner($this); $instance->setOwner($this);
$value = $instance->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7); $value = $instance->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7);
if($value !== null) $values[] = $value; if($value !== null) $values[] = $value;

View File

@ -66,9 +66,9 @@ abstract class DataObjectDecorator extends Extension {
// Array to be merged // Array to be merged
if(self::$decoratable_statics[$name]) { if(self::$decoratable_statics[$name]) {
$origVal = self::uninherited_static($class, $name); $origVal = Object::uninherited_static($class, $name);
// Can't use add_static_var() here as it would merge the array rather than replacing // Can't use add_static_var() here as it would merge the array rather than replacing
self::set_static($class, $name, array_merge((array)$origVal, $newVal)); Object::set_static($class, $name, array_merge((array)$origVal, $newVal));
// Value to be overwritten // Value to be overwritten
} else { } else {