Skip to content

Commit

Permalink
APICHANGE: add_extension() is now called directly on the class, inste…
Browse files Browse the repository at this point in the history
…ad of on Object
  • Loading branch information
andrewandante authored and Sean Harvey committed Nov 6, 2012
1 parent 96a4080 commit fdea532
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 29 deletions.
10 changes: 9 additions & 1 deletion core/Object.php
Expand Up @@ -450,7 +450,15 @@ public static function has_extension($class, $requiredExtension) {
* @param string $extension Subclass of {@link Extension} with optional parameters
* 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)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions dev/SapphireTest.php
Expand Up @@ -291,7 +291,7 @@ public function setUpOnce() {
if(!Object::has_extension($class, $extension)) {
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
$this->extensionsToRemove[$class][] = $extension;
Object::add_extension($class, $extension);
$class::add_extension($extension);
$isAltered = true;
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ public function tearDownOnce() {
// Reapply ones removed
foreach($this->extensionsToReapply as $class => $extensions) {
foreach($extensions as $extension) {
Object::add_extension($class, $extension);
$class::add_extension($extension);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/en/howto/extend-cms-interface.md
Expand Up @@ -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`:

:::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`.
Refresh the CMS, open a page for editing and you should see the new checkbox.
Expand All @@ -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`:

:::php
Object::add_extension('LeftAndMain', 'BookmarkedPagesLeftAndMainExtension');
LeftAndMain::add_extension('BookmarkedPagesLeftAndMainExtension');

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`
Expand Down
4 changes: 2 additions & 2 deletions docs/en/reference/dataextension.md
Expand Up @@ -31,14 +31,14 @@ ForumRole extension to the `[api:Member]` object.


:::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

:::php
// add to mysite/_config.php
Object::add_extension('Member', 'CustomMember');
Member::add_extension('CustomMember');

## Implementation

Expand Down
2 changes: 1 addition & 1 deletion docs/en/reference/member.md
Expand Up @@ -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.

:::php
Object::add_extension('Member', 'ForumRole');
Member::add_extension('ForumRole');
// OR
Member::add_role('ForumRole');

Expand Down
2 changes: 1 addition & 1 deletion docs/en/reference/modeladmin.md
Expand Up @@ -198,7 +198,7 @@ also another tool at your disposal: The `[api:Extension]` API.
}

// mysite/_config.php
Object::add_extension('MyAdmin', 'MyAdminExtension');
MyAdmin::add_extension('MyAdminExtension');

The following extension points are available: `updateEditForm()`, `updateSearchContext()`,
`updateSearchForm()`, `updateList()`, `updateImportForm`.
Expand Down
2 changes: 1 addition & 1 deletion docs/en/reference/siteconfig.md
Expand Up @@ -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.

Object::add_extension('SiteConfig', 'CustomSiteConfig');
SiteConfig::add_extension('CustomSiteConfig');


This tells SilverStripe to add the CustomSiteConfig extension to the `[api:SiteConfig]` class.
Expand Down
2 changes: 1 addition & 1 deletion docs/en/topics/rich-text-editing.md
Expand Up @@ -85,7 +85,7 @@ Example: Remove field for "image captions"

:::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
need to add some fields to the PHP forms, as well as write some
Expand Down
2 changes: 1 addition & 1 deletion docs/en/topics/versioning.md
Expand Up @@ -24,7 +24,7 @@ which map to different database tables.

:::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.

Expand Down
6 changes: 3 additions & 3 deletions search/FulltextSearchable.php
Expand Up @@ -32,7 +32,7 @@ class FulltextSearchable extends DataExtension {
* 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:
* <code>
* Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')");
* MyObject::add_extension("FulltextSearchable('MySearchableField,'MyOtherField')");
* </code>
*
* Caution: This is a wrapper method that should only be used in _config.php,
Expand All @@ -53,15 +53,15 @@ public static function enable($searchableClasses = array('SiteTree', 'File')) {

if(isset($defaultColumns[$class])) {
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 {
throw new Exception(
"FulltextSearchable::enable() I don't know the default search columns for class '$class'");
}
}
self::$searchable_classes = $searchableClasses;
if(class_exists("ContentController")){
Object::add_extension("ContentController", "ContentControllerSearchExtension");
ContentController::add_extension("ContentControllerSearchExtension");
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/core/ObjectTest.php
Expand Up @@ -228,7 +228,7 @@ public function testHasAndAddExtension() {
);

// ObjectTest_ExtendTest3 is added manually
Object::add_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3("Param")');
ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")');
$this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'),
"Extensions are detected with static has_extension() when added through add_extension()"
Expand All @@ -247,7 +247,7 @@ public function testHasAndAddExtension() {

public function testRemoveExtension() {
// manually add ObjectTest_ExtendTest2
Object::add_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2');
ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2');
$this->assertTrue(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are added correctly"
Expand Down
2 changes: 1 addition & 1 deletion tests/forms/FormScaffolderTest.php
Expand Up @@ -147,4 +147,4 @@ public function updateCMSFields(FieldList $fields) {

}

DataObject::add_extension('FormScaffolderTest_Article', 'FormScaffolderTest_ArticleExtension');
FormScaffolderTest_Article::add_extension('FormScaffolderTest_ArticleExtension');
10 changes: 5 additions & 5 deletions tests/model/DataExtensionTest.php
Expand Up @@ -75,7 +75,7 @@ public function testManyManyAssociationWithExtension() {
*/
public function testAddExtensionLoadsStatics() {
// 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
$this->resetDBSchema(true);
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -305,7 +305,7 @@ public function testMethodApplied() {

}

DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext1');
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext2');
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Faves');
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext1');
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext2');
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Faves');

2 changes: 1 addition & 1 deletion tests/model/DataObjectTest.php
Expand Up @@ -1313,5 +1313,5 @@ class DataObjectTest_TeamComment extends DataObject {
);
}

DataObject::add_extension('DataObjectTest_Team', 'DataObjectTest_Team_Extension');
DataObjectTest_Team::add_extension('DataObjectTest_Team_Extension');

2 changes: 1 addition & 1 deletion tests/search/FulltextSearchableTest.php
Expand Up @@ -18,7 +18,7 @@ public function setUp() {
public function tearDown() {
// TODO This shouldn't need all arguments included
if($this->orig['File_searchable']) {
Object::add_extension('File', 'FulltextSearchable(\'"Filename","Title","Content"\')');
File::add_extension('FulltextSearchable(\'"Filename","Title","Content"\')');
}

parent::tearDown();
Expand Down
8 changes: 4 additions & 4 deletions tests/security/MemberTest.php
Expand Up @@ -470,7 +470,7 @@ public function testExtendedCan() {
$this->assertFalse($member->canEdit());

/* 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');

$this->assertTrue($member2->canView());
Expand All @@ -479,7 +479,7 @@ public function testExtendedCan() {

/* Apply a extension that denies viewing of the Member */
Object::remove_extension('Member', 'MemberTest_ViewingAllowedExtension');
Object::add_extension('Member', 'MemberTest_ViewingDeniedExtension');
Member::add_extension('MemberTest_ViewingDeniedExtension');
$member3 = $this->objFromFixture('Member', 'managementmember');

$this->assertFalse($member3->canView());
Expand All @@ -488,7 +488,7 @@ public function testExtendedCan() {

/* Apply a extension that allows viewing and editing but denies deletion */
Object::remove_extension('Member', 'MemberTest_ViewingDeniedExtension');
Object::add_extension('Member', 'MemberTest_EditingAllowedDeletingDeniedExtension');
Member::add_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
$member4 = $this->objFromFixture('Member', 'accountingmember');

$this->assertTrue($member4->canView());
Expand Down Expand Up @@ -603,7 +603,7 @@ public function testMap_in_groupsReturnsAdmins() {
*/
protected function addExtensions($extensions) {
if($extensions) foreach($extensions as $extension) {
Object::add_extension('Member', $extension);
Member::add_extension($extension);
}
return $extensions;
}
Expand Down

0 comments on commit fdea532

Please sign in to comment.