mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE: Don't have any instance caching in singleton(), rely on Injector for this.
This commit is contained in:
parent
f65a7c6b21
commit
114ebb6953
@ -365,15 +365,10 @@ function getClassFile($className) {
|
|||||||
* @return Object
|
* @return Object
|
||||||
*/
|
*/
|
||||||
function singleton($className) {
|
function singleton($className) {
|
||||||
global $_SINGLETONS;
|
if($className == "Config") user_error("Don't pass Config to singleton()", E_USER_ERROR);
|
||||||
if(!isset($className)) user_error("singleton() Called without a class", E_USER_ERROR);
|
if(!isset($className)) user_error("singleton() Called without a class", E_USER_ERROR);
|
||||||
if(!is_string($className)) user_error("singleton() passed bad class_name: " . var_export($className,true), E_USER_ERROR);
|
if(!is_string($className)) user_error("singleton() passed bad class_name: " . var_export($className,true), E_USER_ERROR);
|
||||||
if(!isset($_SINGLETONS[$className])) {
|
return Injector::inst()->get($className);
|
||||||
if(!class_exists($className)) user_error("Bad class to singleton() - $className", E_USER_ERROR);
|
|
||||||
$_SINGLETONS[$className] = Injector::inst()->get($className);
|
|
||||||
if(!$_SINGLETONS[$className]) user_error("singleton() Unknown class '$className'", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
return $_SINGLETONS[$className];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function project() {
|
function project() {
|
||||||
|
@ -466,6 +466,7 @@ abstract class Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Config::inst()->update($class, 'extensions', array($extension));
|
Config::inst()->update($class, 'extensions', array($extension));
|
||||||
|
Injector::inst()->unregisterAllObjects();
|
||||||
|
|
||||||
// load statics now for DataObject classes
|
// load statics now for DataObject classes
|
||||||
if(is_subclass_of($class, 'DataObject')) {
|
if(is_subclass_of($class, 'DataObject')) {
|
||||||
@ -496,8 +497,7 @@ abstract class Object {
|
|||||||
Config::inst()->remove($class, 'extensions', Config::anything(), $extension);
|
Config::inst()->remove($class, 'extensions', Config::anything(), $extension);
|
||||||
|
|
||||||
// unset singletons to avoid side-effects
|
// unset singletons to avoid side-effects
|
||||||
global $_SINGLETONS;
|
Injector::inst()->unregisterAllObjects();
|
||||||
$_SINGLETONS = array();
|
|
||||||
|
|
||||||
// unset some caches
|
// unset some caches
|
||||||
$subclasses = ClassInfo::subclassesFor($class);
|
$subclasses = ClassInfo::subclassesFor($class);
|
||||||
@ -541,7 +541,6 @@ abstract class Object {
|
|||||||
|
|
||||||
if($extensionClasses = ClassInfo::ancestry($this->class)) foreach($extensionClasses as $class) {
|
if($extensionClasses = ClassInfo::ancestry($this->class)) foreach($extensionClasses as $class) {
|
||||||
if(in_array($class, $notExtendable)) continue;
|
if(in_array($class, $notExtendable)) continue;
|
||||||
|
|
||||||
if($extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED)) {
|
if($extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED)) {
|
||||||
foreach($extensions as $extension) {
|
foreach($extensions as $extension) {
|
||||||
// Get the extension class for this extension
|
// Get the extension class for this extension
|
||||||
|
@ -299,8 +299,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
// clear singletons, they're caching old extension info
|
// clear singletons, they're caching old extension info
|
||||||
// which is used in DatabaseAdmin->doBuild()
|
// which is used in DatabaseAdmin->doBuild()
|
||||||
global $_SINGLETONS;
|
Injector::inst()->unregisterAllObjects();
|
||||||
$_SINGLETONS = array();
|
|
||||||
|
|
||||||
// Set default timezone consistently to avoid NZ-specific dependencies
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
||||||
date_default_timezone_set('UTC');
|
date_default_timezone_set('UTC');
|
||||||
@ -789,9 +788,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
function resetDBSchema($includeExtraDataObjects = false) {
|
function resetDBSchema($includeExtraDataObjects = false) {
|
||||||
if(self::using_temp_db()) {
|
if(self::using_temp_db()) {
|
||||||
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
||||||
global $_SINGLETONS;
|
|
||||||
Injector::inst()->unregisterAllObjects();
|
Injector::inst()->unregisterAllObjects();
|
||||||
$_SINGLETONS = array();
|
|
||||||
|
|
||||||
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
||||||
array_shift($dataClasses);
|
array_shift($dataClasses);
|
||||||
|
@ -123,7 +123,8 @@ class DataQuery {
|
|||||||
|
|
||||||
$this->query->setFrom("\"$baseClass\"");
|
$this->query->setFrom("\"$baseClass\"");
|
||||||
|
|
||||||
singleton($this->dataClass)->extend('augmentDataQueryCreation', $this->query, $this);
|
$obj = Injector::inst()->get($baseClass);
|
||||||
|
$obj->extend('augmentDataQueryCreation', $this->query, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setQueriedColumns($queriedColumns) {
|
function setQueriedColumns($queriedColumns) {
|
||||||
@ -225,7 +226,9 @@ class DataQuery {
|
|||||||
$query->selectField("CASE WHEN \"$baseClass\".\"ClassName\" IS NOT NULL THEN \"$baseClass\".\"ClassName\" ELSE '$baseClass' END", "RecordClassName");
|
$query->selectField("CASE WHEN \"$baseClass\".\"ClassName\" IS NOT NULL THEN \"$baseClass\".\"ClassName\" ELSE '$baseClass' END", "RecordClassName");
|
||||||
|
|
||||||
// TODO: Versioned, Translatable, SiteTreeSubsites, etc, could probably be better implemented as subclasses of DataQuery
|
// TODO: Versioned, Translatable, SiteTreeSubsites, etc, could probably be better implemented as subclasses of DataQuery
|
||||||
singleton($this->dataClass)->extend('augmentSQL', $query, $this);
|
|
||||||
|
$obj = Injector::inst()->get(ClassInfo::baseDataClass($this->dataClass));
|
||||||
|
$obj->extend('augmentSQL', $query, $this);
|
||||||
|
|
||||||
$this->ensureSelectContainsOrderbyColumns($query);
|
$this->ensureSelectContainsOrderbyColumns($query);
|
||||||
|
|
||||||
|
@ -11,9 +11,7 @@ class ObjectTest extends SapphireTest {
|
|||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
Injector::inst()->unregisterAllObjects();
|
||||||
global $_SINGLETONS;
|
|
||||||
$_SINGLETONS = array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testHasmethodBehaviour() {
|
function testHasmethodBehaviour() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user