APICHANGE: Use late static binding for Object::has_extension()

This commit is contained in:
Andrew O'Neil 2012-10-10 10:59:23 +13:00 committed by Sean Harvey
parent 6dd6a5c188
commit 0c8de0a1de
8 changed files with 34 additions and 23 deletions

View File

@ -135,7 +135,8 @@ abstract class CMSBatchAction extends Object {
} }
} }
if(Object::has_extension($this->managedClass, 'Versioned')) { $managed_class = $this->managedClass;
if($managed_class::has_extension('Versioned')) {
// Get the pages that only exist on live (deleted from stage) // Get the pages that only exist on live (deleted from stage)
if($checkLivePages && $onlyOnLive) { if($checkLivePages && $onlyOnLive) {
$SQL_ids = implode(', ', array_keys($onlyOnLive)); $SQL_ids = implode(', ', array_keys($onlyOnLive));

View File

@ -85,7 +85,7 @@ class CMSBatchActionHandler extends RequestHandler {
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]); foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
if($ids) { if($ids) {
if(class_exists('Translatable') && Object::has_extension('SiteTree','Translatable')) { if(class_exists('Translatable') && SiteTree::has_extension('Translatable')) {
Translatable::disable_locale_filter(); Translatable::disable_locale_filter();
} }
@ -98,11 +98,12 @@ class CMSBatchActionHandler extends RequestHandler {
) )
); );
if(class_exists('Translatable') && Object::has_extension('SiteTree','Translatable')) { if(class_exists('Translatable') && SiteTree::has_extension('Translatable')) {
Translatable::enable_locale_filter(); Translatable::enable_locale_filter();
} }
if(Object::has_extension($this->recordClass, 'Versioned')) { $record_class = $this->recordClass;
if($record_class::has_extension('Versioned')) {
// If we didn't query all the pages, then find the rest on the live site // If we didn't query all the pages, then find the rest on the live site
if(!$pages || $pages->Count() < sizeof($ids)) { if(!$pages || $pages->Count() < sizeof($ids)) {
foreach($ids as $id) $idsFromLive[$id] = true; foreach($ids as $id) $idsFromLive[$id] = true;

View File

@ -1035,8 +1035,10 @@ class LeftAndMain extends Controller implements PermissionProvider {
if(!$fields->dataFieldByName('ClassName')) { if(!$fields->dataFieldByName('ClassName')) {
$fields->push(new HiddenField('ClassName')); $fields->push(new HiddenField('ClassName'));
} }
$tree_class = $this->stat('tree_class');
if( if(
Object::has_extension($this->stat('tree_class'), 'Hierarchy') $tree_class::has_extension('Hierarchy')
&& !$fields->dataFieldByName('ParentID') && !$fields->dataFieldByName('ParentID')
) { ) {
$fields->push(new HiddenField('ParentID')); $fields->push(new HiddenField('ParentID'));

View File

@ -422,10 +422,17 @@ abstract class Object {
/** /**
* Return TRUE if a class has a specified extension * Return TRUE if a class has a specified extension
* *
* @param string $class
* @param string $requiredExtension the class name of the extension to check for. * @param string $requiredExtension the class name of the extension to check for.
*/ */
public static function has_extension($class, $requiredExtension) { public static function has_extension($requiredExtension) {
$class = get_called_class();
if(func_num_args() > 1) {
Deprecation::notice('3.1.0', "Object::has_extension() deprecated. Call has_extension() on the class");
$class = func_get_arg(0);
$extension = func_get_arg(1);
}
$requiredExtension = strtolower($requiredExtension); $requiredExtension = strtolower($requiredExtension);
$extensions = Config::inst()->get($class, 'extensions'); $extensions = Config::inst()->get($class, 'extensions');

View File

@ -275,7 +275,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
// Remove any illegal extensions that are present // Remove any illegal extensions that are present
foreach($this->illegalExtensions as $class => $extensions) { foreach($this->illegalExtensions as $class => $extensions) {
foreach($extensions as $extension) { foreach($extensions as $extension) {
if (Object::has_extension($class, $extension)) { if ($class::has_extension($extension)) {
if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array(); if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array();
$this->extensionsToReapply[$class][] = $extension; $this->extensionsToReapply[$class][] = $extension;
$class::remove_extension($extension); $class::remove_extension($extension);
@ -288,7 +288,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
foreach($this->requiredExtensions as $class => $extensions) { foreach($this->requiredExtensions as $class => $extensions) {
$this->extensionsToRemove[$class] = array(); $this->extensionsToRemove[$class] = array();
foreach($extensions as $extension) { foreach($extensions as $extension) {
if(!Object::has_extension($class, $extension)) { if(!$class::has_extension($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;
$class::add_extension($extension); $class::add_extension($extension);

View File

@ -69,7 +69,7 @@ class FileIFrameField extends FileField {
if($this->form->getRecord() && $this->form->getRecord()->exists()) { if($this->form->getRecord() && $this->form->getRecord()->exists()) {
$record = $this->form->getRecord(); $record = $this->form->getRecord();
if(class_exists('Translatable') && Object::has_extension('SiteTree', 'Translatable') && $record->Locale){ if(class_exists('Translatable') && SiteTree::has_extension('Translatable') && $record->Locale){
$iframe = "iframe?locale=".$record->Locale; $iframe = "iframe?locale=".$record->Locale;
}else{ }else{
$iframe = "iframe"; $iframe = "iframe";

View File

@ -192,11 +192,11 @@ class ObjectTest extends SapphireTest {
public function testHasAndAddExtension() { public function testHasAndAddExtension() {
// ObjectTest_ExtendTest1 is built in via $extensions // ObjectTest_ExtendTest1 is built in via $extensions
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'OBJECTTEST_ExtendTest1'), ObjectTest_ExtensionTest::has_extension('OBJECTTEST_ExtendTest1'),
"Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity" "Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity"
); );
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest1'), ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest1'),
"Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity" "Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity"
); );
$this->assertTrue( $this->assertTrue(
@ -207,7 +207,7 @@ class ObjectTest extends SapphireTest {
// ObjectTest_ExtendTest2 is built in via $extensions (with parameters) // ObjectTest_ExtendTest2 is built in via $extensions (with parameters)
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest2'), ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest2'),
"Extensions are detected with static has_extension() when set on Object::\$extensions with" "Extensions are detected with static has_extension() when set on Object::\$extensions with"
. " additional parameters" . " additional parameters"
); );
@ -217,7 +217,7 @@ class ObjectTest extends SapphireTest {
. " additional parameters" . " additional parameters"
); );
$this->assertFalse( $this->assertFalse(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'), ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest3'),
"Other extensions available in the system are not present unless explicitly added to this object" "Other extensions available in the system are not present unless explicitly added to this object"
. " when checking through has_extension()" . " when checking through has_extension()"
); );
@ -230,7 +230,7 @@ class ObjectTest extends SapphireTest {
// ObjectTest_ExtendTest3 is added manually // ObjectTest_ExtendTest3 is added manually
ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")'); ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")');
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'), ObjectTest_ExtensionTest::has_extension('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()"
); );
// a singleton() wouldn't work as its already initialized // a singleton() wouldn't work as its already initialized
@ -249,13 +249,13 @@ class ObjectTest extends SapphireTest {
// manually add ObjectTest_ExtendTest2 // manually add ObjectTest_ExtendTest2
ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2'); ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2');
$this->assertTrue( $this->assertTrue(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'), ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are added correctly" "Extension added through \$add_extension() are added correctly"
); );
ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest2'); ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest2');
$this->assertFalse( $this->assertFalse(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'), ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are detected as removed in has_extension()" "Extension added through \$add_extension() are detected as removed in has_extension()"
); );
$this->assertFalse( $this->assertFalse(
@ -266,7 +266,7 @@ class ObjectTest extends SapphireTest {
// ObjectTest_ExtendTest1 is already present in $extensions // ObjectTest_ExtendTest1 is already present in $extensions
ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1'); ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1');
$this->assertFalse( $this->assertFalse(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest1'), ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest1'),
"Extension added through \$extensions are detected as removed in has_extension()" "Extension added through \$extensions are detected as removed in has_extension()"
); );
$objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest(); $objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest();

View File

@ -9,7 +9,7 @@ class FulltextSearchableTest extends SapphireTest {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->orig['File_searchable'] = Object::has_extension('File', 'FulltextSearchable'); $this->orig['File_searchable'] = File::has_extension('FulltextSearchable');
// TODO This shouldn't need all arguments included // TODO This shouldn't need all arguments included
File::remove_extension('FulltextSearchable(\'"Filename","Title","Content"\')'); File::remove_extension('FulltextSearchable(\'"Filename","Title","Content"\')');
@ -26,17 +26,17 @@ class FulltextSearchableTest extends SapphireTest {
public function testEnable() { public function testEnable() {
FulltextSearchable::enable(); FulltextSearchable::enable();
$this->assertTrue(Object::has_extension('File', 'FulltextSearchable')); $this->assertTrue(File::has_extension('FulltextSearchable'));
} }
public function testEnableWithCustomClasses() { public function testEnableWithCustomClasses() {
FulltextSearchable::enable(array('File')); FulltextSearchable::enable(array('File'));
$this->assertTrue(Object::has_extension('File', 'FulltextSearchable')); $this->assertTrue(File::has_extension('FulltextSearchable'));
// TODO This shouldn't need all arguments included // TODO This shouldn't need all arguments included
Object::remove_extension('File', 'FulltextSearchable(\'"Filename","Title","Content"\')'); File::remove_extension('FulltextSearchable(\'"Filename","Title","Content"\')');
$this->assertFalse(Object::has_extension('File', 'FulltextSearchable')); $this->assertFalse(File::has_extension('File', 'FulltextSearchable'));
} }
} }