64 'eventManager' =>
'wcmf\lib\core\EventManager',
65 'logger' =>
'wcmf\lib\core\Logger',
66 'logManager' =>
'wcmf\lib\core\LogManager',
67 'session' =>
'wcmf\lib\core\Session',
68 'configuration' =>
'wcmf\lib\config\Configuration',
69 'localization' =>
'wcmf\lib\i18n\Localization',
70 'message' =>
'wcmf\lib\i18n\Message',
71 'cache' =>
'wcmf\lib\io\Cache',
72 'persistenceFacade' =>
'wcmf\lib\persistence\PersistenceFacade',
73 'transaction' =>
'wcmf\lib\persistence\Transaction',
74 'concurrencyManager' =>
'wcmf\lib\persistence\concurrency\ConcurrencyManager',
75 'actionMapper' =>
'wcmf\lib\presentation\ActionMapper',
76 'request' =>
'wcmf\lib\presentation\Request',
77 'response' =>
'wcmf\lib\presentation\Response',
78 'listStrategies' =>
'wcmf\lib\presentation\control\lists\ListStrategy',
79 'formats' =>
'wcmf\lib\presentation\format\Format',
80 'formatter' =>
'wcmf\lib\presentation\format\Formatter',
81 'view' =>
'wcmf\lib\presentation\view\View',
82 'authenticationManager' =>
'wcmf\lib\security\AuthenticationManager',
83 'permissionManager' =>
'wcmf\lib\security\PermissionManager',
84 'principalFactory' =>
'wcmf\lib\security\principal\PrincipalFactory',
85 'user' =>
'wcmf\lib\security\principal\User',
86 'role' =>
'wcmf\lib\security\principal\Role',
105 $this->_configuration = $configuration;
111 public function getInstance($name, $dynamicConfiguration=array()) {
115 $instanceKey =
sizeof($dynamicConfiguration) == 0 ? strtolower($name) :
116 strtolower($name.json_encode($dynamicConfiguration));
119 if (!isset($this->_instances[$instanceKey])) {
121 $staticConfiguration = $this->_configuration->getSection($name);
122 $configuration = array_merge($staticConfiguration, $dynamicConfiguration);
123 $instance = $this->
createInstance($name, $configuration, $instanceKey);
126 $instance = $this->_instances[$instanceKey];
135 $configuration = array_merge(array(
138 ), $dynamicConfiguration);
148 $instanceKey = strtolower($name);
149 $this->_instances[$instanceKey] = $instance;
156 $this->_requiredInterfaces = array_merge($this->_requiredInterfaces, $interfaces);
163 $this->_instances = array();
190 if (isset($configuration[
'__class'])) {
192 $className = $configuration[
'__class'];
195 if (class_exists($className)) {
199 $refClass = new \ReflectionClass($className);
200 if ($refClass->hasMethod(
'__construct')) {
201 $refConstructor = new \ReflectionMethod($className,
'__construct');
202 $refParameters = $refConstructor->getParameters();
203 foreach ($refParameters as $param) {
204 $paramName = $param->name;
205 $paramInstanceKey = strtolower($paramName);
206 if (isset($this->_instances[$paramInstanceKey])) {
208 $cParams[$paramName] = $this->_instances[$paramInstanceKey];
210 elseif (isset($configuration[$paramName])) {
212 $cParams[$paramName] = $this->
resolveValue($configuration[$paramName]);
214 elseif ($this->_configuration->hasSection($paramName)) {
216 $cParams[$paramName] = $this->
getInstance($paramName);
218 elseif (($paramClass = $param->getClass()) != null) {
225 '\' in
class \
''.$className.
'\' cannot be injected.
');
227 // delete resolved parameters from configuration
228 unset($configuration[$paramName]);
232 // create the instance
233 $obj = $refClass->newInstanceArgs($cParams);
235 // check against interface
236 $interface = $this->getInterface($name);
237 if ($interface != null && !($obj instanceof $interface)) {
238 throw new ConfigurationException('Class \
''.$className.
239 '\' is required to implement interface \
''.$interface.
'\'.
');
242 // register the instance if it is shared (default)
243 // NOTE we do this before setting the instance properties in order
244 // to allow to resolve circular dependencies (dependent objects that
245 // are injected into the current instance via property injection can
246 // already use this instance)
247 if (!isset($configuration['__shared
']) || $configuration['__shared
'] == 'true') {
248 $this->registerInstance($instanceKey, $obj);
251 // set the instance properties from the remaining configuration
252 foreach ($configuration as $key => $value) {
253 // exclude properties starting with __ and constructor parameters
254 if (strpos($key, '__
') !== 0 && !isset($cParams[$key])) {
255 $value = $this->resolveValue($value);
257 $setterName = $this->getSetterName($key);
258 if (method_exists($obj, $setterName)) {
259 $obj->$setterName($value);
269 throw new ConfigurationException('Class \
''.$className.
'\' is not found.
');
273 // the instance is a map
275 // get interface that map values should implement
276 $interface = $this->getInterface($name);
278 foreach ($configuration as $key => $value) {
279 // create instances for variables denoted by a leading $
280 if (strpos($value, '$
') === 0) {
281 $obj = $this->getInstance(preg_replace('/^\$/
', '', $value));
282 // check against interface
283 if ($interface != null && !($obj instanceof $interface)) {
284 throw new ConfigurationException('Class of \
''.$name.
'.'.$key.
285 '\' is required to implement interface \
''.$interface.
'\'.
');
287 $configuration[$key] = $obj;
290 // always register maps
291 $this->registerInstance($instanceKey, $configuration);
292 $instance = $configuration;
302 protected function resolveValue($value) {
303 // special treatments, if value is a string
304 if (is_string($value)) {
305 // replace variables denoted by a leading $
306 if (strpos($value, '$
') === 0) {
307 $value = $this->getInstance(preg_replace('/^\$/
', '', $value));
311 $lower = strtolower($value);
312 if ($lower === 'true') {
315 if ($lower === 'false') {
320 // special treatments, if value is an array
321 if (is_array($value)) {
323 $containsInstance = false;
324 // check for variables
325 foreach ($value as $val) {
326 if (is_string($val) && strpos($val, '$
') === 0) {
327 $result[] = $this->getInstance(preg_replace('/^\$/
', '', $val));
328 $containsInstance = true;
334 // only replace value, if the array containes an variable
335 if ($containsInstance) {
347 protected function getInterface($name) {
348 if (isset($this->_requiredInterfaces[$name])) {
349 return $this->_requiredInterfaces[$name];
$_requiredInterfaces
Interfaces that must be implemented by the given instances used in the framework. ...
__construct(Configuration $configuration)
Constructor.
DefaultFactory is used to create service instances.
createInstance($name, $configuration, $instanceKey)
Create an instance from the given configuration array.
addInterfaces(array $interfaces)
registerInstance($name, $instance)
getInstance($name, $dynamicConfiguration=array())
resolveValue($value)
Resolve a configuration value into a parameter.
getSetterName($property)
Get the setter method name for a property.
Implementations of Configuration give access to the application configuration.
Interface for Factory implementations.
static underScoreToCamelCase($string, $firstLowerCase=false)
Convert a string in underscore notation to camel case notation.
$_configuration
The Configuration instance that holds the instance definitions.
getClassInstance($class, $dynamicConfiguration=array())
$_instances
The registry for already created instances.
ConfigurationException signals an exception in the configuration.