APICHANGE: add_extension() is now called directly on the class, instead of on Object

This commit is contained in:
Andrew O'Neil 2012-10-09 16:55:37 +13:00 committed by Sean Harvey
parent 96a408029b
commit fdea5321c7
16 changed files with 37 additions and 29 deletions

View File

@ -450,7 +450,15 @@ abstract class Object {
* @param string $extension Subclass of {@link Extension} with optional parameters * @param string $extension Subclass of {@link Extension} with optional parameters
* as a string, e.g. "Versioned" or "Translatable('Param')" * as a string, e.g. "Versioned" or "Translatable('Param')"
*/ */
public static function add_extension($class, $extension) { public static function add_extension($extension) {
$class = get_called_class();
if(func_num_args() > 1) {
Deprecation::notice('3.1.0', "Object::add_extension() deprecated. Call add_extension() on the class");
$class = func_get_arg(0);
$extension = func_get_arg(1);
}
if(!preg_match('/^([^(]*)/', $extension, $matches)) { if(!preg_match('/^([^(]*)/', $extension, $matches)) {
return false; return false;
} }

View File

@ -291,7 +291,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
if(!Object::has_extension($class, $extension)) { if(!Object::has_extension($class, $extension)) {
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array(); if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
$this->extensionsToRemove[$class][] = $extension; $this->extensionsToRemove[$class][] = $extension;
Object::add_extension($class, $extension); $class::add_extension($extension);
$isAltered = true; $isAltered = true;
} }
} }
@ -326,7 +326,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
// Reapply ones removed // Reapply ones removed
foreach($this->extensionsToReapply as $class => $extensions) { foreach($this->extensionsToReapply as $class => $extensions) {
foreach($extensions as $extension) { foreach($extensions as $extension) {
Object::add_extension($class, $extension); $class::add_extension($extension);
} }
} }
} }

View File

@ -97,7 +97,7 @@ Create a new file called `zzz_admin/code/BookmarkedPageExtension.php` and insert
Enable the extension with the following line in `zzz_mysite/_config.php`: Enable the extension with the following line in `zzz_mysite/_config.php`:
:::php :::php
Object::add_extension('SiteTree', 'BookmarkedPageExtension'); SiteTree::add_extension('BookmarkedPageExtension');
In order to add the field to the database, run a `dev/build/?flush=all`. In order to add the field to the database, run a `dev/build/?flush=all`.
Refresh the CMS, open a page for editing and you should see the new checkbox. Refresh the CMS, open a page for editing and you should see the new checkbox.
@ -121,7 +121,7 @@ Add the following code to a new file `zzz_admin/code/BookmarkedLeftAndMainExtens
Enable the extension with the following line in `zzz_mysite/_config.php`: Enable the extension with the following line in `zzz_mysite/_config.php`:
:::php :::php
Object::add_extension('LeftAndMain', 'BookmarkedPagesLeftAndMainExtension'); LeftAndMain::add_extension('BookmarkedPagesLeftAndMainExtension');
As the last step, replace the hardcoded links with our list from the database. As the last step, replace the hardcoded links with our list from the database.
Find the `<ul>` you created earlier in `zzz_admin/admin/templates/LeftAndMain.ss` Find the `<ul>` you created earlier in `zzz_admin/admin/templates/LeftAndMain.ss`

View File

@ -31,14 +31,14 @@ ForumRole extension to the `[api:Member]` object.
:::php :::php
Object::add_extension('Class You Want To Override', 'Your Class Name'); ClassYouWantToOverride::add_extension('Your Class Name');
For example above we want to override Member with a Custom Member so we would write the following For example above we want to override Member with a Custom Member so we would write the following
:::php :::php
// add to mysite/_config.php // add to mysite/_config.php
Object::add_extension('Member', 'CustomMember'); Member::add_extension('CustomMember');
## Implementation ## Implementation

View File

@ -91,7 +91,7 @@ Using inheritance to add extra behaviour or data fields to a member is limiting,
class. A better way is to use role extensions to add this behaviour. class. A better way is to use role extensions to add this behaviour.
:::php :::php
Object::add_extension('Member', 'ForumRole'); Member::add_extension('ForumRole');
// OR // OR
Member::add_role('ForumRole'); Member::add_role('ForumRole');

View File

@ -198,7 +198,7 @@ also another tool at your disposal: The `[api:Extension]` API.
} }
// mysite/_config.php // mysite/_config.php
Object::add_extension('MyAdmin', 'MyAdminExtension'); MyAdmin::add_extension('MyAdminExtension');
The following extension points are available: `updateEditForm()`, `updateSearchContext()`, The following extension points are available: `updateEditForm()`, `updateSearchContext()`,
`updateSearchForm()`, `updateList()`, `updateImportForm`. `updateSearchForm()`, `updateList()`, `updateImportForm`.

View File

@ -51,7 +51,7 @@ Create a mysite/code/CustomSiteConfig.php file.
Then add a link to your extension in the _config.php file like below. Then add a link to your extension in the _config.php file like below.
Object::add_extension('SiteConfig', 'CustomSiteConfig'); SiteConfig::add_extension('CustomSiteConfig');
This tells SilverStripe to add the CustomSiteConfig extension to the `[api:SiteConfig]` class. This tells SilverStripe to add the CustomSiteConfig extension to the `[api:SiteConfig]` class.

View File

@ -85,7 +85,7 @@ Example: Remove field for "image captions"
:::php :::php
// File: mysite/_config.php // File: mysite/_config.php
Object::add_extension('HtmlEditorField', 'MyToolbarExtension'); HtmlEditorField::add_extension('MyToolbarExtension');
Adding functionality is a bit more advanced, you'll most likely Adding functionality is a bit more advanced, you'll most likely
need to add some fields to the PHP forms, as well as write some need to add some fields to the PHP forms, as well as write some

View File

@ -24,7 +24,7 @@ which map to different database tables.
:::php :::php
// mysite/_config.php // mysite/_config.php
Object::add_extension('MyRecord', 'Versioned("Stage","Live")'); MyRecord::add_extension('Versioned("Stage","Live")');
Note: The extension is automatically applied to `SiteTree` class. Note: The extension is automatically applied to `SiteTree` class.

View File

@ -32,7 +32,7 @@ class FulltextSearchable extends DataExtension {
* It can be used to limit the searched classes, but not to add your own classes. * It can be used to limit the searched classes, but not to add your own classes.
* For this purpose, please use {@link Object::add_extension()} directly: * For this purpose, please use {@link Object::add_extension()} directly:
* <code> * <code>
* Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')"); * MyObject::add_extension("FulltextSearchable('MySearchableField,'MyOtherField')");
* </code> * </code>
* *
* Caution: This is a wrapper method that should only be used in _config.php, * Caution: This is a wrapper method that should only be used in _config.php,
@ -53,7 +53,7 @@ class FulltextSearchable extends DataExtension {
if(isset($defaultColumns[$class])) { if(isset($defaultColumns[$class])) {
Config::inst()->update($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM')); Config::inst()->update($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')"); $class::add_extension("FulltextSearchable('{$defaultColumns[$class]}')");
} else { } else {
throw new Exception( throw new Exception(
"FulltextSearchable::enable() I don't know the default search columns for class '$class'"); "FulltextSearchable::enable() I don't know the default search columns for class '$class'");
@ -61,7 +61,7 @@ class FulltextSearchable extends DataExtension {
} }
self::$searchable_classes = $searchableClasses; self::$searchable_classes = $searchableClasses;
if(class_exists("ContentController")){ if(class_exists("ContentController")){
Object::add_extension("ContentController", "ContentControllerSearchExtension"); ContentController::add_extension("ContentControllerSearchExtension");
} }
} }

View File

@ -228,7 +228,7 @@ class ObjectTest extends SapphireTest {
); );
// ObjectTest_ExtendTest3 is added manually // ObjectTest_ExtendTest3 is added manually
Object::add_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3("Param")'); ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")');
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'), Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'),
"Extensions are detected with static has_extension() when added through add_extension()" "Extensions are detected with static has_extension() when added through add_extension()"
@ -247,7 +247,7 @@ class ObjectTest extends SapphireTest {
public function testRemoveExtension() { public function testRemoveExtension() {
// manually add ObjectTest_ExtendTest2 // manually add ObjectTest_ExtendTest2
Object::add_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'); ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2');
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'), Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are added correctly" "Extension added through \$add_extension() are added correctly"

View File

@ -147,4 +147,4 @@ class FormScaffolderTest_ArticleExtension extends DataExtension implements TestO
} }
DataObject::add_extension('FormScaffolderTest_Article', 'FormScaffolderTest_ArticleExtension'); FormScaffolderTest_Article::add_extension('FormScaffolderTest_ArticleExtension');

View File

@ -75,7 +75,7 @@ class DataExtensionTest extends SapphireTest {
*/ */
public function testAddExtensionLoadsStatics() { public function testAddExtensionLoadsStatics() {
// Object::add_extension() will load DOD statics directly, so let's try adding a extension on the fly // Object::add_extension() will load DOD statics directly, so let's try adding a extension on the fly
Object::add_extension('DataExtensionTest_Player', 'DataExtensionTest_PlayerExtension'); DataExtensionTest_Player::add_extension('DataExtensionTest_PlayerExtension');
// Now that we've just added the extension, we need to rebuild the database // Now that we've just added the extension, we need to rebuild the database
$this->resetDBSchema(true); $this->resetDBSchema(true);
@ -226,7 +226,7 @@ class DataExtensionTest_RelatedObject extends DataObject implements TestOnly {
} }
DataObject::add_extension('DataExtensionTest_Member', 'DataExtensionTest_ContactRole'); DataExtensionTest_Member::add_extension('DataExtensionTest_ContactRole');
class DataExtensionTest_MyObject extends DataObject implements TestOnly { class DataExtensionTest_MyObject extends DataObject implements TestOnly {
@ -305,7 +305,7 @@ class DataExtensionTest_AppliedToDO extends DataExtension implements TestOnly {
} }
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext1'); DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext1');
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext2'); DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext2');
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Faves'); DataExtensionTest_MyObject::add_extension('DataExtensionTest_Faves');

View File

@ -1313,5 +1313,5 @@ class DataObjectTest_TeamComment extends DataObject {
); );
} }
DataObject::add_extension('DataObjectTest_Team', 'DataObjectTest_Team_Extension'); DataObjectTest_Team::add_extension('DataObjectTest_Team_Extension');

View File

@ -18,7 +18,7 @@ class FulltextSearchableTest extends SapphireTest {
public function tearDown() { public function tearDown() {
// TODO This shouldn't need all arguments included // TODO This shouldn't need all arguments included
if($this->orig['File_searchable']) { if($this->orig['File_searchable']) {
Object::add_extension('File', 'FulltextSearchable(\'"Filename","Title","Content"\')'); File::add_extension('FulltextSearchable(\'"Filename","Title","Content"\')');
} }
parent::tearDown(); parent::tearDown();

View File

@ -470,7 +470,7 @@ class MemberTest extends FunctionalTest {
$this->assertFalse($member->canEdit()); $this->assertFalse($member->canEdit());
/* Apply a extension that allows viewing in any case (most likely the case for member profiles) */ /* Apply a extension that allows viewing in any case (most likely the case for member profiles) */
Object::add_extension('Member', 'MemberTest_ViewingAllowedExtension'); Member::add_extension('MemberTest_ViewingAllowedExtension');
$member2 = $this->objFromFixture('Member', 'staffmember'); $member2 = $this->objFromFixture('Member', 'staffmember');
$this->assertTrue($member2->canView()); $this->assertTrue($member2->canView());
@ -479,7 +479,7 @@ class MemberTest extends FunctionalTest {
/* Apply a extension that denies viewing of the Member */ /* Apply a extension that denies viewing of the Member */
Object::remove_extension('Member', 'MemberTest_ViewingAllowedExtension'); Object::remove_extension('Member', 'MemberTest_ViewingAllowedExtension');
Object::add_extension('Member', 'MemberTest_ViewingDeniedExtension'); Member::add_extension('MemberTest_ViewingDeniedExtension');
$member3 = $this->objFromFixture('Member', 'managementmember'); $member3 = $this->objFromFixture('Member', 'managementmember');
$this->assertFalse($member3->canView()); $this->assertFalse($member3->canView());
@ -488,7 +488,7 @@ class MemberTest extends FunctionalTest {
/* Apply a extension that allows viewing and editing but denies deletion */ /* Apply a extension that allows viewing and editing but denies deletion */
Object::remove_extension('Member', 'MemberTest_ViewingDeniedExtension'); Object::remove_extension('Member', 'MemberTest_ViewingDeniedExtension');
Object::add_extension('Member', 'MemberTest_EditingAllowedDeletingDeniedExtension'); Member::add_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
$member4 = $this->objFromFixture('Member', 'accountingmember'); $member4 = $this->objFromFixture('Member', 'accountingmember');
$this->assertTrue($member4->canView()); $this->assertTrue($member4->canView());
@ -603,7 +603,7 @@ class MemberTest extends FunctionalTest {
*/ */
protected function addExtensions($extensions) { protected function addExtensions($extensions) {
if($extensions) foreach($extensions as $extension) { if($extensions) foreach($extensions as $extension) {
Object::add_extension('Member', $extension); Member::add_extension($extension);
} }
return $extensions; return $extensions;
} }