Skip to content

Instantly share code, notes, and snippets.

@ajshort
Created November 2, 2013 14:05
Show Gist options
  • Save ajshort/7279243 to your computer and use it in GitHub Desktop.
Save ajshort/7279243 to your computer and use it in GitHub Desktop.
diff --git a/control/injector/InjectionCreator.php b/control/injector/InjectionCreator.php
index c521a68..fdfab80 100644
--- a/control/injector/InjectionCreator.php
+++ b/control/injector/InjectionCreator.php
@@ -6,7 +6,7 @@
* @package framework
* @subpackage injector
*/
-class InjectionCreator {
+class InjectionCreator implements InjectorObjectCreator {
/**
* @param string $object
diff --git a/control/injector/Injector.php b/control/injector/Injector.php
index 82d9cae..7fcf189 100644
--- a/control/injector/Injector.php
+++ b/control/injector/Injector.php
@@ -1,9 +1,10 @@
<?php
-require_once dirname(__FILE__) . '/InjectionCreator.php';
-require_once dirname(__FILE__) . '/SilverStripeInjectionCreator.php';
-require_once dirname(__FILE__) . '/ServiceConfigurationLocator.php';
-require_once dirname(__FILE__) . '/SilverStripeServiceConfigurationLocator.php';
+require_once __DIR__ . '/InjectorObjectCreator.php';
+require_once __DIR__ . '/InjectionCreator.php';
+require_once __DIR__ . '/SilverStripeInjectionCreator.php';
+require_once __DIR__ . '/ServiceConfigurationLocator.php';
+require_once __DIR__ . '/SilverStripeServiceConfigurationLocator.php';
/**
* A simple injection manager that manages creating objects and injecting
@@ -121,6 +122,13 @@ require_once dirname(__FILE__) . '/SilverStripeServiceConfigurationLocator.php';
class Injector {
/**
+ * The default object factory class.
+ *
+ * @var string
+ */
+ const DEFAULT_CREATOR = 'InjectionCreator';
+
+ /**
* Local store of all services
*
* @var array
@@ -161,16 +169,18 @@ class Injector {
* @var boolean
*/
private $autoScanProperties = false;
-
+
/**
- * The object used to create new class instances
- *
- * Use a custom class here to change the way classes are created to use
- * a custom creation method. By default the InjectionCreator class is used,
- * which simply creates a new class via 'new', however this could be overridden
- * to use, for example, SilverStripe's Object::create() method.
+ * The default factory used to create new instances.
+ *
+ * The {@link InjectionCreator} is used by default, which simply directly
+ * creates objects. This can be changed to use a different default creation
+ * method if desired.
+ *
+ * Each individual component can also specify a custom factory to use by
+ * using the `factory` parameter.
*
- * @var InjectionCreator
+ * @var InjectorObjectCreator
*/
protected $objectCreator;
@@ -192,7 +202,7 @@ class Injector {
$this->autoProperties = array();
- $creatorClass = isset($config['creator']) ? $config['creator'] : 'InjectionCreator';
+ $creatorClass = isset($config['creator']) ? $config['creator'] : self::DEFAULT_CREATOR;
$locatorClass = isset($config['locator']) ? $config['locator'] : 'ServiceConfigurationLocator';
$this->objectCreator = new $creatorClass;
@@ -228,17 +238,16 @@ class Injector {
}
/**
- * Sets the object to use for creating new objects
+ * Sets the default factory to use for creating new objects.
*
- * @param InjectionCreator $obj
+ * @param InjectorObjectCreator $obj
*/
- public function setObjectCreator($obj) {
+ public function setObjectCreator(InjectorObjectCreator $obj) {
$this->objectCreator = $obj;
}
/**
- * Accessor (for testing purposes)
- * @return InjectionCreator
+ * @return InjectorObjectCreator
*/
public function getObjectCreator() {
return $this->objectCreator;
@@ -488,8 +497,10 @@ class Injector {
$constructorParams = $spec['constructor'];
}
- $object = $this->objectCreator->create($class, $constructorParams);
-
+ /** @var InjectorObjectCreator $creator */
+ $creator = isset($spec['creator']) ? $this->get($spec['creator']) : $this->getObjectCreator();
+ $object = $creator->create($class, $constructorParams);
+
// figure out if we have a specific id set or not. In some cases, we might be instantiating objects
// that we don't manage directly; we don't want to store these in the service cache below
if (!$id) {
diff --git a/control/injector/SilverStripeInjectionCreator.php b/control/injector/SilverStripeInjectionCreator.php
index 809d611..b9f1a1f 100644
--- a/control/injector/SilverStripeInjectionCreator.php
+++ b/control/injector/SilverStripeInjectionCreator.php
@@ -5,7 +5,7 @@
* @subpackage injector
*/
-class SilverStripeInjectionCreator {
+class SilverStripeInjectionCreator implements InjectorObjectCreator {
/**
*
* @param string $object
@emman2141
Copy link

Kk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment