mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX #4929: Fixed Object::add_static_vars() for uninherited static.s (from r97586)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102510 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
6994e715ca
commit
69543bb575
@ -280,6 +280,19 @@ abstract class Object {
|
|||||||
if($parentProp == $classProp) $classProp = null;
|
if($parentProp == $classProp) $classProp = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add data from extra_statics if it has been applied to this specific class (it
|
||||||
|
// wouldn't make sense to have them inherit in this method). This is kept separate
|
||||||
|
// from the equivalent get_static code because it's so much simpler
|
||||||
|
if(isset(self::$extra_statics[$class][$name])) {
|
||||||
|
$toMerge = self::$extra_statics[$class][$name];
|
||||||
|
|
||||||
|
if(is_array($toMerge) && is_array($classProp)) {
|
||||||
|
$classProp = array_merge($toMerge, $classProp);
|
||||||
|
} elseif(!$classProp) {
|
||||||
|
$classProp = $toMerge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self::$cached_uninherited_statics[$class][$name] = true;
|
self::$cached_uninherited_statics[$class][$name] = true;
|
||||||
self::$uninherited_statics[$class][$name] = $classProp;
|
self::$uninherited_statics[$class][$name] = $classProp;
|
||||||
}
|
}
|
||||||
@ -355,8 +368,11 @@ abstract class Object {
|
|||||||
if ($replace) {
|
if ($replace) {
|
||||||
self::set_static($class, $name, $value);
|
self::set_static($class, $name, $value);
|
||||||
self::$replaced_statics[$class][$name] = true;
|
self::$replaced_statics[$class][$name] = true;
|
||||||
|
|
||||||
|
// Clear caches
|
||||||
} else {
|
} else {
|
||||||
self::$cached_statics[$class][$name] = null;
|
self::$cached_statics[$class][$name] = null;
|
||||||
|
self::$cached_uninherited_statics[$class][$name] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +70,25 @@ class ObjectStaticTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Confirms that Object::add_static_var() doesn't work for uninherited stats
|
* Checks that Object::add_static_var() also works for uninherited stats
|
||||||
*/
|
*/
|
||||||
public function testAddStaticVarDoesntWorkFor() {
|
public function testAddStaticVarWorksForUninheritedStatics() {
|
||||||
Object::add_static_var('ObjectStaticTest_First', 'first', array('test_1b'));
|
Object::add_static_var('ObjectStaticTest_First', 'first', array('test_1b'));
|
||||||
Object::add_static_var('ObjectStaticTest_Second', 'first', array('test_2b'));
|
Object::add_static_var('ObjectStaticTest_Second', 'first', array('test_2b'));
|
||||||
|
|
||||||
$this->assertNotContains('test_1b', Object::uninherited_static('ObjectStaticTest_First', 'first'));
|
// Check that it can be applied to parent and subclasses, and queried directly
|
||||||
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Second', 'first'));
|
$this->assertContains('test_1b', Object::uninherited_static('ObjectStaticTest_First', 'first'));
|
||||||
|
$this->assertContains('test_2b', Object::uninherited_static('ObjectStaticTest_Second', 'first'));
|
||||||
|
|
||||||
|
// But it won't affect subclasses - this is *uninherited* static
|
||||||
|
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Third', 'first'));
|
||||||
|
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
|
||||||
|
|
||||||
|
// Subclasses that don't have the static explicitly defined should allow definition, also
|
||||||
|
// This also checks that add_static_var can be called after the first uninherited_static()
|
||||||
|
// call (which can be buggy due to caching)
|
||||||
|
Object::add_static_var('ObjectStaticTest_Fourth', 'first', array('test_4b'));
|
||||||
|
$this->assertContains('test_4b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user