Skip to content

Commit

Permalink
BUGFIX Moved initialisation of injector to the start of Core.php to m…
Browse files Browse the repository at this point in the history
…ake sure that it is initialised correctly before additional code blocks reference it (in particular some of the code introduced by Sam that is triggered during add_extension).
  • Loading branch information
Marcus Nyeholt committed Jun 14, 2012
1 parent 56388ef commit 77099dd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
24 changes: 24 additions & 0 deletions control/injector/Injector.php
Expand Up @@ -230,6 +230,30 @@ public function setObjectCreator($obj) {
$this->objectCreator = $obj;
}

/**
* Accessor (for testing purposes)
* @return InjectionCreator
*/
public function getObjectCreator() {
return $this->objectCreator;
}

/**
* Set the configuration locator
* @param ServiceConfigurationLocator $configLocator
*/
public function setConfigLocator($configLocator) {
$this->configLocator = $configLocator;
}

/**
* Retrieve the configuration locator
* @return ServiceConfigurationLocator
*/
public function getConfigLocator() {
return $this->configLocator;
}

/**
* Add in a specific mapping that should be catered for on a type.
* This allows configuration of what should occur when an object
Expand Down
9 changes: 6 additions & 3 deletions core/Core.php
Expand Up @@ -249,6 +249,12 @@
require_once 'core/manifest/TemplateLoader.php';
require_once 'core/manifest/TemplateManifest.php';
require_once 'core/manifest/TokenisedRegularExpression.php';
require_once 'control/injector/Injector.php';

// Initialise the dependency injector as soon as possible, as it is
// subsequently used by some of the following code
$default_options = array('locator' => 'SilverStripeServiceConfigurationLocator');
Injector::inst($default_options);

///////////////////////////////////////////////////////////////////////////////
// MANIFEST
Expand Down Expand Up @@ -286,9 +292,6 @@
*/
Debug::loadErrorHandlers();

// initialise the dependency injector
$default_options = array('locator' => 'SilverStripeServiceConfigurationLocator');
Injector::inst($default_options);

///////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS
Expand Down
6 changes: 6 additions & 0 deletions tests/injector/InjectorTest.php
Expand Up @@ -12,6 +12,12 @@
*/
class InjectorTest extends SapphireTest {

public function testCorrectlyInitialised() {
$injector = Injector::inst();
$this->assertTrue($injector->getConfigLocator() instanceof SilverStripeServiceConfigurationLocator,
'If this fails, it is likely because the injector has been referenced BEFORE being initialised in Core.php');
}

public function testBasicInjector() {
$injector = new Injector();
$injector->setAutoScanProperties(true);
Expand Down

0 comments on commit 77099dd

Please sign in to comment.