Dependency injection in phpCube - The container

The "servicemanager" is the actual container which instantiates, configures, and manages a number of objects. These "services" typically collaborate with one another, and thus have dependencies between themselves. These dependencies are reflected in the configuration data used by the ServiceManager.

How to use servicemanager

For our sample we use two class class1 and class2. Class1 will be injected with an instance of Class2 and other simple values and values from the configuration.


<?php
class Class1 {
	var $constructorObject;
	var $constructorConfigValue;  
	var $constructorValue;
	var $setterValue;
	var $setterConfigValue;
	function __constructor(&$object,$value,$configValue)  {
		$this->constructorObject=&$object;
		$this->constructorValue = $value;
		$this->constructorConfigValue=$configValue;
	}
	function & getConstructorClass2() { 
		return $this->constructorObject;
	}
	function getConstructorValue() { 
		return $this->constructorValue;
	}
	function getConstructorConfigValue() { 
		return $this->constructorConfigValue;
	}
	function setSetterClass2(&$class2) { 
		$thio->setterObject= &$class2;
	}
	function & getSetterClass2() { 
		return $this->constructorObject;
	}
	function setSetterValues($value,$configValue) { 
		$this->setterValue=$value;
		$this->setterConfigValue=$configValue;
	}
}
?>
------------------------------------------------
<?php
class Class2 { 
}
?>
------------------------------------------------

Each modul has an configuration file: "${application}/dependencies/profile.res".
In this configuration file there 4 main categories:
1. [interfaces]
Configure here all instances in the following way:
key=namespace file className instanceType [NORMAL|SINGLETON|SESSIONSINGLETON]

2. [constructorInjection]
Configure here how the constructor injection should work.
The key is defined in the following way:
"interfacekey"."argument nummber"."referencetype".
The refernce type can be.
ref = Reference to another instance
value = A simple value
config = The value will be provided through the

3. [constructorInjection]
Configure here how the setter injection should work.
The key is defined in the following way:
"interfacekey"."method name"."argument nummber"."referencetype".

4. [components]
Configure here the depencies to other modules. You can access instances of the other modul with "component.interfaceKey"
For example if you want to inject an instance of your modul with an instance of other modul use "othermodul.interfayKey"

[interfaces]
class1=sample.application class1 class1 SINGLETON
class2=sample.application class1 class1 SINGLETON

[constructorInjection]
class1.0.ref=class2
class1.1.value=constructor.value
class1.2.config=test.test.test

[setterInjection]
class1.setterclass2.0.ref=class2
class1.settervalues.0.value=setter.value
class1.settervalues.1.config=test.test.test

[components]
othermodul=sample.othermodul.dependencies.profile

To get an object from the servicemanager inside your testcase use:

$builder = singleton_singleton::getInstance('servicemanager_ServiceMangerBuilder');
$serviceManager = $builder->createServiceManager('module.namespace','context');
$object =  $serviceManager->getServiceObject('interfaceKey');

The ServiceMangerBuilder will provide allways the same ServiceManager for the same profile.res