4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 //@require 'Swift/DependencyException.php';
14 * Dependency Injection container.
16 * @author Chris Corbyn
18 class Swift_DependencyContainer
21 /** Constant for literal value types */
22 const TYPE_VALUE
= 0x0001;
24 /** Constant for new instance types */
25 const TYPE_INSTANCE
= 0x0010;
27 /** Constant for shared instance types */
28 const TYPE_SHARED
= 0x0100;
30 /** Constant for aliases */
31 const TYPE_ALIAS
= 0x1000;
33 /** Singleton instance */
34 private static $_instance = null;
36 /** The data container */
37 private $_store = array();
39 /** The current endpoint in the data container */
43 * Constructor should not be used.
44 * Use {@link getInstance()} instead.
46 public function __construct() { }
49 * Returns a singleton of the DependencyContainer.
50 * @return Swift_DependencyContainer
52 public static function getInstance()
54 if (!isset(self
::$_instance))
56 self
::$_instance = new self();
58 return self
::$_instance;
62 * List the names of all items stored in the Container.
65 public function listItems()
67 return array_keys($this->_store
);
71 * Test if an item is registered in this container with the given name.
72 * @param string $itemName
76 public function has($itemName)
78 return array_key_exists($itemName, $this->_store
)
79 && isset($this->_store
[$itemName]['lookupType']);
83 * Lookup the item with the given $itemName.
84 * @param string $itemName
86 * @throws Swift_DependencyException If the dependency is not found
89 public function lookup($itemName)
91 if (!$this->has($itemName))
93 throw new Swift_DependencyException(
94 'Cannot lookup dependency "' . $itemName . '" since it is not registered.'
98 switch ($this->_store
[$itemName]['lookupType'])
100 case self
::TYPE_ALIAS
:
101 return $this->_createAlias($itemName);
102 case self
::TYPE_VALUE
:
103 return $this->_getValue($itemName);
104 case self
::TYPE_INSTANCE
:
105 return $this->_createNewInstance($itemName);
106 case self
::TYPE_SHARED
:
107 return $this->_createSharedInstance($itemName);
112 * Create an array of arguments passed to the constructor of $itemName.
113 * @param string $itemName
116 public function createDependenciesFor($itemName)
119 if (isset($this->_store
[$itemName]['args']))
121 $args = $this->_resolveArgs($this->_store
[$itemName]['args']);
127 * Register a new dependency with $itemName.
128 * This method returns the current DependencyContainer instance because it
129 * requires the use of the fluid interface to set the specific details for the
132 * @param string $itemName
133 * @return Swift_DependencyContainer
134 * @see asNewInstanceOf(), asSharedInstanceOf(), asValue()
136 public function register($itemName)
138 $this->_store
[$itemName] = array();
139 $this->_endPoint
=& $this->_store
[$itemName];
144 * Specify the previously registered item as a literal value.
145 * {@link register()} must be called before this will work.
147 * @param mixed $value
148 * @return Swift_DependencyContainer
150 public function asValue($value)
152 $endPoint =& $this->_getEndPoint();
153 $endPoint['lookupType'] = self
::TYPE_VALUE
;
154 $endPoint['value'] = $value;
159 * Specify the previously registered item as an alias of another item.
160 * @param string $lookup
161 * @return Swift_DependencyContainer
163 public function asAliasOf($lookup)
165 $endPoint =& $this->_getEndPoint();
166 $endPoint['lookupType'] = self
::TYPE_ALIAS
;
167 $endPoint['ref'] = $lookup;
172 * Specify the previously registered item as a new instance of $className.
173 * {@link register()} must be called before this will work.
174 * Any arguments can be set with {@link withDependencies()},
175 * {@link addConstructorValue()} or {@link addConstructorLookup()}.
177 * @param string $className
178 * @return Swift_DependencyContainer
179 * @see withDependencies(), addConstructorValue(), addConstructorLookup()
181 public function asNewInstanceOf($className)
183 $endPoint =& $this->_getEndPoint();
184 $endPoint['lookupType'] = self
::TYPE_INSTANCE
;
185 $endPoint['className'] = $className;
190 * Specify the previously registered item as a shared instance of $className.
191 * {@link register()} must be called before this will work.
192 * @param string $className
193 * @return Swift_DependencyContainer
195 public function asSharedInstanceOf($className)
197 $endPoint =& $this->_getEndPoint();
198 $endPoint['lookupType'] = self
::TYPE_SHARED
;
199 $endPoint['className'] = $className;
204 * Specify a list of injected dependencies for the previously registered item.
205 * This method takes an array of lookup names.
207 * @param array $lookups
208 * @return Swift_DependencyContainer
209 * @see addConstructorValue(), addConstructorLookup()
211 public function withDependencies(array $lookups)
213 $endPoint =& $this->_getEndPoint();
214 $endPoint['args'] = array();
215 foreach ($lookups as $lookup)
217 $this->addConstructorLookup($lookup);
223 * Specify a literal (non looked up) value for the constructor of the
224 * previously registered item.
226 * @param mixed $value
227 * @return Swift_DependencyContainer
228 * @see withDependencies(), addConstructorLookup()
230 public function addConstructorValue($value)
232 $endPoint =& $this->_getEndPoint();
233 if (!isset($endPoint['args']))
235 $endPoint['args'] = array();
237 $endPoint['args'][] = array('type' => 'value', 'item' => $value);
242 * Specify a dependency lookup for the constructor of the previously
245 * @param string $lookup
246 * @return Swift_DependencyContainer
247 * @see withDependencies(), addConstructorValue()
249 public function addConstructorLookup($lookup)
251 $endPoint =& $this->_getEndPoint();
252 if (!isset($this->_endPoint
['args']))
254 $endPoint['args'] = array();
256 $endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
260 // -- Private methods
262 /** Get the literal value with $itemName */
263 private function _getValue($itemName)
265 return $this->_store
[$itemName]['value'];
268 /** Resolve an alias to another item */
269 private function _createAlias($itemName)
271 return $this->lookup($this->_store
[$itemName]['ref']);
274 /** Create a fresh instance of $itemName */
275 private function _createNewInstance($itemName)
277 $reflector = new ReflectionClass($this->_store
[$itemName]['className']);
278 if ($reflector->getConstructor())
280 return $reflector->newInstanceArgs(
281 $this->createDependenciesFor($itemName)
286 return $reflector->newInstance();
290 /** Create and register a shared instance of $itemName */
291 private function _createSharedInstance($itemName)
293 if (!isset($this->_store
[$itemName]['instance']))
295 $this->_store
[$itemName]['instance'] = $this->_createNewInstance($itemName);
297 return $this->_store
[$itemName]['instance'];
300 /** Get the current endpoint in the store */
301 private function &_getEndPoint()
303 if (!isset($this->_endPoint
))
305 throw new BadMethodCallException(
306 'Component must first be registered by calling register()'
309 return $this->_endPoint
;
312 /** Get an argument list with dependencies resolved */
313 private function _resolveArgs(array $args)
316 foreach ($args as $argDefinition)
318 switch ($argDefinition['type'])
321 $resolved[] = $this->_lookupRecursive($argDefinition['item']);
324 $resolved[] = $argDefinition['item'];
331 /** Resolve a single dependency with an collections */
332 private function _lookupRecursive($item)
336 $collection = array();
337 foreach ($item as $k => $v)
339 $collection[$k] = $this->_lookupRecursive($v);
345 return $this->lookup($item);