54 if (!isset(self::$_instance))
56 self::$_instance =
new self();
58 return self::$_instance;
67 return array_keys($this->_store);
76 public function has($itemName)
78 return array_key_exists($itemName, $this->_store)
79 && isset($this->_store[$itemName][
'lookupType']);
91 if (!$this->
has($itemName))
94 'Cannot lookup dependency "' . $itemName .
'" since it is not registered.'
98 switch ($this->_store[$itemName][
'lookupType'])
100 case self::TYPE_ALIAS:
102 case self::TYPE_VALUE:
104 case self::TYPE_INSTANCE:
106 case self::TYPE_SHARED:
119 if (isset($this->_store[$itemName][
'args']))
121 $args = $this->
_resolveArgs($this->_store[$itemName][
'args']);
136 public function register($itemName)
138 $this->_store[$itemName] = array();
139 $this->_endPoint =& $this->_store[$itemName];
153 $endPoint[
'lookupType'] = self::TYPE_VALUE;
154 $endPoint[
'value'] = $value;
166 $endPoint[
'lookupType'] = self::TYPE_ALIAS;
167 $endPoint[
'ref'] = $lookup;
184 $endPoint[
'lookupType'] = self::TYPE_INSTANCE;
185 $endPoint[
'className'] = $className;
198 $endPoint[
'lookupType'] = self::TYPE_SHARED;
199 $endPoint[
'className'] = $className;
214 $endPoint[
'args'] = array();
215 foreach ($lookups as $lookup)
233 if (!isset($endPoint[
'args']))
235 $endPoint[
'args'] = array();
237 $endPoint[
'args'][] = array(
'type' =>
'value',
'item' => $value);
252 if (!isset($this->_endPoint[
'args']))
254 $endPoint[
'args'] = array();
256 $endPoint[
'args'][] = array(
'type' =>
'lookup',
'item' => $lookup);
265 return $this->_store[$itemName][
'value'];
271 return $this->
lookup($this->_store[$itemName][
'ref']);
277 $reflector =
new ReflectionClass($this->_store[$itemName][
'className']);
278 if ($reflector->getConstructor())
280 return $reflector->newInstanceArgs(
286 return $reflector->newInstance();
293 if (!isset($this->_store[$itemName][
'instance']))
297 return $this->_store[$itemName][
'instance'];
303 if (!isset($this->_endPoint))
305 throw new BadMethodCallException(
306 'Component must first be registered by calling register()'
316 foreach ($args as $argDefinition)
318 switch ($argDefinition[
'type'])
324 $resolved[] = $argDefinition[
'item'];
336 $collection = array();
337 foreach ($item as $k => $v)
345 return $this->
lookup($item);