DependencyContainer.php 9.6 KB

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