From d8b46e5501227942ff9ee9d9c5c38a28ca658393 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 26 May 2009 00:52:54 +0000 Subject: [PATCH] ENHANCEMENT: Allow altering of DataObject:$api_access by decorators. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77787 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObjectDecorator.php | 43 ++++++++++++++++++------------ tests/DataObjectDecoratorTest.php | 10 ++++++- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/core/model/DataObjectDecorator.php b/core/model/DataObjectDecorator.php index 0ffe74b7a..58a1262dc 100755 --- a/core/model/DataObjectDecorator.php +++ b/core/model/DataObjectDecorator.php @@ -13,18 +13,22 @@ abstract class DataObjectDecorator extends Extension { * which can be decorated onto. This list is * limited for security and performance reasons. * + * Keys are the static names, and the values are whether or not the value is an array that should + * be merged. + * * @var array */ protected static $decoratable_statics = array( - 'db', - 'has_one', - 'indexes', - 'defaults', - 'has_many', - 'many_many', - 'belongs_many_many', - 'many_many_extraFields', - 'searchable_fields', + 'db' => true, + 'has_one' => true, + 'indexes' => true, + 'defaults' => true, + 'has_many' => true, + 'many_many' => true, + 'belongs_many_many' => true, + 'many_many_extraFields' => true, + 'searchable_fields' => true, + 'api_access' => false, ); /** @@ -53,14 +57,19 @@ abstract class DataObjectDecorator extends Extension { if(Object::has_extension($this->owner->parentClass(), $this->class)) return; if($fields = $this->extraStatics()) { - foreach($fields as $relation => $fields) { - if(in_array($relation, self::$decoratable_statics)) { - // Can't use add_static_var() here as it would merge the array rather than replacing - Object::set_static ( - $this->owner->class, - $relation, - array_merge((array) Object::get_static($this->owner->class, $relation), $fields) - ); + foreach($fields as $relation => $newVal) { + if(isset(self::$decoratable_statics[$relation])) { + $origVal = Object::get_static($this->owner->class, $relation); + + // Array to be merged + if(self::$decoratable_statics[$relation]) { + // Can't use add_static_var() here as it would merge the array rather than replacing + Object::set_static($this->owner->class, $relation, array_merge((array)$origVal, $newVal)); + + // Value to be overwritten + } else { + Object::set_static ($this->owner->class, $relation, $newVal); + } } } diff --git a/tests/DataObjectDecoratorTest.php b/tests/DataObjectDecoratorTest.php index 2a20d013b..0ebd9e1c1 100644 --- a/tests/DataObjectDecoratorTest.php +++ b/tests/DataObjectDecoratorTest.php @@ -31,6 +31,13 @@ class DataObjectDecoratorTest extends SapphireTest { $contact->delete(); } + /** + * Test that DataObject::$api_access can be set to true via a decorator + */ + function testApiAccessCanBeDecorated() { + $this->assertTrue(Object::get_static('DataObjectDecoratorTest_Member', 'api_access')); + } + function testPermissionDecoration() { // testing behaviour in isolation, too many sideeffects and other checks // in SiteTree->can*() methods to test one single feature reliably with them @@ -97,7 +104,8 @@ class DataObjectDecoratorTest_ContactRole extends DataObjectDecorator implements ), 'defaults' => array( 'Phone' => '123' - ) + ), + 'api_access' => true, ); }