2012-05-09 14:26:29 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-03 01:30:22 +01:00
|
|
|
require_once FRAMEWORK_PATH . '/src/SilverStripe/Framework/Injector/Factory.php';
|
|
|
|
|
|
|
|
require_once __DIR__ . '/InjectionCreator.php';
|
|
|
|
require_once __DIR__ . '/SilverStripeInjectionCreator.php';
|
|
|
|
require_once __DIR__ . '/ServiceConfigurationLocator.php';
|
|
|
|
require_once __DIR__ . '/SilverStripeServiceConfigurationLocator.php';
|
|
|
|
|
|
|
|
use SilverStripe\Framework\Injector\Factory;
|
2013-09-13 09:26:06 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* A simple injection manager that manages creating objects and injecting
|
|
|
|
* dependencies between them. It borrows quite a lot from ideas taken from
|
|
|
|
* Spring's configuration, but is adapted to the stateless PHP way of doing
|
2014-08-15 08:53:05 +02:00
|
|
|
* things.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* In its simplest form, the dependency injector can be used as a mechanism to
|
|
|
|
* instantiate objects. Simply call
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Injector::inst()->get('ClassName')
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* and a new instance of ClassName will be created and returned to you.
|
|
|
|
*
|
|
|
|
* Classes can have specific configuration defined for them to
|
|
|
|
* indicate dependencies that should be injected. This takes the form of
|
2012-05-09 14:26:29 +02:00
|
|
|
* a static variable $dependencies defined in the class (or configuration),
|
2014-08-15 08:53:05 +02:00
|
|
|
* which indicates the name of a property that should be set.
|
|
|
|
*
|
|
|
|
* eg
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* <code>
|
|
|
|
* class MyController extends Controller {
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* public $permissions;
|
|
|
|
* public $defaultText;
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* static $dependencies = array(
|
|
|
|
* 'defaultText' => 'Override in configuration',
|
|
|
|
* 'permissions' => '%$PermissionService',
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* will result in an object of type MyController having the defaultText property
|
|
|
|
* set to 'Override in configuration', and an object identified
|
2014-08-15 08:53:05 +02:00
|
|
|
* as PermissionService set into the property called 'permissions'. The %$
|
2012-05-09 14:26:29 +02:00
|
|
|
* syntax tells the injector to look the provided name up as an item to be created
|
2014-08-15 08:53:05 +02:00
|
|
|
* by the Injector itself.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* A key concept of the injector is whether to manage the object as
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* * A pseudo-singleton, in that only one item will be created for a particular
|
|
|
|
* identifier (but the same class could be used for multiple identifiers)
|
|
|
|
* * A prototype, where the same configuration is used, but a new object is
|
|
|
|
* created each time
|
2014-08-15 08:53:05 +02:00
|
|
|
* * unmanaged, in which case a new object is created and injected, but no
|
2012-05-09 14:26:29 +02:00
|
|
|
* information about its state is managed.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Additional configuration of items managed by the injector can be done by
|
|
|
|
* providing configuration for the types, either by manually loading in an
|
2012-05-09 14:26:29 +02:00
|
|
|
* array describing the configuration, or by specifying the configuration
|
2014-08-15 08:53:05 +02:00
|
|
|
* for a type via SilverStripe's configuration mechanism.
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
|
|
|
* Specify a configuration array of the format
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* array(
|
|
|
|
* 'id' => 'BeanId', // the name to be used if diff from the filename
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'priority' => 1, // priority. If another bean is defined with the same ID,
|
2012-05-09 14:26:29 +02:00
|
|
|
* // but has a lower priority, it is NOT overridden
|
|
|
|
* 'class' => 'ClassName', // the name of the PHP class
|
|
|
|
* 'src' => '/path/to/file' // the location of the class
|
2012-09-26 23:34:00 +02:00
|
|
|
* 'type' => 'singleton|prototype' // if you want prototype object generation, set it as the
|
|
|
|
* // type
|
2012-05-09 14:26:29 +02:00
|
|
|
* // By default, singleton is assumed
|
|
|
|
*
|
2014-02-03 01:30:22 +01:00
|
|
|
* 'factory' => 'FactoryService' // A factory service to use to create instances.
|
2012-05-09 14:26:29 +02:00
|
|
|
* 'construct' => array( // properties to set at construction
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'scalar',
|
2012-05-09 14:26:29 +02:00
|
|
|
* '%$BeanId',
|
|
|
|
* )
|
|
|
|
* 'properties' => array(
|
|
|
|
* 'name' => 'value' // scalar value
|
|
|
|
* 'name' => '%$BeanId', // a reference to another bean
|
|
|
|
* 'name' => array(
|
|
|
|
* 'scalar',
|
|
|
|
* '%$BeanId'
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* // alternatively
|
|
|
|
* 'MyBean' => array(
|
|
|
|
* 'class' => 'ClassName',
|
|
|
|
* )
|
|
|
|
* // or simply
|
|
|
|
* 'OtherBean' => 'SomeClass',
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* In addition to specifying the bindings directly in the configuration,
|
|
|
|
* you can simply create a publicly accessible property on the target
|
2014-02-03 01:30:22 +01:00
|
|
|
* class which will automatically be injected if the autoScanProperties
|
2012-05-09 14:26:29 +02:00
|
|
|
* option is set to true. This means a class defined as
|
2014-02-03 01:30:22 +01:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* <code>
|
|
|
|
* class MyController extends Controller {
|
2014-02-03 01:30:22 +01:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* private $permissionService;
|
2014-02-03 01:30:22 +01:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* public setPermissionService($p) {
|
|
|
|
* $this->permissionService = $p;
|
2014-02-03 01:30:22 +01:00
|
|
|
* }
|
2012-05-09 14:26:29 +02:00
|
|
|
* }
|
|
|
|
* </code>
|
2014-02-03 01:30:22 +01:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* will have setPermissionService called if
|
2014-02-03 01:30:22 +01:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* * Injector::inst()->setAutoScanProperties(true) is called and
|
2014-02-03 01:30:22 +01:00
|
|
|
* * A service named 'PermissionService' has been configured
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* @author marcus@silverstripe.com.au
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2012-05-09 14:26:29 +02:00
|
|
|
* @subpackage injector
|
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/
|
|
|
|
*/
|
|
|
|
class Injector {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Local store of all services
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $serviceCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of items that need to be mapped for each service that gets injected
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $injectMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A store of all the service configurations that have been defined.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $specs;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* A map of all the properties that should be automagically set on all
|
2012-05-09 14:26:29 +02:00
|
|
|
* objects instantiated by the injector
|
|
|
|
*/
|
|
|
|
private $autoProperties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A singleton if you want to use it that way
|
|
|
|
*
|
|
|
|
* @var Injector
|
|
|
|
*/
|
|
|
|
private static $instance;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Indicates whether or not to automatically scan properties in injected objects to auto inject
|
2014-08-15 08:53:05 +02:00
|
|
|
* stuff, similar to the way grails does things.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $autoScanProperties = false;
|
2014-02-03 01:30:22 +01:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2014-02-03 01:30:22 +01:00
|
|
|
* 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.
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
2014-02-03 01:30:22 +01:00
|
|
|
* @var Factory
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
protected $objectCreator;
|
2015-09-28 11:21:08 +02:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
/**
|
|
|
|
* Locator for determining Config properties for services
|
2015-09-28 11:21:08 +02:00
|
|
|
*
|
|
|
|
* @var ServiceConfigurationLocator
|
2014-03-12 02:58:49 +01:00
|
|
|
*/
|
|
|
|
protected $configLocator;
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Create a new injector.
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
|
|
|
* @param array $config
|
|
|
|
* Service configuration
|
|
|
|
*/
|
|
|
|
public function __construct($config = null) {
|
|
|
|
$this->injectMap = array();
|
2012-06-14 09:11:36 +02:00
|
|
|
$this->serviceCache = array(
|
|
|
|
'Injector' => $this,
|
|
|
|
);
|
|
|
|
$this->specs = array(
|
|
|
|
'Injector' => array('class' => 'Injector')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->autoProperties = array();
|
2014-02-03 01:30:22 +01:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
$creatorClass = isset($config['creator']) ? $config['creator'] : 'InjectionCreator';
|
|
|
|
$locatorClass = isset($config['locator']) ? $config['locator'] : 'ServiceConfigurationLocator';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->objectCreator = new $creatorClass;
|
|
|
|
$this->configLocator = new $locatorClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if ($config) {
|
|
|
|
$this->load($config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
/**
|
|
|
|
* The injector instance this one was copied from when Injector::nest() was called.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-04-22 05:21:12 +02:00
|
|
|
* @var Injector
|
|
|
|
*/
|
|
|
|
protected $nestedFrom = null;
|
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* If a user wants to use the injector as a static reference
|
|
|
|
*
|
|
|
|
* @param array $config
|
|
|
|
* @return Injector
|
|
|
|
*/
|
|
|
|
public static function inst($config=null) {
|
|
|
|
if (!self::$instance) {
|
|
|
|
self::$instance = new Injector($config);
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
2014-02-03 01:30:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the default global injector instance.
|
|
|
|
*
|
|
|
|
* @param Injector $instance
|
2014-04-22 05:21:12 +02:00
|
|
|
* @return Injector Reference to new active Injector instance
|
2014-02-03 01:30:22 +01:00
|
|
|
*/
|
|
|
|
public static function set_inst(Injector $instance) {
|
2014-04-22 05:21:12 +02:00
|
|
|
return self::$instance = $instance;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Make the newly active {@link Injector} be a copy of the current active
|
2014-04-22 05:21:12 +02:00
|
|
|
* {@link Injector} instance.
|
|
|
|
*
|
|
|
|
* You can then make changes to the injector with methods such as
|
|
|
|
* {@link Injector::inst()->registerService()} which will be discarded
|
|
|
|
* upon a subsequent call to {@link Injector::unnest()}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-04-22 05:21:12 +02:00
|
|
|
* @return Injector Reference to new active Injector instance
|
|
|
|
*/
|
|
|
|
public static function nest() {
|
|
|
|
$current = self::$instance;
|
|
|
|
|
|
|
|
$new = clone $current;
|
|
|
|
$new->nestedFrom = $current;
|
|
|
|
return self::set_inst($new);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Change the active Injector back to the Injector instance the current active
|
2014-04-22 05:21:12 +02:00
|
|
|
* Injector object was copied from.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-04-22 05:21:12 +02:00
|
|
|
* @return Injector Reference to restored active Injector instance
|
|
|
|
*/
|
|
|
|
public static function unnest() {
|
2015-03-07 13:11:45 +01:00
|
|
|
if (self::inst()->nestedFrom) {
|
|
|
|
self::set_inst(self::inst()->nestedFrom);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
user_error(
|
|
|
|
"Unable to unnest root Injector, please make sure you don't have mis-matched nest/unnest",
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return self::inst();
|
2014-02-03 01:30:22 +01:00
|
|
|
}
|
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Indicate whether we auto scan injected objects for properties to set.
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
|
|
|
* @param boolean $val
|
|
|
|
*/
|
|
|
|
public function setAutoScanProperties($val) {
|
|
|
|
$this->autoScanProperties = $val;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2014-02-03 01:30:22 +01:00
|
|
|
* Sets the default factory to use for creating new objects.
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
2014-02-03 01:30:22 +01:00
|
|
|
* @param Factory $obj
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
2014-02-03 01:30:22 +01:00
|
|
|
public function setObjectCreator(Factory $obj) {
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->objectCreator = $obj;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 12:27:22 +02:00
|
|
|
/**
|
2014-02-03 01:30:22 +01:00
|
|
|
* @return Factory
|
2012-06-14 12:27:22 +02:00
|
|
|
*/
|
|
|
|
public function getObjectCreator() {
|
|
|
|
return $this->objectCreator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 12:27:22 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set the configuration locator
|
|
|
|
* @param ServiceConfigurationLocator $configLocator
|
2012-06-14 12:27:22 +02:00
|
|
|
*/
|
|
|
|
public function setConfigLocator($configLocator) {
|
|
|
|
$this->configLocator = $configLocator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 12:27:22 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Retrieve the configuration locator
|
|
|
|
* @return ServiceConfigurationLocator
|
2012-06-14 12:27:22 +02:00
|
|
|
*/
|
|
|
|
public function getConfigLocator() {
|
|
|
|
return $this->configLocator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Add in a specific mapping that should be catered for on a type.
|
2012-05-09 14:26:29 +02:00
|
|
|
* This allows configuration of what should occur when an object
|
|
|
|
* of a particular type is injected, and what items should be injected
|
|
|
|
* for those properties / methods.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $class The class to set a mapping for
|
|
|
|
* @param string $property The property to set the mapping for
|
|
|
|
* @param string $toInject The registered type that will be injected
|
|
|
|
* @param string $injectVia Whether to inject by setting a property or calling a setter
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function setInjectMapping($class, $property, $toInject, $injectVia = 'property') {
|
|
|
|
$mapping = isset($this->injectMap[$class]) ? $this->injectMap[$class] : array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$mapping[$property] = array('name' => $toInject, 'type' => $injectVia);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->injectMap[$class] = $mapping;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Add an object that should be automatically set on managed objects
|
|
|
|
*
|
|
|
|
* This allows you to specify, for example, that EVERY managed object
|
|
|
|
* will be automatically inject with a log object by the following
|
|
|
|
*
|
|
|
|
* $injector->addAutoProperty('log', new Logger());
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* the name of the property
|
|
|
|
* @param object $object
|
|
|
|
* the object to be set
|
|
|
|
*/
|
|
|
|
public function addAutoProperty($property, $object) {
|
|
|
|
$this->autoProperties[$property] = $object;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load services using the passed in configuration for those services
|
|
|
|
*
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function load($config = array()) {
|
|
|
|
$services = array();
|
|
|
|
|
|
|
|
foreach ($config as $specId => $spec) {
|
|
|
|
if (is_string($spec)) {
|
|
|
|
$spec = array('class' => $spec);
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$file = isset($spec['src']) ? $spec['src'] : null;
|
2012-05-09 14:26:29 +02:00
|
|
|
$name = null;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
$filename = basename($file);
|
|
|
|
$name = substr($filename, 0, strrpos($filename, '.'));
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// class is whatever's explicitly set,
|
2012-05-09 14:26:29 +02:00
|
|
|
$class = isset($spec['class']) ? $spec['class'] : $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// or the specid if nothing else available.
|
|
|
|
if (!$class && is_string($specId)) {
|
|
|
|
$class = $specId;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// make sure the class is set...
|
|
|
|
$spec['class'] = $class;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$id = is_string($specId) ? $specId : (isset($spec['id']) ? $spec['id'] : $class);
|
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$priority = isset($spec['priority']) ? $spec['priority'] : 1;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// see if we already have this defined. If so, check priority weighting
|
|
|
|
if (isset($this->specs[$id]) && isset($this->specs[$id]['priority'])) {
|
|
|
|
if ($this->specs[$id]['priority'] > $priority) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, actually include it now we know we're going to use it
|
|
|
|
if (file_exists($file)) {
|
|
|
|
require_once $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure to set the id for later when instantiating
|
|
|
|
// to ensure we get cached
|
|
|
|
$spec['id'] = $id;
|
|
|
|
|
|
|
|
// We've removed this check because new functionality means that the 'class' field doesn't need to refer
|
|
|
|
// specifically to a class anymore - it could be a compound statement, ala SilverStripe's old Object::create
|
|
|
|
// functionality
|
2014-08-15 08:53:05 +02:00
|
|
|
//
|
2012-05-09 14:26:29 +02:00
|
|
|
// if (!class_exists($class)) {
|
|
|
|
// throw new Exception("Failed to load '$class' from $file");
|
|
|
|
// }
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// store the specs for now - we lazy load on demand later on.
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->specs[$id] = $spec;
|
|
|
|
|
|
|
|
// EXCEPT when there's already an existing instance at this id.
|
|
|
|
// if so, we need to instantiate and replace immediately
|
|
|
|
if (isset($this->serviceCache[$id])) {
|
2012-12-07 03:16:33 +01:00
|
|
|
$this->updateSpecConstructor($spec);
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->instantiate($spec, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Update the configuration of an already defined service
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Use this if you don't want to register a complete new config, just append
|
|
|
|
* to an existing configuration. Helpful to avoid overwriting someone else's changes
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* updateSpec('RequestProcessor', 'filters', '%$MyFilter')
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* The name of the service to update the definition for
|
|
|
|
* @param string $property
|
2014-08-15 08:53:05 +02:00
|
|
|
* The name of the property to update.
|
|
|
|
* @param mixed $value
|
2012-05-09 14:26:29 +02:00
|
|
|
* The value to set
|
|
|
|
* @param boolean $append
|
|
|
|
* Whether to append (the default) when the property is an array
|
|
|
|
*/
|
|
|
|
public function updateSpec($id, $property, $value, $append = true) {
|
|
|
|
if (isset($this->specs[$id]['properties'][$property])) {
|
|
|
|
// by ref so we're updating the actual value
|
|
|
|
$current = &$this->specs[$id]['properties'][$property];
|
|
|
|
if (is_array($current) && $append) {
|
|
|
|
$current[] = $value;
|
|
|
|
} else {
|
|
|
|
$this->specs[$id]['properties'][$property] = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// and reload the object; existing bindings don't get
|
2014-08-15 08:53:05 +02:00
|
|
|
// updated though! (for now...)
|
2012-05-09 14:26:29 +02:00
|
|
|
if (isset($this->serviceCache[$id])) {
|
2014-02-07 04:59:51 +01:00
|
|
|
$this->instantiate(array('class'=>$id), $id);
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
/**
|
|
|
|
* Update a class specification to convert constructor configuration information if needed
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* We do this as a separate process to avoid unneeded calls to convertServiceProperty
|
|
|
|
*
|
2012-12-07 03:16:33 +01:00
|
|
|
* @param array $spec
|
|
|
|
* The class specification to update
|
|
|
|
*/
|
|
|
|
protected function updateSpecConstructor(&$spec) {
|
|
|
|
if (isset($spec['constructor'])) {
|
|
|
|
$spec['constructor'] = $this->convertServiceProperty($spec['constructor']);
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively convert a value into its proper representation with service references
|
|
|
|
* resolved to actual objects
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $value
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function convertServiceProperty($value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$newVal = array();
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$newVal[$k] = $this->convertServiceProperty($v);
|
|
|
|
}
|
|
|
|
return $newVal;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if (is_string($value) && strpos($value, '%$') === 0) {
|
|
|
|
$id = substr($value, 2);
|
|
|
|
return $this->get($id);
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a managed object
|
|
|
|
*
|
|
|
|
* Given a specification of the form
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 'class' => 'ClassName',
|
|
|
|
* 'properties' => array('property' => 'scalar', 'other' => '%$BeanRef')
|
|
|
|
* 'id' => 'ServiceId',
|
|
|
|
* 'type' => 'singleton|prototype'
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* will create a new object, store it in the service registry, and
|
|
|
|
* set any relevant properties
|
|
|
|
*
|
|
|
|
* Optionally, you can pass a class name directly for creation
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* To access this from the outside, you should call ->get('Name') to ensure
|
2014-08-15 08:53:05 +02:00
|
|
|
* the appropriate checks are made on the specific type.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
*
|
|
|
|
* @param array $spec
|
|
|
|
* The specification of the class to instantiate
|
2012-06-14 09:11:36 +02:00
|
|
|
* @param string $id
|
|
|
|
* The name of the object being created. If not supplied, then the id will be inferred from the
|
|
|
|
* object being created
|
|
|
|
* @param string $type
|
|
|
|
* Whether to create as a singleton or prototype object. Allows code to be explicit as to how it
|
|
|
|
* wants the object to be returned
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
2012-06-14 09:11:36 +02:00
|
|
|
protected function instantiate($spec, $id=null, $type = null) {
|
2012-05-09 14:26:29 +02:00
|
|
|
if (is_string($spec)) {
|
|
|
|
$spec = array('class' => $spec);
|
|
|
|
}
|
|
|
|
$class = $spec['class'];
|
|
|
|
|
|
|
|
// create the object, using any constructor bindings
|
|
|
|
$constructorParams = array();
|
|
|
|
if (isset($spec['constructor']) && is_array($spec['constructor'])) {
|
|
|
|
$constructorParams = $spec['constructor'];
|
|
|
|
}
|
|
|
|
|
2014-02-03 01:30:22 +01:00
|
|
|
$factory = isset($spec['factory']) ? $this->get($spec['factory']) : $this->getObjectCreator();
|
|
|
|
$object = $factory->create($class, $constructorParams);
|
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// 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) {
|
|
|
|
$id = isset($spec['id']) ? $spec['id'] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now set the service in place if needbe. This is NOT done for prototype beans, as they're
|
|
|
|
// created anew each time
|
2012-06-14 09:11:36 +02:00
|
|
|
if (!$type) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$type = isset($spec['type']) ? $spec['type'] : null;
|
2012-06-14 09:11:36 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if ($id && (!$type || $type != 'prototype')) {
|
|
|
|
// this ABSOLUTELY must be set before the object is injected.
|
|
|
|
// This prevents circular reference errors down the line
|
|
|
|
$this->serviceCache[$id] = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now inject safely
|
|
|
|
$this->inject($object, $id);
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject $object with available objects from the service cache
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* @todo Track all the existing objects that have had a service bound
|
|
|
|
* into them, so we can update that binding at a later point if needbe (ie
|
|
|
|
* if the managed service changes)
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* The object to inject
|
|
|
|
* @param string $asType
|
|
|
|
* The ID this item was loaded as. This is so that the property configuration
|
|
|
|
* for a type is referenced correctly in case $object is no longer the same
|
2014-08-15 08:53:05 +02:00
|
|
|
* type as the loaded config specification had it as.
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function inject($object, $asType=null) {
|
|
|
|
$objtype = $asType ? $asType : get_class($object);
|
|
|
|
$mapping = isset($this->injectMap[$objtype]) ? $this->injectMap[$objtype] : null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
// first off, set any properties defined in the service specification for this
|
|
|
|
// object type
|
|
|
|
if (isset($this->specs[$objtype]) && isset($this->specs[$objtype]['properties'])) {
|
|
|
|
foreach ($this->specs[$objtype]['properties'] as $key => $value) {
|
|
|
|
$val = $this->convertServiceProperty($value);
|
|
|
|
$this->setObjectProperty($object, $key, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, use any cached information about what properties this object type has
|
|
|
|
// and set based on name resolution
|
|
|
|
if (!$mapping) {
|
|
|
|
if ($this->autoScanProperties) {
|
|
|
|
// we use an object to prevent array copies if/when passed around
|
|
|
|
$mapping = new ArrayObject();
|
|
|
|
|
|
|
|
// This performs public variable based injection
|
|
|
|
$robj = new ReflectionObject($object);
|
|
|
|
$properties = $robj->getProperties();
|
|
|
|
|
|
|
|
foreach ($properties as $propertyObject) {
|
|
|
|
/* @var $propertyObject ReflectionProperty */
|
|
|
|
if ($propertyObject->isPublic() && !$propertyObject->getValue($object)) {
|
|
|
|
$origName = $propertyObject->getName();
|
|
|
|
$name = ucfirst($origName);
|
|
|
|
if ($this->hasService($name)) {
|
|
|
|
// Pull the name out of the registry
|
|
|
|
$value = $this->get($name);
|
|
|
|
$propertyObject->setValue($object, $value);
|
|
|
|
$mapping[$origName] = array('name' => $name, 'type' => 'property');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and this performs setter based injection
|
|
|
|
$methods = $robj->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
|
|
|
|
|
foreach ($methods as $methodObj) {
|
|
|
|
/* @var $methodObj ReflectionMethod */
|
|
|
|
$methName = $methodObj->getName();
|
|
|
|
if (strpos($methName, 'set') === 0) {
|
|
|
|
$pname = substr($methName, 3);
|
|
|
|
if ($this->hasService($pname)) {
|
|
|
|
// Pull the name out of the registry
|
|
|
|
$value = $this->get($pname);
|
|
|
|
$methodObj->invoke($object, $value);
|
|
|
|
$mapping[$methName] = array('name' => $pname, 'type' => 'method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we store the information about what needs to be injected for objects of this
|
|
|
|
// type here
|
|
|
|
$this->injectMap[get_class($object)] = $mapping;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($mapping as $prop => $spec) {
|
|
|
|
if ($spec['type'] == 'property') {
|
|
|
|
$value = $this->get($spec['name']);
|
|
|
|
$object->$prop = $value;
|
|
|
|
} else {
|
|
|
|
$method = $prop;
|
|
|
|
$value = $this->get($spec['name']);
|
|
|
|
$object->$method($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$injections = Config::inst()->get(get_class($object), 'dependencies');
|
|
|
|
// If the type defines some injections, set them here
|
|
|
|
if ($injections && count($injections)) {
|
|
|
|
foreach ($injections as $property => $value) {
|
2014-06-12 00:07:04 +02:00
|
|
|
// we're checking empty in case it already has a property at this name
|
2014-08-15 08:53:05 +02:00
|
|
|
// this doesn't catch privately set things, but they will only be set by a setter method,
|
|
|
|
// which should be responsible for preventing further setting if it doesn't want it.
|
2014-06-12 00:07:04 +02:00
|
|
|
if (empty($object->$property)) {
|
2012-05-09 14:26:29 +02:00
|
|
|
$value = $this->convertServiceProperty($value);
|
|
|
|
$this->setObjectProperty($object, $property, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->autoProperties as $property => $value) {
|
|
|
|
if (!isset($object->$property)) {
|
|
|
|
$value = $this->convertServiceProperty($value);
|
|
|
|
$this->setObjectProperty($object, $property, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the 'injected' method if it exists
|
|
|
|
if (method_exists($object, 'injected')) {
|
|
|
|
$object->injected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to set a property's value
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* Set an object's property to a specific value
|
|
|
|
* @param string $name
|
|
|
|
* The name of the property to set
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param mixed $value
|
2012-05-09 14:26:29 +02:00
|
|
|
* The value to set
|
|
|
|
*/
|
|
|
|
protected function setObjectProperty($object, $name, $value) {
|
|
|
|
if (method_exists($object, 'set'.$name)) {
|
|
|
|
$object->{'set'.$name}($value);
|
|
|
|
} else {
|
|
|
|
$object->$name = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the given service exist, and if so, what's the stored name for it?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* We do a special check here for services that are using compound names. For example,
|
2012-05-09 14:26:29 +02:00
|
|
|
* we might want to say that a property should be injected with Log.File or Log.Memory,
|
2014-08-15 08:53:05 +02:00
|
|
|
* but have only registered a 'Log' service, we'll instead return that.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Will recursively call hasService for each depth of dotting
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2012-05-09 14:26:29 +02:00
|
|
|
* The name of the service (as it might be different from the one passed in)
|
|
|
|
*/
|
|
|
|
public function hasService($name) {
|
|
|
|
// common case, get it overwith first
|
|
|
|
if (isset($this->specs[$name])) {
|
|
|
|
return $name;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// okay, check whether we've got a compound name - don't worry about 0 index, cause that's an
|
2012-05-09 14:26:29 +02:00
|
|
|
// invalid name
|
|
|
|
if (!strpos($name, '.')) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
return $this->hasService(substr($name, 0, strrpos($name, '.')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a service object with an optional name to register it as the
|
|
|
|
* service for
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* @param stdClass $service
|
|
|
|
* The object to register
|
|
|
|
* @param string $replace
|
2014-08-15 08:53:05 +02:00
|
|
|
* The name of the object to replace (if different to the
|
2012-05-09 14:26:29 +02:00
|
|
|
* class name of the object to register)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
2013-10-14 22:53:34 +02:00
|
|
|
public function registerService($service, $replace = null) {
|
2012-05-09 14:26:29 +02:00
|
|
|
$registerAt = get_class($service);
|
|
|
|
if ($replace != null) {
|
|
|
|
$registerAt = $replace;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-14 22:53:34 +02:00
|
|
|
$this->specs[$registerAt] = array('class' => get_class($service));
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->serviceCache[$registerAt] = $service;
|
|
|
|
$this->inject($service);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Register a service with an explicit name
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated since 4.0
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function registerNamedService($name, $service) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'registerNamedService is deprecated, use registerService instead');
|
2013-10-14 22:53:34 +02:00
|
|
|
return $this->registerService($service, $name);
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
/**
|
|
|
|
* Removes a named object from the cached list of objects managed
|
|
|
|
* by the inject
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $name The name to unregister
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
*/
|
|
|
|
public function unregisterNamedObject($name) {
|
|
|
|
unset($this->serviceCache[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Clear out all objects that are managed by the injetor.
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
*/
|
|
|
|
public function unregisterAllObjects() {
|
2012-06-14 09:11:36 +02:00
|
|
|
$this->serviceCache = array('Injector' => $this);
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Get a named managed object
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Will first check to see if the item has been registered as a configured service/bean
|
2014-08-15 08:53:05 +02:00
|
|
|
* and return that if so.
|
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Next, will check to see if there's any registered configuration for the given type
|
|
|
|
* and will then try and load that
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Failing all of that, will just return a new instance of the
|
2012-05-09 14:26:29 +02:00
|
|
|
* specificied object.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* the name of the service to retrieve. If not a registered
|
2012-05-09 14:26:29 +02:00
|
|
|
* service, then a class of the given name is instantiated
|
|
|
|
* @param boolean $asSingleton
|
|
|
|
* Whether to register the created object as a singleton
|
|
|
|
* if no other configuration is found
|
|
|
|
* @param array $constructorArgs
|
|
|
|
* Optional set of arguments to pass as constructor arguments
|
2014-08-15 08:53:05 +02:00
|
|
|
* if this object is to be created from scratch
|
2012-05-09 14:26:29 +02:00
|
|
|
* (ie asSingleton = false)
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return mixed the instance of the specified object
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function get($name, $asSingleton = true, $constructorArgs = null) {
|
|
|
|
// reassign the name as it might actually be a compound name
|
|
|
|
if ($serviceName = $this->hasService($name)) {
|
|
|
|
// check to see what the type of bean is. If it's a prototype,
|
|
|
|
// we don't want to return the singleton version of it.
|
|
|
|
$spec = $this->specs[$serviceName];
|
|
|
|
$type = isset($spec['type']) ? $spec['type'] : null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
// if we're explicitly a prototype OR we're not wanting a singleton
|
2012-05-09 14:26:29 +02:00
|
|
|
if (($type && $type == 'prototype') || !$asSingleton) {
|
2012-06-14 09:11:36 +02:00
|
|
|
if ($spec && $constructorArgs) {
|
2012-05-09 14:26:29 +02:00
|
|
|
$spec['constructor'] = $constructorArgs;
|
2012-12-07 03:16:33 +01:00
|
|
|
} else {
|
2014-08-15 08:53:05 +02:00
|
|
|
// convert any _configured_ constructor args.
|
|
|
|
// we don't call this for get() calls where someone passes in
|
2012-12-07 03:16:33 +01:00
|
|
|
// constructor args, otherwise we end up calling convertServiceParams
|
|
|
|
// way too often
|
|
|
|
$this->updateSpecConstructor($spec);
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
2012-06-14 09:11:36 +02:00
|
|
|
return $this->instantiate($spec, $serviceName, !$type ? 'prototype' : $type);
|
2012-05-09 14:26:29 +02:00
|
|
|
} else {
|
|
|
|
if (!isset($this->serviceCache[$serviceName])) {
|
2012-12-07 03:16:33 +01:00
|
|
|
$this->updateSpecConstructor($spec);
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->instantiate($spec, $serviceName);
|
|
|
|
}
|
|
|
|
return $this->serviceCache[$serviceName];
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$config = $this->configLocator->locateConfigFor($name);
|
|
|
|
if ($config) {
|
|
|
|
$this->load(array($name => $config));
|
|
|
|
if (isset($this->specs[$name])) {
|
|
|
|
$spec = $this->specs[$name];
|
2012-12-07 03:16:33 +01:00
|
|
|
$this->updateSpecConstructor($spec);
|
2013-02-15 00:24:47 +01:00
|
|
|
if ($constructorArgs) {
|
|
|
|
$spec['constructor'] = $constructorArgs;
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
return $this->instantiate($spec, $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// If we've got this far, we're dealing with a case of a user wanting
|
2012-05-09 14:26:29 +02:00
|
|
|
// to create an object based on its name. So, we need to fake its config
|
|
|
|
// if the user wants it managed as a singleton service style object
|
|
|
|
$spec = array('class' => $name, 'constructor' => $constructorArgs);
|
|
|
|
if ($asSingleton) {
|
|
|
|
// need to load the spec in; it'll be given the singleton type by default
|
|
|
|
$this->load(array($name => $spec));
|
|
|
|
return $this->instantiate($spec, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->instantiate($spec);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
/**
|
|
|
|
* Magic method to return an item directly
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-06-14 09:11:36 +02:00
|
|
|
* @param string $name
|
|
|
|
* The named object to retrieve
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
return $this->get($name);
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to get() but always returns a new object of the given type
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Additional parameters are passed through as
|
|
|
|
*
|
|
|
|
* @param string $name
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return mixed A new instance of the specified object
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
|
|
|
public function create($name) {
|
|
|
|
$constructorArgs = func_get_args();
|
|
|
|
array_shift($constructorArgs);
|
|
|
|
return $this->get($name, false, count($constructorArgs) ? $constructorArgs : null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* Creates an object with the supplied argument array
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* @param string $name
|
|
|
|
* Name of the class to create an object of
|
|
|
|
* @param array $args
|
|
|
|
* Arguments to pass to the constructor
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function createWithArgs($name, $constructorArgs) {
|
|
|
|
return $this->get($name, false, $constructorArgs);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|