/framework/vendor/swift/lib/classes/Swift/DependencyContainer.php

http://zoop.googlecode.com/ · PHP · 349 lines · 195 code · 34 blank · 120 comment · 12 complexity · 0bb31b04a8c55b76f6a8fcba02cda2d9 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/DependencyException.php';
  10. /**
  11. * Dependency Injection container.
  12. * @package Swift
  13. * @author Chris Corbyn
  14. */
  15. class Swift_DependencyContainer
  16. {
  17. /** Constant for literal value types */
  18. const TYPE_VALUE = 0x0001;
  19. /** Constant for new instance types */
  20. const TYPE_INSTANCE = 0x0010;
  21. /** Constant for shared instance types */
  22. const TYPE_SHARED = 0x0100;
  23. /** Constant for aliases */
  24. const TYPE_ALIAS = 0x1000;
  25. /** Singleton instance */
  26. private static $_instance = null;
  27. /** The data container */
  28. private $_store = array();
  29. /** The current endpoint in the data container */
  30. private $_endPoint;
  31. /**
  32. * Constructor should not be used.
  33. * Use {@link getInstance()} instead.
  34. */
  35. public function __construct() { }
  36. /**
  37. * Returns a singleton of the DependencyContainer.
  38. * @return Swift_DependencyContainer
  39. */
  40. public static function getInstance()
  41. {
  42. if (!isset(self::$_instance))
  43. {
  44. self::$_instance = new self();
  45. }
  46. return self::$_instance;
  47. }
  48. /**
  49. * List the names of all items stored in the Container.
  50. * @return array
  51. */
  52. public function listItems()
  53. {
  54. return array_keys($this->_store);
  55. }
  56. /**
  57. * Test if an item is registered in this container with the given name.
  58. * @param string $itemName
  59. * @return boolean
  60. * @see register()
  61. */
  62. public function has($itemName)
  63. {
  64. return array_key_exists($itemName, $this->_store)
  65. && isset($this->_store[$itemName]['lookupType']);
  66. }
  67. /**
  68. * Lookup the item with the given $itemName.
  69. * @param string $itemName
  70. * @return mixed
  71. * @throws Swift_DependencyException If the dependency is not found
  72. * @see register()
  73. */
  74. public function lookup($itemName)
  75. {
  76. if (!$this->has($itemName))
  77. {
  78. throw new Swift_DependencyException(
  79. 'Cannot lookup dependency "' . $itemName . '" since it is not registered.'
  80. );
  81. }
  82. switch ($this->_store[$itemName]['lookupType'])
  83. {
  84. case self::TYPE_ALIAS:
  85. return $this->_createAlias($itemName);
  86. case self::TYPE_VALUE:
  87. return $this->_getValue($itemName);
  88. case self::TYPE_INSTANCE:
  89. return $this->_createNewInstance($itemName);
  90. case self::TYPE_SHARED:
  91. return $this->_createSharedInstance($itemName);
  92. }
  93. }
  94. /**
  95. * Create an array of arguments passed to the constructor of $itemName.
  96. * @param string $itemName
  97. * @return array
  98. */
  99. public function createDependenciesFor($itemName)
  100. {
  101. $args = array();
  102. if (isset($this->_store[$itemName]['args']))
  103. {
  104. $args = $this->_resolveArgs($this->_store[$itemName]['args']);
  105. }
  106. return $args;
  107. }
  108. /**
  109. * Register a new dependency with $itemName.
  110. * This method returns the current DependencyContainer instance because it
  111. * requires the use of the fluid interface to set the specific details for the
  112. * dependency.
  113. *
  114. * @param string $itemName
  115. * @return Swift_DependencyContainer
  116. * @see asNewInstanceOf(), asSharedInstanceOf(), asValue()
  117. */
  118. public function register($itemName)
  119. {
  120. $this->_store[$itemName] = array();
  121. $this->_endPoint =& $this->_store[$itemName];
  122. return $this;
  123. }
  124. /**
  125. * Specify the previously registered item as a literal value.
  126. * {@link register()} must be called before this will work.
  127. *
  128. * @param mixed $value
  129. * @return Swift_DependencyContainer
  130. */
  131. public function asValue($value)
  132. {
  133. $endPoint =& $this->_getEndPoint();
  134. $endPoint['lookupType'] = self::TYPE_VALUE;
  135. $endPoint['value'] = $value;
  136. return $this;
  137. }
  138. /**
  139. * Specify the previously registered item as an alias of another item.
  140. * @param string $lookup
  141. * @return Swift_DependencyContainer
  142. */
  143. public function asAliasOf($lookup)
  144. {
  145. $endPoint =& $this->_getEndPoint();
  146. $endPoint['lookupType'] = self::TYPE_ALIAS;
  147. $endPoint['ref'] = $lookup;
  148. return $this;
  149. }
  150. /**
  151. * Specify the previously registered item as a new instance of $className.
  152. * {@link register()} must be called before this will work.
  153. * Any arguments can be set with {@link withDependencies()},
  154. * {@link addConstructorValue()} or {@link addConstructorLookup()}.
  155. *
  156. * @param string $className
  157. * @return Swift_DependencyContainer
  158. * @see withDependencies(), addConstructorValue(), addConstructorLookup()
  159. */
  160. public function asNewInstanceOf($className)
  161. {
  162. $endPoint =& $this->_getEndPoint();
  163. $endPoint['lookupType'] = self::TYPE_INSTANCE;
  164. $endPoint['className'] = $className;
  165. return $this;
  166. }
  167. /**
  168. * Specify the previously registered item as a shared instance of $className.
  169. * {@link register()} must be called before this will work.
  170. * @param string $className
  171. * @return Swift_DependencyContainer
  172. */
  173. public function asSharedInstanceOf($className)
  174. {
  175. $endPoint =& $this->_getEndPoint();
  176. $endPoint['lookupType'] = self::TYPE_SHARED;
  177. $endPoint['className'] = $className;
  178. return $this;
  179. }
  180. /**
  181. * Specify a list of injected dependencies for the previously registered item.
  182. * This method takes an array of lookup names.
  183. *
  184. * @param array $lookups
  185. * @return Swift_DependencyContainer
  186. * @see addConstructorValue(), addConstructorLookup()
  187. */
  188. public function withDependencies(array $lookups)
  189. {
  190. $endPoint =& $this->_getEndPoint();
  191. $endPoint['args'] = array();
  192. foreach ($lookups as $lookup)
  193. {
  194. $this->addConstructorLookup($lookup);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Specify a literal (non looked up) value for the constructor of the
  200. * previously registered item.
  201. *
  202. * @param mixed $value
  203. * @return Swift_DependencyContainer
  204. * @see withDependencies(), addConstructorLookup()
  205. */
  206. public function addConstructorValue($value)
  207. {
  208. $endPoint =& $this->_getEndPoint();
  209. if (!isset($endPoint['args']))
  210. {
  211. $endPoint['args'] = array();
  212. }
  213. $endPoint['args'][] = array('type' => 'value', 'item' => $value);
  214. return $this;
  215. }
  216. /**
  217. * Specify a dependency lookup for the constructor of the previously
  218. * registered item.
  219. *
  220. * @param string $lookup
  221. * @return Swift_DependencyContainer
  222. * @see withDependencies(), addConstructorValue()
  223. */
  224. public function addConstructorLookup($lookup)
  225. {
  226. $endPoint =& $this->_getEndPoint();
  227. if (!isset($this->_endPoint['args']))
  228. {
  229. $endPoint['args'] = array();
  230. }
  231. $endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
  232. return $this;
  233. }
  234. // -- Private methods
  235. /** Get the literal value with $itemName */
  236. private function _getValue($itemName)
  237. {
  238. return $this->_store[$itemName]['value'];
  239. }
  240. /** Resolve an alias to another item */
  241. private function _createAlias($itemName)
  242. {
  243. return $this->lookup($this->_store[$itemName]['ref']);
  244. }
  245. /** Create a fresh instance of $itemName */
  246. private function _createNewInstance($itemName)
  247. {
  248. $reflector = new ReflectionClass($this->_store[$itemName]['className']);
  249. if ($reflector->getConstructor())
  250. {
  251. return $reflector->newInstanceArgs(
  252. $this->createDependenciesFor($itemName)
  253. );
  254. }
  255. else
  256. {
  257. return $reflector->newInstance();
  258. }
  259. }
  260. /** Create and register a shared instance of $itemName */
  261. private function _createSharedInstance($itemName)
  262. {
  263. if (!isset($this->_store[$itemName]['instance']))
  264. {
  265. $this->_store[$itemName]['instance'] = $this->_createNewInstance($itemName);
  266. }
  267. return $this->_store[$itemName]['instance'];
  268. }
  269. /** Get the current endpoint in the store */
  270. private function &_getEndPoint()
  271. {
  272. if (!isset($this->_endPoint))
  273. {
  274. throw new BadMethodCallException(
  275. 'Component must first be registered by calling register()'
  276. );
  277. }
  278. return $this->_endPoint;
  279. }
  280. /** Get an argument list with dependencies resolved */
  281. private function _resolveArgs(array $args)
  282. {
  283. $resolved = array();
  284. foreach ($args as $argDefinition)
  285. {
  286. switch ($argDefinition['type'])
  287. {
  288. case 'lookup':
  289. $resolved[] = $this->_lookupRecursive($argDefinition['item']);
  290. break;
  291. case 'value':
  292. $resolved[] = $argDefinition['item'];
  293. break;
  294. }
  295. }
  296. return $resolved;
  297. }
  298. /** Resolve a single dependency with an collections */
  299. private function _lookupRecursive($item)
  300. {
  301. if (is_array($item))
  302. {
  303. $collection = array();
  304. foreach ($item as $k => $v)
  305. {
  306. $collection[$k] = $this->_lookupRecursive($v);
  307. }
  308. return $collection;
  309. }
  310. else
  311. {
  312. return $this->lookup($item);
  313. }
  314. }
  315. }